@@ -242,52 +242,24 @@ internal static int ArgPrecedence(Type t)
242
242
243
243
TypeCode tc = Type . GetTypeCode ( t ) ;
244
244
// TODO: Clean up
245
- switch ( tc )
245
+ return tc switch
246
246
{
247
- case TypeCode . Object :
248
- return 1 ;
249
-
250
- case TypeCode . UInt64 :
251
- return 10 ;
252
-
253
- case TypeCode . UInt32 :
254
- return 11 ;
255
-
256
- case TypeCode . UInt16 :
257
- return 12 ;
258
-
259
- case TypeCode . Int64 :
260
- return 13 ;
261
-
262
- case TypeCode . Int32 :
263
- return 14 ;
264
-
265
- case TypeCode . Int16 :
266
- return 15 ;
267
-
268
- case TypeCode . Char :
269
- return 16 ;
270
-
271
- case TypeCode . SByte :
272
- return 17 ;
273
-
274
- case TypeCode . Byte :
275
- return 18 ;
276
-
277
- case TypeCode . Single :
278
- return 20 ;
279
-
280
- case TypeCode . Double :
281
- return 21 ;
282
-
283
- case TypeCode . String :
284
- return 30 ;
285
-
286
- case TypeCode . Boolean :
287
- return 40 ;
288
- }
289
-
290
- return 2000 ;
247
+ TypeCode . Object => 1 ,
248
+ TypeCode . UInt64 => 10 ,
249
+ TypeCode . UInt32 => 11 ,
250
+ TypeCode . UInt16 => 12 ,
251
+ TypeCode . Int64 => 13 ,
252
+ TypeCode . Int32 => 14 ,
253
+ TypeCode . Int16 => 15 ,
254
+ TypeCode . Char => 16 ,
255
+ TypeCode . SByte => 17 ,
256
+ TypeCode . Byte => 18 ,
257
+ TypeCode . Single => 20 ,
258
+ TypeCode . Double => 21 ,
259
+ TypeCode . String => 30 ,
260
+ TypeCode . Boolean => 40 ,
261
+ _ => 2000 ,
262
+ } ;
291
263
}
292
264
293
265
/// <summary>
@@ -410,18 +382,14 @@ public MismatchedMethod(Exception exception, MethodBase mb)
410
382
isGeneric = true ;
411
383
}
412
384
ParameterInfo [ ] pi = mi . GetParameters ( ) ;
413
- ArrayList ? defaultArgList ;
414
- bool paramsArray ;
415
- int kwargsMatched ;
416
- int defaultsNeeded ;
417
385
bool isOperator = OperatorMethod . IsOperatorMethod ( mi ) ;
418
386
// Binary operator methods will have 2 CLR args but only one Python arg
419
387
// (unary operators will have 1 less each), since Python operator methods are bound.
420
388
isOperator = isOperator && pynargs == pi . Length - 1 ;
421
389
bool isReverse = isOperator && OperatorMethod . IsReverse ( ( MethodInfo ) mi ) ; // Only cast if isOperator.
422
390
if ( isReverse && OperatorMethod . IsComparisonOp ( ( MethodInfo ) mi ) )
423
391
continue ; // Comparison operators in Python have no reverse mode.
424
- if ( ! MatchesArgumentCount ( pynargs , pi , kwargDict , out paramsArray , out defaultArgList , out kwargsMatched , out defaultsNeeded ) && ! isOperator )
392
+ if ( ! MatchesArgumentCount ( pynargs , pi , kwargDict , out bool paramsArray , out ArrayList ? defaultArgList , out int kwargsMatched , out int defaultsNeeded ) && ! isOperator )
425
393
{
426
394
continue ;
427
395
}
@@ -436,8 +404,7 @@ public MismatchedMethod(Exception exception, MethodBase mb)
436
404
// We need to take the first CLR argument.
437
405
pi = pi . Take ( 1 ) . ToArray ( ) ;
438
406
}
439
- int outs ;
440
- var margs = TryConvertArguments ( pi , paramsArray , args , pynargs , kwargDict , defaultArgList , outs : out outs ) ;
407
+ var margs = TryConvertArguments ( pi , paramsArray , args , pynargs , kwargDict , defaultArgList , outs : out int outs ) ;
441
408
if ( margs == null )
442
409
{
443
410
var mismatchCause = PythonException . FetchCurrent ( ) ;
@@ -495,7 +462,7 @@ public MismatchedMethod(Exception exception, MethodBase mb)
495
462
{
496
463
// Best effort for determining method to match on gives multiple possible
497
464
// matches and we need at least one default argument - bail from this point
498
- StringBuilder stringBuilder = new StringBuilder ( "Not enough arguments provided to disambiguate the method. Found:" ) ;
465
+ var stringBuilder = new StringBuilder ( "Not enough arguments provided to disambiguate the method. Found:" ) ;
499
466
foreach ( var matchedMethod in argMatchedMethods )
500
467
{
501
468
stringBuilder . AppendLine ( ) ;
@@ -523,18 +490,20 @@ public MismatchedMethod(Exception exception, MethodBase mb)
523
490
//CLRObject co = (CLRObject)ManagedType.GetManagedObject(inst);
524
491
// InvalidCastException: Unable to cast object of type
525
492
// 'Python.Runtime.ClassObject' to type 'Python.Runtime.CLRObject'
526
- var co = ManagedType . GetManagedObject ( inst ) as CLRObject ;
527
493
528
494
// Sanity check: this ensures a graceful exit if someone does
529
495
// something intentionally wrong like call a non-static method
530
496
// on the class rather than on an instance of the class.
531
497
// XXX maybe better to do this before all the other rigmarole.
532
- if ( co == null )
498
+ if ( ManagedType . GetManagedObject ( inst ) is CLRObject co )
499
+ {
500
+ target = co . inst ;
501
+ }
502
+ else
533
503
{
534
504
Exceptions . SetError ( Exceptions . TypeError , "Invoked a non-static method with an invalid instance" ) ;
535
505
return null ;
536
506
}
537
- target = co . inst ;
538
507
}
539
508
540
509
return new Binding ( mi , target , margs , outs ) ;
@@ -623,7 +592,7 @@ static BorrowedReference HandleParamsArray(BorrowedReference args, int arrayStar
623
592
for ( int paramIndex = 0 ; paramIndex < pi . Length ; paramIndex ++ )
624
593
{
625
594
var parameter = pi [ paramIndex ] ;
626
- bool hasNamedParam = parameter . Name != null ? kwargDict . ContainsKey ( parameter . Name ) : false ;
595
+ bool hasNamedParam = parameter . Name != null && kwargDict . ContainsKey ( parameter . Name ) ;
627
596
628
597
if ( paramIndex >= pyArgCount && ! ( hasNamedParam || ( paramsArray && paramIndex == arrayStart ) ) )
629
598
{
@@ -658,8 +627,7 @@ static BorrowedReference HandleParamsArray(BorrowedReference args, int arrayStar
658
627
}
659
628
}
660
629
661
- bool isOut ;
662
- if ( ! TryConvertArgument ( op , parameter . ParameterType , out margs [ paramIndex ] , out isOut ) )
630
+ if ( ! TryConvertArgument ( op , parameter . ParameterType , out margs [ paramIndex ] , out bool isOut ) )
663
631
{
664
632
tempObject . Dispose ( ) ;
665
633
return null ;
@@ -789,7 +757,7 @@ static bool MatchesArgumentCount(int positionalArgumentCount, ParameterInfo[] pa
789
757
{
790
758
defaultArgList = null ;
791
759
var match = false ;
792
- paramsArray = parameters . Length > 0 ? Attribute . IsDefined ( parameters [ parameters . Length - 1 ] , typeof ( ParamArrayAttribute ) ) : false ;
760
+ paramsArray = parameters . Length > 0 && Attribute . IsDefined ( parameters [ parameters . Length - 1 ] , typeof ( ParamArrayAttribute ) ) ;
793
761
kwargsMatched = 0 ;
794
762
defaultsNeeded = 0 ;
795
763
if ( positionalArgumentCount == parameters . Length && kwargDict . Count == 0 )
0 commit comments