1- using System ;
1+ using System ;
22using System . Collections ;
33using System . Dynamic ;
44using System . Linq . Expressions ;
@@ -936,6 +936,34 @@ public override bool TrySetMember(SetMemberBinder binder, object value)
936936 return true ;
937937 }
938938
939+ private void GetArgs ( object [ ] inargs , CallInfo callInfo , out PyTuple args , out PyDict kwargs )
940+ {
941+ if ( callInfo == null || callInfo . ArgumentNames . Count == 0 )
942+ {
943+ GetArgs ( inargs , out args , out kwargs ) ;
944+ return ;
945+ }
946+
947+ // Support for .net named arguments
948+ var namedArgumentCount = callInfo . ArgumentNames . Count ;
949+ var regularArgumentCount = callInfo . ArgumentCount - namedArgumentCount ;
950+
951+ var argTuple = Runtime . PyTuple_New ( regularArgumentCount ) ;
952+ for ( int i = 0 ; i < regularArgumentCount ; ++ i )
953+ {
954+ AddArgument ( argTuple , i , inargs [ i ] ) ;
955+ }
956+ args = new PyTuple ( argTuple ) ;
957+
958+ var namedArgs = new object [ namedArgumentCount * 2 ] ;
959+ for ( int i = 0 ; i < namedArgumentCount ; ++ i )
960+ {
961+ namedArgs [ i * 2 ] = callInfo . ArgumentNames [ i ] ;
962+ namedArgs [ i * 2 + 1 ] = inargs [ regularArgumentCount + i ] ;
963+ }
964+ kwargs = Py . kw ( namedArgs ) ;
965+ }
966+
939967 private void GetArgs ( object [ ] inargs , out PyTuple args , out PyDict kwargs )
940968 {
941969 int arg_count ;
@@ -946,22 +974,10 @@ private void GetArgs(object[] inargs, out PyTuple args, out PyDict kwargs)
946974 IntPtr argtuple = Runtime . PyTuple_New ( arg_count ) ;
947975 for ( var i = 0 ; i < arg_count ; i ++ )
948976 {
949- IntPtr ptr ;
950- if ( inargs [ i ] is PyObject )
951- {
952- ptr = ( ( PyObject ) inargs [ i ] ) . Handle ;
953- Runtime . XIncref ( ptr ) ;
954- }
955- else
956- {
957- ptr = Converter . ToPython ( inargs [ i ] , inargs [ i ] ? . GetType ( ) ) ;
958- }
959- if ( Runtime . PyTuple_SetItem ( argtuple , i , ptr ) < 0 )
960- {
961- throw new PythonException ( ) ;
962- }
977+ AddArgument ( argtuple , i , inargs [ i ] ) ;
963978 }
964979 args = new PyTuple ( argtuple ) ;
980+
965981 kwargs = null ;
966982 for ( int i = arg_count ; i < inargs . Length ; i ++ )
967983 {
@@ -980,6 +996,32 @@ private void GetArgs(object[] inargs, out PyTuple args, out PyDict kwargs)
980996 }
981997 }
982998
999+ private static void AddArgument ( IntPtr argtuple , int i , object target )
1000+ {
1001+ IntPtr ptr = GetPythonObject ( target ) ;
1002+
1003+ if ( Runtime . PyTuple_SetItem ( argtuple , i , ptr ) < 0 )
1004+ {
1005+ throw new PythonException ( ) ;
1006+ }
1007+ }
1008+
1009+ private static IntPtr GetPythonObject ( object target )
1010+ {
1011+ IntPtr ptr ;
1012+ if ( target is PyObject )
1013+ {
1014+ ptr = ( ( PyObject ) target ) . Handle ;
1015+ Runtime . XIncref ( ptr ) ;
1016+ }
1017+ else
1018+ {
1019+ ptr = Converter . ToPython ( target , target ? . GetType ( ) ) ;
1020+ }
1021+
1022+ return ptr ;
1023+ }
1024+
9831025 public override bool TryInvokeMember ( InvokeMemberBinder binder , object [ ] args , out object result )
9841026 {
9851027 if ( this . HasAttr ( binder . Name ) && this . GetAttr ( binder . Name ) . IsCallable ( ) )
@@ -988,7 +1030,7 @@ public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, o
9881030 PyDict kwargs = null ;
9891031 try
9901032 {
991- GetArgs ( args , out pyargs , out kwargs ) ;
1033+ GetArgs ( args , binder . CallInfo , out pyargs , out kwargs ) ;
9921034 result = CheckNone ( InvokeMethod ( binder . Name , pyargs , kwargs ) ) ;
9931035 }
9941036 finally
@@ -1018,7 +1060,7 @@ public override bool TryInvoke(InvokeBinder binder, object[] args, out object re
10181060 PyDict kwargs = null ;
10191061 try
10201062 {
1021- GetArgs ( args , out pyargs , out kwargs ) ;
1063+ GetArgs ( args , binder . CallInfo , out pyargs , out kwargs ) ;
10221064 result = CheckNone ( Invoke ( pyargs , kwargs ) ) ;
10231065 }
10241066 finally
0 commit comments