Skip to content

Commit d834e17

Browse files
mickpMick Phillips
andauthored
Port IronLanguages/ironpython3#1643 to ironpython2 for #838. (#839)
Co-authored-by: Mick Phillips <mick.phillips@berkeleylights.com>
1 parent 31116c9 commit d834e17

File tree

3 files changed

+105
-72
lines changed

3 files changed

+105
-72
lines changed

Src/IronPython/Runtime/Operations/IntOps.cs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public static object __new__(CodeContext context, PythonType cls) {
232232
}
233233

234234
#region Binary Operators
235-
235+
236236
[SpecialName]
237237
public static object FloorDivide(int x, int y) {
238238
if (y == -1 && x == Int32.MinValue) {
@@ -414,15 +414,14 @@ public static object __coerce__(CodeContext context, int x, object o) {
414414
public static string __format__(CodeContext/*!*/ context, int self, [NotNull]string/*!*/ formatSpec) {
415415
StringFormatSpec spec = StringFormatSpec.FromString(formatSpec);
416416

417-
if (spec.Precision != null) {
418-
throw PythonOps.ValueError("Precision not allowed in integer format specifier");
419-
}
420-
421417
string digits;
422418
int width = 0;
423419

424420
switch (spec.Type) {
425421
case 'n':
422+
if (spec.Precision != null) {
423+
throw PythonOps.ValueError("Precision not allowed in integer format specifier");
424+
}
426425
CultureInfo culture = context.LanguageContext.NumericCulture;
427426

428427
if (culture == CultureInfo.InvariantCulture) {
@@ -444,6 +443,9 @@ public static string __format__(CodeContext/*!*/ context, int self, [NotNull]str
444443
break;
445444
case null:
446445
case 'd':
446+
if (spec.Precision != null) {
447+
throw PythonOps.ValueError("Precision not allowed in integer format specifier");
448+
}
447449
if (spec.ThousandsComma) {
448450
width = spec.Width ?? 0;
449451

@@ -511,22 +513,36 @@ public static string __format__(CodeContext/*!*/ context, int self, [NotNull]str
511513
}
512514
break;
513515
case 'X':
516+
if (spec.Precision != null) {
517+
throw PythonOps.ValueError("Precision not allowed in integer format specifier");
518+
}
514519
digits = ToHex(self, false);
515520
break;
516521
case 'x':
522+
if (spec.Precision != null) {
523+
throw PythonOps.ValueError("Precision not allowed in integer format specifier");
524+
}
517525
digits = ToHex(self, true);
518526
break;
519527
case 'o': // octal
528+
if (spec.Precision != null) {
529+
throw PythonOps.ValueError("Precision not allowed in integer format specifier");
530+
}
520531
digits = ToOctal(self, true);
521532
break;
522533
case 'b': // binary
534+
if (spec.Precision != null) {
535+
throw PythonOps.ValueError("Precision not allowed in integer format specifier");
536+
}
523537
digits = ToBinary(self, false);
524538
break;
525539
case 'c': // single char
540+
if (spec.Precision != null) {
541+
throw PythonOps.ValueError("Precision not allowed in integer format specifier");
542+
}
526543
if (spec.Sign != null) {
527544
throw PythonOps.ValueError("Sign not allowed with integer format specifier 'c'");
528545
}
529-
530546
if (self < 0 || self > 0xFF) {
531547
throw PythonOps.OverflowError("%c arg not in range(0x10000)");
532548
}
@@ -588,7 +604,7 @@ internal static string ToBinary(int self) {
588604
if (self == Int32.MinValue) {
589605
return "-0b10000000000000000000000000000000";
590606
}
591-
607+
592608
string res = ToBinary(self, true);
593609
if (self < 0) {
594610
res = "-" + res;
@@ -619,7 +635,7 @@ private static string ToBinary(int self, bool includeType) {
619635
} else {
620636
digits = "10000000000000000000000000000000";
621637
}
622-
638+
623639
if (includeType) {
624640
digits = "0b" + digits;
625641
}

Src/IronPython/Runtime/Operations/LongOps.cs

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static object __new__(CodeContext context, PythonType cls, string s, int
4141
public static object __new__(CodeContext/*!*/ context, PythonType cls, IList<byte> s) {
4242
return __new__(context, cls, s, 10);
4343
}
44-
44+
4545
[StaticExtensionMethod]
4646
public static object __new__(CodeContext/*!*/ context, PythonType cls, IList<byte> s, int redix) {
4747
object value;
@@ -86,7 +86,7 @@ public static object __new__(CodeContext context, PythonType cls, object x) {
8686
if (x is double) return ReturnObject(context, cls, DoubleOps.__long__((double)x));
8787
if (x is int) return ReturnObject(context, cls, (BigInteger)(int)x);
8888
if (x is BigInteger) return ReturnObject(context, cls, x);
89-
89+
9090
if (x is Complex) throw PythonOps.TypeError("can't convert complex to long; use long(abs(z))");
9191

9292
if (x is decimal) {
@@ -331,7 +331,7 @@ public static double TrueDivide([NotNull]BigInteger x, [NotNull]BigInteger y) {
331331
// otherwise give the user the truncated result if the result fits in a float
332332
BigInteger rem;
333333
BigInteger res = BigInteger.DivRem(x, y, out rem);
334-
if (res.TryToFloat64(out fRes)) {
334+
if (res.TryToFloat64(out fRes)) {
335335
if(rem != BigInteger.Zero) {
336336
// try and figure out the fractional portion
337337
BigInteger fraction = y / rem;
@@ -343,7 +343,7 @@ public static double TrueDivide([NotNull]BigInteger x, [NotNull]BigInteger y) {
343343
}
344344

345345
return fRes;
346-
}
346+
}
347347

348348
// otherwise report an error
349349
throw PythonOps.OverflowError("long/long too large for a float");
@@ -444,7 +444,7 @@ public static string __oct__(BigInteger x) {
444444
}
445445

446446
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.
448448
if (x < 0) {
449449
return "-0x" + (-x).ToString(16).ToLower() + "L";
450450
} else {
@@ -540,7 +540,7 @@ public static int Compare(BigInteger x, BigInteger y) {
540540
[SpecialName]
541541
public static int Compare(BigInteger x, int y) {
542542
int ix;
543-
if (x.AsInt32(out ix)) {
543+
if (x.AsInt32(out ix)) {
544544
return ix == y ? 0 : ix > y ? 1 : -1;
545545
}
546546

@@ -568,7 +568,7 @@ public static int Compare(BigInteger x, [NotNull]Extensible<double> y) {
568568
}
569569

570570
[SpecialName]
571-
public static int Compare(BigInteger x, decimal y) {
571+
public static int Compare(BigInteger x, decimal y) {
572572
return DecimalOps.__cmp__(x, y);
573573
}
574574

@@ -604,7 +604,7 @@ public static int __hash__(BigInteger self) {
604604
}
605605

606606
// 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
608608
// assert the assumption below.
609609
int hash = self.GetHashCode();
610610
#if DEBUG
@@ -637,7 +637,7 @@ public static float ToFloat(BigInteger/*!*/ self) {
637637
}
638638

639639
#region Binary Ops
640-
640+
641641
[PythonHidden]
642642
public static BigInteger Xor(BigInteger x, BigInteger y) {
643643
return x ^ y;
@@ -706,7 +706,7 @@ public static bool AsUInt32(BigInteger self, out uint res) {
706706
public static bool AsUInt64(BigInteger self, out ulong res) {
707707
return self.AsUInt64(out res);
708708
}
709-
709+
710710
#endregion
711711

712712
#region Direct Conversions
@@ -907,18 +907,17 @@ public static int GetWordCount(BigInteger self) {
907907
public static string/*!*/ __format__(CodeContext/*!*/ context, BigInteger/*!*/ self, [NotNull]string/*!*/ formatSpec) {
908908
StringFormatSpec spec = StringFormatSpec.FromString(formatSpec);
909909

910-
if (spec.Precision != null) {
911-
throw PythonOps.ValueError("Precision not allowed in integer format specifier");
912-
}
913-
914910
BigInteger val = self;
915911
if (self < 0) {
916912
val = -self;
917913
}
918914
string digits;
919-
915+
920916
switch (spec.Type) {
921917
case 'n':
918+
if (spec.Precision != null) {
919+
throw PythonOps.ValueError("Precision not allowed in integer format specifier");
920+
}
922921
CultureInfo culture = context.LanguageContext.NumericCulture;
923922

924923
if (culture == CultureInfo.InvariantCulture) {
@@ -931,6 +930,9 @@ public static int GetWordCount(BigInteger self) {
931930
break;
932931
case null:
933932
case 'd':
933+
if (spec.Precision != null) {
934+
throw PythonOps.ValueError("Precision not allowed in integer format specifier");
935+
}
934936
if (spec.ThousandsComma) {
935937
var width = spec.Width ?? 0;
936938
// If we're inserting commas, and we're padding with leading zeros.
@@ -995,22 +997,41 @@ public static int GetWordCount(BigInteger self) {
995997
}
996998
break;
997999
case 'X':
1000+
if (spec.Precision != null) {
1001+
throw PythonOps.ValueError("Precision not allowed in integer format specifier");
1002+
}
9981003
digits = AbsToHex(val, false);
9991004
break;
10001005
case 'x':
1006+
if (spec.Precision != null) {
1007+
throw PythonOps.ValueError("Precision not allowed in integer format specifier");
1008+
}
10011009
digits = AbsToHex(val, true);
10021010
break;
10031011
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+
}
10041018
digits = ToOctal(val, true);
10051019
break;
10061020
case 'b': // binary
1021+
if (spec.Precision != null) {
1022+
throw PythonOps.ValueError("Precision not allowed in integer format specifier");
1023+
}
10071024
digits = ToBinary(val, false, true);
10081025
break;
10091026
case 'c': // single char
1010-
int iVal;
1027+
if (spec.Precision != null) {
1028+
throw PythonOps.ValueError("Precision not allowed in integer format specifier");
1029+
}
10111030
if (spec.Sign != null) {
10121031
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)) {
10141035
throw PythonOps.OverflowError("long int too large to convert to int");
10151036
} else if(iVal < 0 || iVal > 0xFF) {
10161037
throw PythonOps.OverflowError("%c arg not in range(0x10000)");
@@ -1035,7 +1056,7 @@ private static string ToOctal(BigInteger val, bool lowercase) {
10351056
return ToDigits(val, 8, lowercase);
10361057
}
10371058

1038-
internal static string ToBinary(BigInteger val) {
1059+
internal static string ToBinary(BigInteger val) {
10391060
string res = ToBinary(val.Abs(), true, true);
10401061
if (val.IsNegative()) {
10411062
res = "-" + res;
@@ -1047,7 +1068,7 @@ private static string ToBinary(BigInteger val, bool includeType, bool lowercase)
10471068
Debug.Assert(!val.IsNegative());
10481069

10491070
string digits = ToDigits(val, 2, lowercase);
1050-
1071+
10511072
if (includeType) {
10521073
digits = (lowercase ? "0b" : "0B") + digits;
10531074
}
@@ -1062,7 +1083,7 @@ private static string ToBinary(BigInteger val, bool includeType, bool lowercase)
10621083

10631084
StringBuilder tmp = new StringBuilder();
10641085
tmp.Append(digits[0]);
1065-
1086+
10661087
for (int i = 1; i < maxPrecision && i < digits.Length; i++) {
10671088
// append if we have a significant digit or if we are forcing a minimum precision
10681089
if (digits[i] != '0' || i <= minPrecision) {
@@ -1129,7 +1150,7 @@ private static string ToBinary(BigInteger val, bool includeType, bool lowercase)
11291150
for (int i = str.Length - 1; i >= 0; i--) {
11301151
res.Append(str[i]);
11311152
}
1132-
1153+
11331154
return res.ToString();
11341155
}
11351156
}

0 commit comments

Comments
 (0)