@@ -86,9 +86,6 @@ internal static IntPtr GetPythonTypeByAlias(Type op)
8686 if ( op == int32Type )
8787 return Runtime . PyIntType ;
8888
89- if ( op == int64Type && Runtime . IsPython2 )
90- return Runtime . PyLongType ;
91-
9289 if ( op == int64Type )
9390 return Runtime . PyIntType ;
9491
@@ -488,63 +485,35 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
488485 return true ;
489486
490487 case TypeCode . Int32 :
491- // Trickery to support 64-bit platforms.
492- if ( Runtime . IsPython2 && Runtime . Is32Bit )
488+ // Python3 always use PyLong API
489+ op = Runtime . PyNumber_Long ( value ) ;
490+ if ( op == IntPtr . Zero )
493491 {
494- op = Runtime . PyNumber_Int ( value ) ;
495-
496- // As of Python 2.3, large ints magically convert :(
497- if ( Runtime . PyLong_Check ( op ) )
492+ Exceptions . Clear ( ) ;
493+ if ( Exceptions . ExceptionMatches ( overflow ) )
498494 {
499- Runtime . XDecref ( op ) ;
500495 goto overflow ;
501496 }
502-
503- if ( op == IntPtr . Zero )
504- {
505- if ( Exceptions . ExceptionMatches ( overflow ) )
506- {
507- goto overflow ;
508- }
509- goto type_error ;
510- }
511- ival = ( int ) Runtime . PyInt_AsLong ( op ) ;
512- Runtime . XDecref ( op ) ;
513- result = ival ;
514- return true ;
497+ goto type_error ;
515498 }
516- else // Python3 always use PyLong API
499+ long ll = ( long ) Runtime . PyLong_AsLongLong ( op ) ;
500+ Runtime . XDecref ( op ) ;
501+ if ( ll == - 1 && Exceptions . ErrorOccurred ( ) )
517502 {
518- op = Runtime . PyNumber_Long ( value ) ;
519- if ( op == IntPtr . Zero )
520- {
521- Exceptions . Clear ( ) ;
522- if ( Exceptions . ExceptionMatches ( overflow ) )
523- {
524- goto overflow ;
525- }
526- goto type_error ;
527- }
528- long ll = ( long ) Runtime . PyLong_AsLongLong ( op ) ;
529- Runtime . XDecref ( op ) ;
530- if ( ll == - 1 && Exceptions . ErrorOccurred ( ) )
531- {
532- goto overflow ;
533- }
534- if ( ll > Int32 . MaxValue || ll < Int32 . MinValue )
535- {
536- goto overflow ;
537- }
538- result = ( int ) ll ;
539- return true ;
503+ goto overflow ;
540504 }
505+ if ( ll > Int32 . MaxValue || ll < Int32 . MinValue )
506+ {
507+ goto overflow ;
508+ }
509+ result = ( int ) ll ;
510+ return true ;
541511
542512 case TypeCode . Boolean :
543513 result = Runtime . PyObject_IsTrue ( value ) != 0 ;
544514 return true ;
545515
546516 case TypeCode . Byte :
547- #if PYTHON3
548517 if ( Runtime . PyObject_TypeCheck ( value , Runtime . PyBytesType ) )
549518 {
550519 if ( Runtime . PyBytes_Size ( value ) == 1 )
@@ -555,18 +524,6 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
555524 }
556525 goto type_error ;
557526 }
558- #elif PYTHON2
559- if ( Runtime . PyObject_TypeCheck ( value , Runtime . PyStringType ) )
560- {
561- if ( Runtime . PyString_Size ( value ) == 1 )
562- {
563- op = Runtime . PyString_AsString ( value ) ;
564- result = ( byte ) Marshal . ReadByte ( op ) ;
565- return true ;
566- }
567- goto type_error ;
568- }
569- #endif
570527
571528 op = Runtime . PyNumber_Int ( value ) ;
572529 if ( op == IntPtr . Zero )
@@ -589,7 +546,6 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
589546 return true ;
590547
591548 case TypeCode . SByte :
592- #if PYTHON3
593549 if ( Runtime . PyObject_TypeCheck ( value , Runtime . PyBytesType ) )
594550 {
595551 if ( Runtime . PyBytes_Size ( value ) == 1 )
@@ -600,18 +556,6 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
600556 }
601557 goto type_error ;
602558 }
603- #elif PYTHON2
604- if ( Runtime . PyObject_TypeCheck ( value , Runtime . PyStringType ) )
605- {
606- if ( Runtime . PyString_Size ( value ) == 1 )
607- {
608- op = Runtime . PyString_AsString ( value ) ;
609- result = ( sbyte ) Marshal . ReadByte ( op ) ;
610- return true ;
611- }
612- goto type_error ;
613- }
614- #endif
615559
616560 op = Runtime . PyNumber_Int ( value ) ;
617561 if ( op == IntPtr . Zero )
@@ -634,7 +578,6 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
634578 return true ;
635579
636580 case TypeCode . Char :
637- #if PYTHON3
638581 if ( Runtime . PyObject_TypeCheck ( value , Runtime . PyBytesType ) )
639582 {
640583 if ( Runtime . PyBytes_Size ( value ) == 1 )
@@ -645,18 +588,6 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
645588 }
646589 goto type_error ;
647590 }
648- #elif PYTHON2
649- if ( Runtime . PyObject_TypeCheck ( value , Runtime . PyStringType ) )
650- {
651- if ( Runtime . PyString_Size ( value ) == 1 )
652- {
653- op = Runtime . PyString_AsString ( value ) ;
654- result = ( char ) Marshal . ReadByte ( op ) ;
655- return true ;
656- }
657- goto type_error ;
658- }
659- #endif
660591 else if ( Runtime . PyObject_TypeCheck ( value , Runtime . PyUnicodeType ) )
661592 {
662593 if ( Runtime . PyUnicode_GetSize ( value ) == 1 )
@@ -753,20 +684,20 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
753684 }
754685 goto type_error ;
755686 }
756-
687+
757688 uint ui ;
758- try
689+ try
759690 {
760691 ui = Convert . ToUInt32 ( Runtime . PyLong_AsUnsignedLong ( op ) ) ;
761692 } catch ( OverflowException )
762693 {
763694 // Probably wasn't an overflow in python but was in C# (e.g. if cpython
764- // longs are 64 bit then 0xFFFFFFFF + 1 will not overflow in
695+ // longs are 64 bit then 0xFFFFFFFF + 1 will not overflow in
765696 // PyLong_AsUnsignedLong)
766697 Runtime . XDecref ( op ) ;
767698 goto overflow ;
768699 }
769-
700+
770701
771702 if ( Exceptions . ErrorOccurred ( ) )
772703 {
@@ -900,7 +831,7 @@ private static bool ToArray(IntPtr value, Type obType, out object result, bool s
900831
901832 var listType = typeof ( List < > ) ;
902833 var constructedListType = listType . MakeGenericType ( elementType ) ;
903- IList list = IsSeqObj ? ( IList ) Activator . CreateInstance ( constructedListType , new Object [ ] { ( int ) len } ) :
834+ IList list = IsSeqObj ? ( IList ) Activator . CreateInstance ( constructedListType , new Object [ ] { ( int ) len } ) :
904835 ( IList ) Activator . CreateInstance ( constructedListType ) ;
905836 IntPtr item ;
906837
@@ -921,7 +852,7 @@ private static bool ToArray(IntPtr value, Type obType, out object result, bool s
921852
922853 items = Array . CreateInstance ( elementType , list . Count ) ;
923854 list . CopyTo ( items , 0 ) ;
924-
855+
925856 result = items ;
926857 return true ;
927858 }
0 commit comments