@@ -472,100 +472,100 @@ private static void AddPythonMethod(string methodName, PyObject func, TypeBuilde
472472 methodName = pyMethodName . As < string > ( ) ?? throw new ArgumentNullException ( methodNameAttribute ) ;
473473 }
474474
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 )
477480 {
478- var returnType = pyReturnType . AsManagedObject ( typeof ( Type ) ) as Type ;
479- if ( returnType == null )
480- {
481- returnType = typeof ( void ) ;
482- }
481+ returnType = typeof ( void ) ;
482+ }
483483
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 )
486489 {
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" ) ;
493491 }
492+ argTypes . Add ( argType ) ;
493+ pyArgType . Dispose ( ) ;
494+ }
494495
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 ;
500501
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 ( ) ) ;
505506
506- ILGenerator il = methodBuilder . GetILGenerator ( ) ;
507+ ILGenerator il = methodBuilder . GetILGenerator ( ) ;
507508
508- il . DeclareLocal ( typeof ( object [ ] ) ) ;
509- il . DeclareLocal ( typeof ( RuntimeMethodHandle ) ) ;
509+ il . DeclareLocal ( typeof ( object [ ] ) ) ;
510+ il . DeclareLocal ( typeof ( RuntimeMethodHandle ) ) ;
510511
511- // this
512- il . Emit ( OpCodes . Ldarg_0 ) ;
512+ // this
513+ il . Emit ( OpCodes . Ldarg_0 ) ;
513514
514- // Python method to call
515- il . Emit ( OpCodes . Ldstr , methodName ) ;
515+ // Python method to call
516+ il . Emit ( OpCodes . Ldstr , methodName ) ;
516517
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
519520
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 ) ;
524525
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 )
527534 {
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 ) ;
542541 }
542+ il . Emit ( OpCodes . Stelem , typeof ( object ) ) ;
543+ }
543544
544- // args array
545- il . Emit ( OpCodes . Ldloc_0 ) ;
545+ // args array
546+ il . Emit ( OpCodes . Ldloc_0 ) ;
546547
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 ) ;
551552#pragma warning disable CS0618 // PythonDerivedType is for internal use only
552553
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+ }
563564
564- CodeGenerator . GenerateMarshalByRefsBack ( il , argTypes ) ;
565+ CodeGenerator . GenerateMarshalByRefsBack ( il , argTypes ) ;
565566
566567#pragma warning restore CS0618 // PythonDerivedType is for internal use only
567- il . Emit ( OpCodes . Ret ) ;
568- }
568+ il . Emit ( OpCodes . Ret ) ;
569569 }
570570
571571 /// <summary>
@@ -584,68 +584,62 @@ private static void AddPythonProperty(string propertyName, PyObject func, TypeBu
584584 MethodAttributes . HideBySig |
585585 MethodAttributes . SpecialName ;
586586
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 )
588590 {
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+ }
594593
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 ) ;
599598
600- if ( func . HasAttr ( "fget" ) )
599+ if ( func . HasAttr ( "fget" ) )
600+ {
601+ using var pyfget = func . GetAttr ( "fget" ) ;
602+ if ( pyfget . IsTrue ( ) )
601603 {
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 ) ;
614612#pragma warning disable CS0618 // 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 ) ) ;
617615#pragma warning restore CS0618 // PythonDerivedType is for internal use only
618- il . Emit ( OpCodes . Ret ) ;
616+ il . Emit ( OpCodes . Ret ) ;
619617
620- propertyBuilder . SetGetMethod ( methodBuilder ) ;
621- }
622- }
618+ propertyBuilder . SetGetMethod ( methodBuilder ) ;
623619 }
620+ }
624621
625- if ( func . HasAttr ( "fset" ) )
622+ if ( func . HasAttr ( "fset" ) )
623+ {
624+ using var pyset = func . GetAttr ( "fset" ) ;
625+ if ( pyset . IsTrue ( ) )
626626 {
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 ) ;
640636#pragma warning disable CS0618 // 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 ) ) ;
643639#pragma warning restore CS0618 // PythonDerivedType is for internal use only
644- il . Emit ( OpCodes . Ret ) ;
640+ il . Emit ( OpCodes . Ret ) ;
645641
646- propertyBuilder . SetSetMethod ( methodBuilder ) ;
647- }
648- }
642+ propertyBuilder . SetSetMethod ( methodBuilder ) ;
649643 }
650644 }
651645 }
0 commit comments