@@ -472,100 +472,100 @@ private static void AddPythonMethod(string methodName, PyObject func, TypeBuilde
472
472
methodName = pyMethodName . As < string > ( ) ?? throw new ArgumentNullException ( methodNameAttribute ) ;
473
473
}
474
474
475
- using ( PyObject pyReturnType = func . GetAttr ( "_clr_return_type_" ) )
476
- using ( var pyArgTypes = PyIter . GetIter ( func . GetAttr ( "_clr_arg_types_" ) ) )
475
+ using var pyReturnType = func . GetAttr ( "_clr_return_type_" ) ;
476
+ using var pyArgTypes = func . GetAttr ( "_clr_arg_types_" ) ;
477
+ using var pyArgTypesIter = PyIter . GetIter ( pyArgTypes ) ;
478
+ var returnType = pyReturnType . AsManagedObject ( typeof ( Type ) ) as Type ;
479
+ if ( returnType == null )
477
480
{
478
- var returnType = pyReturnType . AsManagedObject ( typeof ( Type ) ) as Type ;
479
- if ( returnType == null )
480
- {
481
- returnType = typeof ( void ) ;
482
- }
481
+ returnType = typeof ( void ) ;
482
+ }
483
483
484
- var argTypes = new List < Type > ( ) ;
485
- foreach ( PyObject pyArgType in pyArgTypes )
484
+ var argTypes = new List < Type > ( ) ;
485
+ foreach ( PyObject pyArgType in pyArgTypesIter )
486
+ {
487
+ var argType = pyArgType . AsManagedObject ( typeof ( Type ) ) as Type ;
488
+ if ( argType == null )
486
489
{
487
- var argType = pyArgType . AsManagedObject ( typeof ( Type ) ) as Type ;
488
- if ( argType == null )
489
- {
490
- throw new ArgumentException ( "_clr_arg_types_ must be a list or tuple of CLR types" ) ;
491
- }
492
- argTypes . Add ( argType ) ;
490
+ throw new ArgumentException ( "_clr_arg_types_ must be a list or tuple of CLR types" ) ;
493
491
}
492
+ argTypes . Add ( argType ) ;
493
+ pyArgType . Dispose ( ) ;
494
+ }
494
495
495
- // add the method to call back into python
496
- MethodAttributes methodAttribs = MethodAttributes . Public |
497
- MethodAttributes . Virtual |
498
- MethodAttributes . ReuseSlot |
499
- MethodAttributes . HideBySig ;
496
+ // add the method to call back into python
497
+ MethodAttributes methodAttribs = MethodAttributes . Public |
498
+ MethodAttributes . Virtual |
499
+ MethodAttributes . ReuseSlot |
500
+ MethodAttributes . HideBySig ;
500
501
501
- MethodBuilder methodBuilder = typeBuilder . DefineMethod ( methodName ,
502
- methodAttribs ,
503
- returnType ,
504
- argTypes . ToArray ( ) ) ;
502
+ MethodBuilder methodBuilder = typeBuilder . DefineMethod ( methodName ,
503
+ methodAttribs ,
504
+ returnType ,
505
+ argTypes . ToArray ( ) ) ;
505
506
506
- ILGenerator il = methodBuilder . GetILGenerator ( ) ;
507
+ ILGenerator il = methodBuilder . GetILGenerator ( ) ;
507
508
508
- il . DeclareLocal ( typeof ( object [ ] ) ) ;
509
- il . DeclareLocal ( typeof ( RuntimeMethodHandle ) ) ;
509
+ il . DeclareLocal ( typeof ( object [ ] ) ) ;
510
+ il . DeclareLocal ( typeof ( RuntimeMethodHandle ) ) ;
510
511
511
- // this
512
- il . Emit ( OpCodes . Ldarg_0 ) ;
512
+ // this
513
+ il . Emit ( OpCodes . Ldarg_0 ) ;
513
514
514
- // Python method to call
515
- il . Emit ( OpCodes . Ldstr , methodName ) ;
515
+ // Python method to call
516
+ il . Emit ( OpCodes . Ldstr , methodName ) ;
516
517
517
- // original method name
518
- il . Emit ( OpCodes . Ldnull ) ; // don't fall back to the base type's method
518
+ // original method name
519
+ il . Emit ( OpCodes . Ldnull ) ; // don't fall back to the base type's method
519
520
520
- // create args array
521
- il . Emit ( OpCodes . Ldc_I4 , argTypes . Count ) ;
522
- il . Emit ( OpCodes . Newarr , typeof ( object ) ) ;
523
- il . Emit ( OpCodes . Stloc_0 ) ;
521
+ // create args array
522
+ il . Emit ( OpCodes . Ldc_I4 , argTypes . Count ) ;
523
+ il . Emit ( OpCodes . Newarr , typeof ( object ) ) ;
524
+ il . Emit ( OpCodes . Stloc_0 ) ;
524
525
525
- // fill args array
526
- for ( var i = 0 ; i < argTypes . Count ; ++ i )
526
+ // fill args array
527
+ for ( var i = 0 ; i < argTypes . Count ; ++ i )
528
+ {
529
+ il . Emit ( OpCodes . Ldloc_0 ) ;
530
+ il . Emit ( OpCodes . Ldc_I4 , i ) ;
531
+ il . Emit ( OpCodes . Ldarg , i + 1 ) ;
532
+ var type = argTypes [ i ] ;
533
+ if ( type . IsByRef )
527
534
{
528
- il . Emit ( OpCodes . Ldloc_0 ) ;
529
- il . Emit ( OpCodes . Ldc_I4 , i ) ;
530
- il . Emit ( OpCodes . Ldarg , i + 1 ) ;
531
- var type = argTypes [ i ] ;
532
- if ( type . IsByRef )
533
- {
534
- type = type . GetElementType ( ) ;
535
- il . Emit ( OpCodes . Ldobj , type ) ;
536
- }
537
- if ( type . IsValueType )
538
- {
539
- il . Emit ( OpCodes . Box , type ) ;
540
- }
541
- il . Emit ( OpCodes . Stelem , typeof ( object ) ) ;
535
+ type = type . GetElementType ( ) ;
536
+ il . Emit ( OpCodes . Ldobj , type ) ;
537
+ }
538
+ if ( type . IsValueType )
539
+ {
540
+ il . Emit ( OpCodes . Box , type ) ;
542
541
}
542
+ il . Emit ( OpCodes . Stelem , typeof ( object ) ) ;
543
+ }
543
544
544
- // args array
545
- il . Emit ( OpCodes . Ldloc_0 ) ;
545
+ // args array
546
+ il . Emit ( OpCodes . Ldloc_0 ) ;
546
547
547
- // method handle for the base method is null
548
- il . Emit ( OpCodes . Ldloca_S , 1 ) ;
549
- il . Emit ( OpCodes . Initobj , typeof ( RuntimeMethodHandle ) ) ;
550
- il . Emit ( OpCodes . Ldloc_1 ) ;
548
+ // method handle for the base method is null
549
+ il . Emit ( OpCodes . Ldloca_S , 1 ) ;
550
+ il . Emit ( OpCodes . Initobj , typeof ( RuntimeMethodHandle ) ) ;
551
+ il . Emit ( OpCodes . Ldloc_1 ) ;
551
552
#pragma warning disableCS0618 // PythonDerivedType is for internal use only
552
553
553
- // invoke the method
554
- if ( returnType == typeof ( void ) )
555
- {
556
- il . Emit ( OpCodes . Call , typeof ( PythonDerivedType ) . GetMethod ( nameof ( InvokeMethodVoid ) ) ) ;
557
- }
558
- else
559
- {
560
- il . Emit ( OpCodes . Call ,
561
- typeof ( PythonDerivedType ) . GetMethod ( nameof ( InvokeMethod ) ) . MakeGenericMethod ( returnType ) ) ;
562
- }
554
+ // invoke the method
555
+ if ( returnType == typeof ( void ) )
556
+ {
557
+ il . Emit ( OpCodes . Call , typeof ( PythonDerivedType ) . GetMethod ( nameof ( InvokeMethodVoid ) ) ) ;
558
+ }
559
+ else
560
+ {
561
+ il . Emit ( OpCodes . Call ,
562
+ typeof ( PythonDerivedType ) . GetMethod ( nameof ( InvokeMethod ) ) . MakeGenericMethod ( returnType ) ) ;
563
+ }
563
564
564
- CodeGenerator . GenerateMarshalByRefsBack ( il , argTypes ) ;
565
+ CodeGenerator . GenerateMarshalByRefsBack ( il , argTypes ) ;
565
566
566
567
#pragma warning restoreCS0618 // PythonDerivedType is for internal use only
567
- il . Emit ( OpCodes . Ret ) ;
568
- }
568
+ il . Emit ( OpCodes . Ret ) ;
569
569
}
570
570
571
571
/// <summary>
@@ -584,68 +584,62 @@ private static void AddPythonProperty(string propertyName, PyObject func, TypeBu
584
584
MethodAttributes . HideBySig |
585
585
MethodAttributes . SpecialName ;
586
586
587
- using ( PyObject pyPropertyType = func . GetAttr ( "_clr_property_type_" ) )
587
+ using var pyPropertyType = func . GetAttr ( "_clr_property_type_" ) ;
588
+ var propertyType = pyPropertyType . AsManagedObject ( typeof ( Type ) ) as Type ;
589
+ if ( propertyType == null )
588
590
{
589
- var propertyType = pyPropertyType . AsManagedObject ( typeof ( Type ) ) as Type ;
590
- if ( propertyType == null )
591
- {
592
- throw new ArgumentException ( "_clr_property_type must be a CLR type" ) ;
593
- }
591
+ throw new ArgumentException ( "_clr_property_type must be a CLR type" ) ;
592
+ }
594
593
595
- PropertyBuilder propertyBuilder = typeBuilder . DefineProperty ( propertyName ,
596
- PropertyAttributes . None ,
597
- propertyType ,
598
- null ) ;
594
+ PropertyBuilder propertyBuilder = typeBuilder . DefineProperty ( propertyName ,
595
+ PropertyAttributes . None ,
596
+ propertyType ,
597
+ null ) ;
599
598
600
- if ( func . HasAttr ( "fget" ) )
599
+ if ( func . HasAttr ( "fget" ) )
600
+ {
601
+ using var pyfget = func . GetAttr ( "fget" ) ;
602
+ if ( pyfget . IsTrue ( ) )
601
603
{
602
- using ( PyObject pyfget = func . GetAttr ( "fget" ) )
603
- {
604
- if ( pyfget . IsTrue ( ) )
605
- {
606
- MethodBuilder methodBuilder = typeBuilder . DefineMethod ( "get_" + propertyName ,
607
- methodAttribs ,
608
- propertyType ,
609
- null ) ;
610
-
611
- ILGenerator il = methodBuilder . GetILGenerator ( ) ;
612
- il . Emit ( OpCodes . Ldarg_0 ) ;
613
- il . Emit ( OpCodes . Ldstr , propertyName ) ;
604
+ MethodBuilder methodBuilder = typeBuilder . DefineMethod ( "get_" + propertyName ,
605
+ methodAttribs ,
606
+ propertyType ,
607
+ null ) ;
608
+
609
+ ILGenerator il = methodBuilder . GetILGenerator ( ) ;
610
+ il . Emit ( OpCodes . Ldarg_0 ) ;
611
+ il . Emit ( OpCodes . Ldstr , propertyName ) ;
614
612
#pragma warning disableCS0618 // PythonDerivedType is for internal use only
615
- il . Emit ( OpCodes . Call ,
616
- typeof ( PythonDerivedType ) . GetMethod ( "InvokeGetProperty" ) . MakeGenericMethod ( propertyType ) ) ;
613
+ il . Emit ( OpCodes . Call ,
614
+ typeof ( PythonDerivedType ) . GetMethod ( "InvokeGetProperty" ) . MakeGenericMethod ( propertyType ) ) ;
617
615
#pragma warning restoreCS0618 // PythonDerivedType is for internal use only
618
- il . Emit ( OpCodes . Ret ) ;
616
+ il . Emit ( OpCodes . Ret ) ;
619
617
620
- propertyBuilder . SetGetMethod ( methodBuilder ) ;
621
- }
622
- }
618
+ propertyBuilder . SetGetMethod ( methodBuilder ) ;
623
619
}
620
+ }
624
621
625
- if ( func . HasAttr ( "fset" ) )
622
+ if ( func . HasAttr ( "fset" ) )
623
+ {
624
+ using var pyset = func . GetAttr ( "fset" ) ;
625
+ if ( pyset . IsTrue ( ) )
626
626
{
627
- using ( PyObject pyset = func . GetAttr ( "fset" ) )
628
- {
629
- if ( pyset . IsTrue ( ) )
630
- {
631
- MethodBuilder methodBuilder = typeBuilder . DefineMethod ( "set_" + propertyName ,
632
- methodAttribs ,
633
- null ,
634
- new [ ] { propertyType } ) ;
635
-
636
- ILGenerator il = methodBuilder . GetILGenerator ( ) ;
637
- il . Emit ( OpCodes . Ldarg_0 ) ;
638
- il . Emit ( OpCodes . Ldstr , propertyName ) ;
639
- il . Emit ( OpCodes . Ldarg_1 ) ;
627
+ MethodBuilder methodBuilder = typeBuilder . DefineMethod ( "set_" + propertyName ,
628
+ methodAttribs ,
629
+ null ,
630
+ new [ ] { propertyType } ) ;
631
+
632
+ ILGenerator il = methodBuilder . GetILGenerator ( ) ;
633
+ il . Emit ( OpCodes . Ldarg_0 ) ;
634
+ il . Emit ( OpCodes . Ldstr , propertyName ) ;
635
+ il . Emit ( OpCodes . Ldarg_1 ) ;
640
636
#pragma warning disableCS0618 // PythonDerivedType is for internal use only
641
- il . Emit ( OpCodes . Call ,
642
- typeof ( PythonDerivedType ) . GetMethod ( "InvokeSetProperty" ) . MakeGenericMethod ( propertyType ) ) ;
637
+ il . Emit ( OpCodes . Call ,
638
+ typeof ( PythonDerivedType ) . GetMethod ( "InvokeSetProperty" ) . MakeGenericMethod ( propertyType ) ) ;
643
639
#pragma warning restoreCS0618 // PythonDerivedType is for internal use only
644
- il . Emit ( OpCodes . Ret ) ;
640
+ il . Emit ( OpCodes . Ret ) ;
645
641
646
- propertyBuilder . SetSetMethod ( methodBuilder ) ;
647
- }
648
- }
642
+ propertyBuilder . SetSetMethod ( methodBuilder ) ;
649
643
}
650
644
}
651
645
}