Skip to content

Commit 82f1816

Browse files
authored
Resolve intermittent failure in test_long (#984)
1 parent 13ff1d6 commit 82f1816

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

Src/IronPython/Modules/Builtin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ public static string hex(object? o) {
643643

644644
static string ToHexString(BigInteger b) {
645645
var val = b.ToString("x");
646-
if (val[0] == '0') val = val.TrimStart('0'); // BigInteger.ToString("x") sometimes pads with 0 so we need to trim it (https://github.com/dotnet/runtime/issues/43269)
646+
if (val[0] == '0') val = val.TrimStart('0'); // positive values with the most significant bit set are padded with a 0
647647
return val;
648648
}
649649
}

Src/IronPython/Runtime/Operations/LongOps.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,11 +369,19 @@ public static BigInteger op_LeftShift(BigInteger x, int y) {
369369
return x << y;
370370
}
371371

372+
private static readonly bool hasShiftBug = BigInteger.Parse("-18446744073709543424") >> 32 == 0; // https://github.com/dotnet/runtime/issues/43396
373+
372374
[SpecialName]
373375
public static BigInteger op_RightShift(BigInteger x, int y) {
374376
if (y < 0) {
375377
throw PythonOps.ValueError("negative shift count");
376378
}
379+
if (hasShiftBug && x.IsNegative()) {
380+
if (y == 0) return x;
381+
if (y % 32 == 0) {
382+
return (x >> (y - 1)) >> 1;
383+
}
384+
}
377385
return x >> y;
378386
}
379387

Tests/test_ironmath.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,10 @@ def test_misc(self):
234234
self.assertEqual(BigInteger(1).CompareTo(None), 1)
235235
self.assertEqual(BigInteger(1).CompareTo(True), 0)
236236

237+
def test_rightshiftby32_negative_bug(self):
238+
# test workaround for https://github.com/dotnet/runtime/issues/43396
239+
from System.Numerics import BigInteger
240+
self.assertEqual(BigInteger.Parse("-18446744073709543424") >> 32, -4294967296)
241+
self.assertEqual(BigInteger.Parse("-79228162514264337593543917568") >> 32, -18446744073709551616)
242+
237243
run_test(__name__)

0 commit comments

Comments
 (0)