@@ -41,7 +41,7 @@ public static object __new__(CodeContext context, PythonType cls, string s, int
41
41
public static object __new__ ( CodeContext /*!*/ context , PythonType cls , IList < byte > s ) {
42
42
return __new__ ( context , cls , s , 10 ) ;
43
43
}
44
-
44
+
45
45
[ StaticExtensionMethod ]
46
46
public static object __new__ ( CodeContext /*!*/ context , PythonType cls , IList < byte > s , int redix ) {
47
47
object value ;
@@ -86,7 +86,7 @@ public static object __new__(CodeContext context, PythonType cls, object x) {
86
86
if ( x is double ) return ReturnObject ( context , cls , DoubleOps . __long__ ( ( double ) x ) ) ;
87
87
if ( x is int ) return ReturnObject ( context , cls , ( BigInteger ) ( int ) x ) ;
88
88
if ( x is BigInteger ) return ReturnObject ( context , cls , x ) ;
89
-
89
+
90
90
if ( x is Complex ) throw PythonOps . TypeError ( "can't convert complex to long; use long(abs(z))" ) ;
91
91
92
92
if ( x is decimal ) {
@@ -331,7 +331,7 @@ public static double TrueDivide([NotNull]BigInteger x, [NotNull]BigInteger y) {
331
331
// otherwise give the user the truncated result if the result fits in a float
332
332
BigInteger rem ;
333
333
BigInteger res = BigInteger . DivRem ( x , y , out rem ) ;
334
- if ( res . TryToFloat64 ( out fRes ) ) {
334
+ if ( res . TryToFloat64 ( out fRes ) ) {
335
335
if ( rem != BigInteger . Zero ) {
336
336
// try and figure out the fractional portion
337
337
BigInteger fraction = y / rem ;
@@ -343,7 +343,7 @@ public static double TrueDivide([NotNull]BigInteger x, [NotNull]BigInteger y) {
343
343
}
344
344
345
345
return fRes ;
346
- }
346
+ }
347
347
348
348
// otherwise report an error
349
349
throw PythonOps . OverflowError ( "long/long too large for a float" ) ;
@@ -444,7 +444,7 @@ public static string __oct__(BigInteger x) {
444
444
}
445
445
446
446
public static string __hex__ ( BigInteger x ) {
447
- // CPython 2.5 prints letters in lowercase, with a capital L.
447
+ // CPython 2.5 prints letters in lowercase, with a capital L.
448
448
if ( x < 0 ) {
449
449
return "-0x" + ( - x ) . ToString ( 16 ) . ToLower ( ) + "L" ;
450
450
} else {
@@ -540,7 +540,7 @@ public static int Compare(BigInteger x, BigInteger y) {
540
540
[ SpecialName ]
541
541
public static int Compare ( BigInteger x , int y ) {
542
542
int ix ;
543
- if ( x . AsInt32 ( out ix ) ) {
543
+ if ( x . AsInt32 ( out ix ) ) {
544
544
return ix == y ? 0 : ix > y ? 1 : - 1 ;
545
545
}
546
546
@@ -568,7 +568,7 @@ public static int Compare(BigInteger x, [NotNull]Extensible<double> y) {
568
568
}
569
569
570
570
[ SpecialName ]
571
- public static int Compare ( BigInteger x , decimal y ) {
571
+ public static int Compare ( BigInteger x , decimal y ) {
572
572
return DecimalOps . __cmp__ ( x , y ) ;
573
573
}
574
574
@@ -604,7 +604,7 @@ public static int __hash__(BigInteger self) {
604
604
}
605
605
606
606
// Call the DLR's BigInteger hash function, which will return an int32 representation of
607
- // b if b is within the int32 range. We use that as an optimization for hashing, and
607
+ // b if b is within the int32 range. We use that as an optimization for hashing, and
608
608
// assert the assumption below.
609
609
int hash = self . GetHashCode ( ) ;
610
610
#if DEBUG
@@ -637,7 +637,7 @@ public static float ToFloat(BigInteger/*!*/ self) {
637
637
}
638
638
639
639
#region Binary Ops
640
-
640
+
641
641
[ PythonHidden ]
642
642
public static BigInteger Xor ( BigInteger x , BigInteger y ) {
643
643
return x ^ y ;
@@ -706,7 +706,7 @@ public static bool AsUInt32(BigInteger self, out uint res) {
706
706
public static bool AsUInt64 ( BigInteger self , out ulong res ) {
707
707
return self . AsUInt64 ( out res ) ;
708
708
}
709
-
709
+
710
710
#endregion
711
711
712
712
#region Direct Conversions
@@ -907,18 +907,17 @@ public static int GetWordCount(BigInteger self) {
907
907
public static string /*!*/ __format__ ( CodeContext /*!*/ context , BigInteger /*!*/ self , [ NotNull ] string /*!*/ formatSpec ) {
908
908
StringFormatSpec spec = StringFormatSpec . FromString ( formatSpec ) ;
909
909
910
- if ( spec . Precision != null ) {
911
- throw PythonOps . ValueError ( "Precision not allowed in integer format specifier" ) ;
912
- }
913
-
914
910
BigInteger val = self ;
915
911
if ( self < 0 ) {
916
912
val = - self ;
917
913
}
918
914
string digits ;
919
-
915
+
920
916
switch ( spec . Type ) {
921
917
case 'n' :
918
+ if ( spec . Precision != null ) {
919
+ throw PythonOps . ValueError ( "Precision not allowed in integer format specifier" ) ;
920
+ }
922
921
CultureInfo culture = context . LanguageContext . NumericCulture ;
923
922
924
923
if ( culture == CultureInfo . InvariantCulture ) {
@@ -931,6 +930,9 @@ public static int GetWordCount(BigInteger self) {
931
930
break ;
932
931
case null :
933
932
case 'd' :
933
+ if ( spec . Precision != null ) {
934
+ throw PythonOps . ValueError ( "Precision not allowed in integer format specifier" ) ;
935
+ }
934
936
if ( spec . ThousandsComma ) {
935
937
var width = spec . Width ?? 0 ;
936
938
// If we're inserting commas, and we're padding with leading zeros.
@@ -995,22 +997,41 @@ public static int GetWordCount(BigInteger self) {
995
997
}
996
998
break ;
997
999
case 'X' :
1000
+ if ( spec . Precision != null ) {
1001
+ throw PythonOps . ValueError ( "Precision not allowed in integer format specifier" ) ;
1002
+ }
998
1003
digits = AbsToHex ( val , false ) ;
999
1004
break ;
1000
1005
case 'x' :
1006
+ if ( spec . Precision != null ) {
1007
+ throw PythonOps . ValueError ( "Precision not allowed in integer format specifier" ) ;
1008
+ }
1001
1009
digits = AbsToHex ( val , true ) ;
1002
1010
break ;
1003
1011
case 'o' : // octal
1012
+ if ( spec . Precision != null ) {
1013
+ throw PythonOps . ValueError ( "Precision not allowed in integer format specifier" ) ;
1014
+ }
1015
+ if ( spec . Precision != null ) {
1016
+ throw PythonOps . ValueError ( "Precision not allowed in integer format specifier" ) ;
1017
+ }
1004
1018
digits = ToOctal ( val , true ) ;
1005
1019
break ;
1006
1020
case 'b' : // binary
1021
+ if ( spec . Precision != null ) {
1022
+ throw PythonOps . ValueError ( "Precision not allowed in integer format specifier" ) ;
1023
+ }
1007
1024
digits = ToBinary ( val , false , true ) ;
1008
1025
break ;
1009
1026
case 'c' : // single char
1010
- int iVal ;
1027
+ if ( spec . Precision != null ) {
1028
+ throw PythonOps . ValueError ( "Precision not allowed in integer format specifier" ) ;
1029
+ }
1011
1030
if ( spec . Sign != null ) {
1012
1031
throw PythonOps . ValueError ( "Sign not allowed with integer format specifier 'c'" ) ;
1013
- } else if ( ! self . AsInt32 ( out iVal ) ) {
1032
+ }
1033
+ int iVal ;
1034
+ if ( ! self . AsInt32 ( out iVal ) ) {
1014
1035
throw PythonOps . OverflowError ( "long int too large to convert to int" ) ;
1015
1036
} else if ( iVal < 0 || iVal > 0xFF ) {
1016
1037
throw PythonOps . OverflowError ( "%c arg not in range(0x10000)" ) ;
@@ -1035,7 +1056,7 @@ private static string ToOctal(BigInteger val, bool lowercase) {
1035
1056
return ToDigits ( val , 8 , lowercase ) ;
1036
1057
}
1037
1058
1038
- internal static string ToBinary ( BigInteger val ) {
1059
+ internal static string ToBinary ( BigInteger val ) {
1039
1060
string res = ToBinary ( val . Abs ( ) , true , true ) ;
1040
1061
if ( val . IsNegative ( ) ) {
1041
1062
res = "-" + res ;
@@ -1047,7 +1068,7 @@ private static string ToBinary(BigInteger val, bool includeType, bool lowercase)
1047
1068
Debug . Assert ( ! val . IsNegative ( ) ) ;
1048
1069
1049
1070
string digits = ToDigits ( val , 2 , lowercase ) ;
1050
-
1071
+
1051
1072
if ( includeType ) {
1052
1073
digits = ( lowercase ? "0b" : "0B" ) + digits ;
1053
1074
}
@@ -1062,7 +1083,7 @@ private static string ToBinary(BigInteger val, bool includeType, bool lowercase)
1062
1083
1063
1084
StringBuilder tmp = new StringBuilder ( ) ;
1064
1085
tmp . Append ( digits [ 0 ] ) ;
1065
-
1086
+
1066
1087
for ( int i = 1 ; i < maxPrecision && i < digits . Length ; i ++ ) {
1067
1088
// append if we have a significant digit or if we are forcing a minimum precision
1068
1089
if ( digits [ i ] != '0' || i <= minPrecision ) {
@@ -1129,7 +1150,7 @@ private static string ToBinary(BigInteger val, bool includeType, bool lowercase)
1129
1150
for ( int i = str . Length - 1 ; i >= 0 ; i -- ) {
1130
1151
res . Append ( str [ i ] ) ;
1131
1152
}
1132
-
1153
+
1133
1154
return res . ToString ( ) ;
1134
1155
}
1135
1156
}
0 commit comments