1- using System ;
1+ using System ;
22using System . Collections ;
33using System . Dynamic ;
44using System . Linq . Expressions ;
@@ -915,6 +915,34 @@ public override bool TrySetMember(SetMemberBinder binder, object value)
915915 return true ;
916916 }
917917
918+ private void GetArgs ( object [ ] inargs , CallInfo callInfo , out PyTuple args , out PyDict kwargs )
919+ {
920+ if ( callInfo == null || callInfo . ArgumentNames . Count == 0 )
921+ {
922+ GetArgs ( inargs , out args , out kwargs ) ;
923+ return ;
924+ }
925+
926+ // Support for .net named arguments
927+ var namedArgumentCount = callInfo . ArgumentNames . Count ;
928+ var regularArgumentCount = callInfo . ArgumentCount - namedArgumentCount ;
929+
930+ var argTuple = Runtime . PyTuple_New ( regularArgumentCount ) ;
931+ for ( int i = 0 ; i < regularArgumentCount ; ++ i )
932+ {
933+ AddArgument ( argTuple , i , inargs [ i ] ) ;
934+ }
935+ args = new PyTuple ( argTuple ) ;
936+
937+ var namedArgs = new object [ namedArgumentCount * 2 ] ;
938+ for ( int i = 0 ; i < namedArgumentCount ; ++ i )
939+ {
940+ namedArgs [ i * 2 ] = callInfo . ArgumentNames [ i ] ;
941+ namedArgs [ i * 2 + 1 ] = inargs [ regularArgumentCount + i ] ;
942+ }
943+ kwargs = Py . kw ( namedArgs ) ;
944+ }
945+
918946 private void GetArgs ( object [ ] inargs , out PyTuple args , out PyDict kwargs )
919947 {
920948 int arg_count ;
@@ -925,22 +953,10 @@ private void GetArgs(object[] inargs, out PyTuple args, out PyDict kwargs)
925953 IntPtr argtuple = Runtime . PyTuple_New ( arg_count ) ;
926954 for ( var i = 0 ; i < arg_count ; i ++ )
927955 {
928- IntPtr ptr ;
929- if ( inargs [ i ] is PyObject )
930- {
931- ptr = ( ( PyObject ) inargs [ i ] ) . Handle ;
932- Runtime . XIncref ( ptr ) ;
933- }
934- else
935- {
936- ptr = Converter . ToPython ( inargs [ i ] , inargs [ i ] ? . GetType ( ) ) ;
937- }
938- if ( Runtime . PyTuple_SetItem ( argtuple , i , ptr ) < 0 )
939- {
940- throw new PythonException ( ) ;
941- }
956+ AddArgument ( argtuple , i , inargs [ i ] ) ;
942957 }
943958 args = new PyTuple ( argtuple ) ;
959+
944960 kwargs = null ;
945961 for ( int i = arg_count ; i < inargs . Length ; i ++ )
946962 {
@@ -959,6 +975,32 @@ private void GetArgs(object[] inargs, out PyTuple args, out PyDict kwargs)
959975 }
960976 }
961977
978+ private static void AddArgument ( IntPtr argtuple , int i , object target )
979+ {
980+ IntPtr ptr = GetPythonObject ( target ) ;
981+
982+ if ( Runtime . PyTuple_SetItem ( argtuple , i , ptr ) < 0 )
983+ {
984+ throw new PythonException ( ) ;
985+ }
986+ }
987+
988+ private static IntPtr GetPythonObject ( object target )
989+ {
990+ IntPtr ptr ;
991+ if ( target is PyObject )
992+ {
993+ ptr = ( ( PyObject ) target ) . Handle ;
994+ Runtime . XIncref ( ptr ) ;
995+ }
996+ else
997+ {
998+ ptr = Converter . ToPython ( target , target ? . GetType ( ) ) ;
999+ }
1000+
1001+ return ptr ;
1002+ }
1003+
9621004 public override bool TryInvokeMember ( InvokeMemberBinder binder , object [ ] args , out object result )
9631005 {
9641006 if ( this . HasAttr ( binder . Name ) && this . GetAttr ( binder . Name ) . IsCallable ( ) )
@@ -967,7 +1009,7 @@ public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, o
9671009 PyDict kwargs = null ;
9681010 try
9691011 {
970- GetArgs ( args , out pyargs , out kwargs ) ;
1012+ GetArgs ( args , binder . CallInfo , out pyargs , out kwargs ) ;
9711013 result = CheckNone ( InvokeMethod ( binder . Name , pyargs , kwargs ) ) ;
9721014 }
9731015 finally
@@ -997,7 +1039,7 @@ public override bool TryInvoke(InvokeBinder binder, object[] args, out object re
9971039 PyDict kwargs = null ;
9981040 try
9991041 {
1000- GetArgs ( args , out pyargs , out kwargs ) ;
1042+ GetArgs ( args , binder . CallInfo , out pyargs , out kwargs ) ;
10011043 result = CheckNone ( Invoke ( pyargs , kwargs ) ) ;
10021044 }
10031045 finally
0 commit comments