@@ -371,43 +371,57 @@ static object[] TryConvertArguments(ParameterInfo[] pi, bool paramsArray,
371371 continue ;
372372 }
373373
374+ var parameter = pi [ paramIndex ] ;
374375 IntPtr op = ( arrayStart == paramIndex )
375376 // map remaining Python arguments to a tuple since
376377 // the managed function accepts it - hopefully :]
377378 ? Runtime . PyTuple_GetSlice ( args , arrayStart , pyArgCount )
378379 : Runtime . PyTuple_GetItem ( args , paramIndex ) ;
379380
380- var parameter = pi [ paramIndex ] ;
381-
382- var clrtype = TryComputeClrArgumentType ( parameter . ParameterType , op , needsResolution : needsResolution ) ;
383- if ( clrtype == null )
381+ bool isOut ;
382+ if ( ! TryConvertArgument ( op , parameter . ParameterType , needsResolution , out margs [ paramIndex ] , out isOut ) )
384383 {
385384 return null ;
386385 }
387386
388- if ( parameter . IsOut || clrtype . IsByRef )
389- {
390- outs ++ ;
391- }
392-
393- object arg ;
394- if ( ! Converter . ToManaged ( op , clrtype , out arg , false ) )
395- {
396- Exceptions . Clear ( ) ;
397- return null ;
398- }
399387 if ( arrayStart == paramIndex )
400388 {
389+ // TODO: is this a bug? Should this happen even if the conversion fails?
401390 // GetSlice() creates a new reference but GetItem()
402391 // returns only a borrow reference.
403392 Runtime . XDecref ( op ) ;
404393 }
405- margs [ paramIndex ] = arg ;
394+
395+ if ( parameter . IsOut || isOut )
396+ {
397+ outs ++ ;
398+ }
406399 }
407400
408401 return margs ;
409402 }
410403
404+ static bool TryConvertArgument ( IntPtr op , Type parameterType , bool needsResolution ,
405+ out object arg , out bool isOut )
406+ {
407+ arg = null ;
408+ isOut = false ;
409+ var clrtype = TryComputeClrArgumentType ( parameterType , op , needsResolution : needsResolution ) ;
410+ if ( clrtype == null )
411+ {
412+ return false ;
413+ }
414+
415+ if ( ! Converter . ToManaged ( op , clrtype , out arg , false ) )
416+ {
417+ Exceptions . Clear ( ) ;
418+ return false ;
419+ }
420+
421+ isOut = clrtype . IsByRef ;
422+ return true ;
423+ }
424+
411425 static Type TryComputeClrArgumentType ( Type parameterType , IntPtr argument , bool needsResolution )
412426 {
413427 // this logic below handles cases when multiple overloading methods
0 commit comments