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