@@ -2816,55 +2816,58 @@ public static object ConvertToPythonPrimitive(object value) {
28162816 return value switch
28172817 {
28182818 float f => ( double ) f ,
2819- double d => d ,
28202819 sbyte sb => ( int ) sb ,
28212820 byte b => ( int ) b ,
28222821 char c => ( int ) c ,
28232822 short s => ( int ) s ,
28242823 ushort us => ( int ) us ,
2825- int i => i ,
28262824 uint ui => ( BigInteger ) ui ,
28272825 long l => ( BigInteger ) l ,
28282826 ulong ul => ( BigInteger ) ul ,
2829- BigInteger bi => bi ,
2830- bool b => b ,
2827+ // no conversion needed for: double, int, BigInteger, bool
28312828 _ => value ,
28322829 } ;
28332830 }
28342831
28352832 public static object ? ConvertFloatToComplex ( object value ) {
2836- if ( value == null ) {
2837- return null ;
2838- }
2839-
2840- if ( value is double d ) return new Complex ( d , 0.0 ) ;
2841- if ( value is Extensible < double > ed ) return new Complex ( ed . Value , 0.0 ) ;
2842- throw new InvalidOperationException ( ) ;
2833+ return value switch {
2834+ null => null ,
2835+ double d => new Complex ( d , 0.0 ) ,
2836+ Extensible < double > ed => new Complex ( ed . Value , 0.0 ) ,
2837+ _ => throw new InvalidOperationException ( ) ,
2838+ } ;
28432839 }
28442840
28452841 public static object ? ConvertIntToBigInt ( object ? value ) {
28462842 return value switch {
28472843 null => null ,
28482844 int i => new BigInteger ( i ) ,
2849- BigInteger bi => bi ,
2845+ BigInteger => value ,
2846+ Extensible < BigInteger > ebi => ebi . Value ,
28502847 _ => throw new InvalidOperationException ( ) ,
28512848 } ;
28522849 }
28532850
2854- internal static bool CheckingConvertToInt ( object value ) {
2855- return value is int || value is BigInteger || value is Extensible < BigInteger > ;
2851+ public static object ? ConvertIntToInt32 ( object ? value ) {
2852+ return value switch {
2853+ null => null ,
2854+ int => value ,
2855+ BigInteger bi => ( int ) bi ,
2856+ Extensible < BigInteger > ebi => ( int ) ebi . Value ,
2857+ _ => throw new InvalidOperationException ( ) ,
2858+ } ;
28562859 }
28572860
2858- internal static bool CheckingConvertToLong ( object value ) {
2859- return CheckingConvertToInt ( value ) ;
2861+ internal static bool CheckingConvertToInt ( object value ) {
2862+ return value is int || value is BigInteger || value is Extensible < BigInteger > ;
28602863 }
28612864
28622865 internal static bool CheckingConvertToFloat ( object value ) {
2863- return value is double || ( value != null && value is Extensible < double > ) ;
2866+ return value is double || value is Extensible < double > ;
28642867 }
28652868
28662869 internal static bool CheckingConvertToComplex ( object value ) {
2867- return value is Complex || value is Extensible < Complex > || CheckingConvertToInt ( value ) || CheckingConvertToFloat ( value ) ;
2870+ return value is Complex || value is Extensible < Complex > ;
28682871 }
28692872
28702873 internal static bool CheckingConvertToString ( object value ) {
@@ -2880,11 +2883,6 @@ public static bool CheckingConvertToBool(object value) {
28802883 return value ;
28812884 }
28822885
2883- public static object ? NonThrowingConvertToLong ( object value ) {
2884- if ( ! CheckingConvertToInt ( value ) ) return null ;
2885- return value ;
2886- }
2887-
28882886 public static object ? NonThrowingConvertToFloat ( object value ) {
28892887 if ( ! CheckingConvertToFloat ( value ) ) return null ;
28902888 return value ;
@@ -2920,11 +2918,6 @@ public static object ThrowingConvertToComplex(object value) {
29202918 return value ;
29212919 }
29222920
2923- public static object ThrowingConvertToLong ( object value ) {
2924- if ( ! CheckingConvertToLong ( value ) ) throw TypeError ( " __int__ returned non-int (type {0})" , PythonOps . GetPythonTypeName ( value ) ) ;
2925- return value ;
2926- }
2927-
29282921 public static object ThrowingConvertToString ( object value ) {
29292922 if ( ! CheckingConvertToString ( value ) ) throw TypeError ( " __str__ returned non-str (type {0})" , PythonOps . GetPythonTypeName ( value ) ) ;
29302923 return value ;
0 commit comments