Skip to content

Commit 4e39884

Browse files
committed
[GR-23245] Pass a few test_long tests
PullRequest: graalpython/1103
2 parents ea04022 + 7bba7de commit 4e39884

File tree

11 files changed

+79
-47
lines changed

11 files changed

+79
-47
lines changed

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_long.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
*graalpython.lib-python.3.test.test_long.LongTest.test_access_to_nonexistent_digit_0
2+
*graalpython.lib-python.3.test.test_long.LongTest.test_as_integer_ratio
23
*graalpython.lib-python.3.test.test_long.LongTest.test_bit_length
4+
*graalpython.lib-python.3.test.test_long.LongTest.test_bitop_identities
35
*graalpython.lib-python.3.test.test_long.LongTest.test_conversion
6+
*graalpython.lib-python.3.test.test_long.LongTest.test_division
7+
*graalpython.lib-python.3.test.test_long.LongTest.test_float_conversion
8+
*graalpython.lib-python.3.test.test_long.LongTest.test_float_overflow
49
*graalpython.lib-python.3.test.test_long.LongTest.test_floordiv
10+
*graalpython.lib-python.3.test.test_long.LongTest.test_format
511
*graalpython.lib-python.3.test.test_long.LongTest.test_from_bytes
612
*graalpython.lib-python.3.test.test_long.LongTest.test_huge_lshift
713
*graalpython.lib-python.3.test.test_long.LongTest.test_huge_lshift_of_zero
@@ -11,6 +17,7 @@
1117
*graalpython.lib-python.3.test.test_long.LongTest.test_long
1218
*graalpython.lib-python.3.test.test_long.LongTest.test_lshift_of_zero
1319
*graalpython.lib-python.3.test.test_long.LongTest.test_mod_division
20+
*graalpython.lib-python.3.test.test_long.LongTest.test_nan_inf
1421
*graalpython.lib-python.3.test.test_long.LongTest.test_negative_shift_count
1522
*graalpython.lib-python.3.test.test_long.LongTest.test_shift_bool
1623
*graalpython.lib-python.3.test.test_long.LongTest.test_small_ints

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinConstructors.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ PComplex complexFromLongLong(Object cls, long real, long imaginary) {
384384

385385
@Specialization
386386
PComplex complexFromLongLong(Object cls, PInt real, PInt imaginary) {
387-
return createComplex(cls, real.doubleValue(), imaginary.doubleValue());
387+
return createComplex(cls, real.doubleValueWithOverflow(getRaiseNode()), imaginary.doubleValueWithOverflow(getRaiseNode()));
388388
}
389389

390390
@Specialization
@@ -409,7 +409,7 @@ PComplex complexFromLong(Object cls, long real, @SuppressWarnings("unused") PNon
409409

410410
@Specialization(guards = "isNoValue(imag)")
411411
PComplex complexFromLong(Object cls, PInt real, @SuppressWarnings("unused") PNone imag) {
412-
return createComplex(cls, real.doubleValue(), 0);
412+
return createComplex(cls, real.doubleValueWithOverflow(getRaiseNode()), 0);
413413
}
414414

415415
@Specialization(guards = {"isNoValue(imag)", "!isNoValue(number)", "!isString(number)"}, limit = "1")
@@ -433,7 +433,7 @@ PComplex complexFromLongComplex(Object cls, long one, PComplex two) {
433433

434434
@Specialization
435435
PComplex complexFromPIntComplex(Object cls, PInt one, PComplex two) {
436-
return createComplex(cls, one.doubleValue() - two.getImag(), two.getReal());
436+
return createComplex(cls, one.doubleValueWithOverflow(getRaiseNode()) - two.getImag(), two.getReal());
437437
}
438438

439439
@Specialization
@@ -475,12 +475,12 @@ PComplex complexFromComplexPInt(VirtualFrame frame, Object cls, Object one, PInt
475475
PComplex value = getComplexNumberFromObject(frame, one, lib);
476476
if (value == null) {
477477
if (lib.canBeJavaDouble(one)) {
478-
return createComplex(cls, lib.asJavaDouble(one), two.doubleValue());
478+
return createComplex(cls, lib.asJavaDouble(one), two.doubleValueWithOverflow(getRaiseNode()));
479479
} else {
480480
throw raiseFirstArgError(one);
481481
}
482482
}
483-
return createComplex(cls, value.getReal(), value.getImag() + two.doubleValue());
483+
return createComplex(cls, value.getReal(), value.getImag() + two.doubleValueWithOverflow(getRaiseNode()));
484484
}
485485

486486
@Specialization(guards = "!isString(one)", limit = "1")

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,16 @@ protected String bigToString(BigInteger x) {
279279
return x.toString(2);
280280
}
281281

282+
@TruffleBoundary
283+
protected BigInteger bigAbs(BigInteger x) {
284+
return x.abs();
285+
}
286+
282287
@Specialization
283288
String doL(long x,
284289
@Cached ConditionProfile isMinLong) {
285290
if (isMinLong.profile(x == Long.MIN_VALUE)) {
286-
return buildString(true, bigToString(PInt.longToBigInteger(x)));
291+
return buildString(true, bigToString(bigAbs(PInt.longToBigInteger(x))));
287292
}
288293
return buildString(x < 0, longToString(Math.abs(x)));
289294
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/MathGuards.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
public class MathGuards {
5151

5252
public static boolean fitLong(double value) {
53-
return Long.MIN_VALUE <= value && value <= Long.MAX_VALUE;
53+
return Long.MIN_VALUE < value && value < Long.MAX_VALUE;
5454
}
5555

5656
public static boolean fitInt(double value) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/MathModuleBuiltins.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public double doD(double value) {
129129

130130
@Specialization
131131
public double doPI(PInt value) {
132-
return count(value.doubleValue());
132+
return count(value.doubleValueWithOverflow(getRaiseNode()));
133133
}
134134

135135
@Specialization(guards = "!isNumber(value)", limit = "1")
@@ -175,6 +175,9 @@ protected static BigDecimal sqrtBigNumber(BigInteger value) {
175175
@TruffleBoundary
176176
@Override
177177
public double doPI(PInt value) {
178+
// Tests require that OverflowError is raised when the value does not fit into double
179+
// but we don't actually need the double value, so this is called for side-effect only:
180+
value.doubleValueWithOverflow(getRaiseNode());
178181
BigInteger bValue = value.getValue();
179182
checkMathDomainError(bValue.compareTo(BigInteger.ZERO) < 0);
180183
return sqrtBigNumber(bValue).doubleValue();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/SysModuleBuiltins.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,17 @@ public void initialize(PythonCore core) {
142142
"\n[Graal, " + Truffle.getRuntime().getName() + ", Java " + System.getProperty("java.version") + "]");
143143
// the default values taken from JPython
144144
builtinConstants.put("float_info", core.factory().createTuple(new Object[]{
145-
Double.MAX_VALUE, // DBL_MAX
146-
Double.MAX_EXPONENT, // DBL_MAX_EXP
147-
308, // DBL_MIN_10_EXP
148-
Double.MIN_VALUE, // DBL_MIN
149-
Double.MIN_EXPONENT, // DBL_MIN_EXP
150-
-307, // DBL_MIN_10_EXP
151-
10, // DBL_DIG
152-
53, // DBL_MANT_DIG
153-
2.2204460492503131e-16, // DBL_EPSILON
154-
2, // FLT_RADIX
155-
1 // FLT_ROUNDS
145+
Double.MAX_VALUE, // DBL_MAX
146+
Double.MAX_EXPONENT + 1, // DBL_MAX_EXP
147+
308, // DBL_MIN_10_EXP
148+
Double.MIN_VALUE, // DBL_MIN
149+
Double.MIN_EXPONENT, // DBL_MIN_EXP
150+
-307, // DBL_MIN_10_EXP
151+
10, // DBL_DIG
152+
53, // DBL_MANT_DIG
153+
2.2204460492503131e-16, // DBL_EPSILON
154+
2, // FLT_RADIX
155+
1 // FLT_ROUNDS
156156
}));
157157
builtinConstants.put("maxunicode", IntegerFormatter.LIMIT_UNICODE.intValue() - 1);
158158

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/floats/FloatBuiltins.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
package com.oracle.graal.python.builtins.objects.floats;
2727

2828
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TypeError;
29+
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.OverflowError;
2930
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.ValueError;
3031
import static com.oracle.graal.python.nodes.SpecialMethodNames.__ABS__;
3132
import static com.oracle.graal.python.nodes.SpecialMethodNames.__ADD__;
@@ -240,7 +241,7 @@ PInt doDoubleGenericError(double self) {
240241
try {
241242
return factory().createInt(fromDouble(self));
242243
} catch (NumberFormatException e) {
243-
throw raise(ValueError, ErrorMessages.CANNOT_CONVERT_FLOAT_F_TO_INT, self);
244+
throw raise(Double.isNaN(self) ? ValueError : OverflowError, ErrorMessages.CANNOT_CONVERT_FLOAT_F_TO_INT, self);
244245
}
245246
}
246247

@@ -295,7 +296,7 @@ abstract static class AddNode extends PythonBinaryBuiltinNode {
295296

296297
@Specialization
297298
double doDPi(double left, PInt right) {
298-
return left + right.doubleValue();
299+
return left + right.doubleValueWithOverflow(getRaiseNode());
299300
}
300301

301302
@Specialization
@@ -344,7 +345,7 @@ abstract static class SubNode extends PythonBinaryBuiltinNode {
344345

345346
@Specialization
346347
double doDPi(double left, PInt right) {
347-
return left - right.doubleValue();
348+
return left - right.doubleValueWithOverflow(getRaiseNode());
348349
}
349350

350351
@Specialization
@@ -354,7 +355,7 @@ abstract static class SubNode extends PythonBinaryBuiltinNode {
354355

355356
@Specialization
356357
double doPiD(PInt left, double right) {
357-
return left.doubleValue() - right;
358+
return left.doubleValueWithOverflow(getRaiseNode()) - right;
358359
}
359360

360361
@SuppressWarnings("unused")
@@ -381,7 +382,7 @@ abstract static class MulNode extends PythonBinaryBuiltinNode {
381382

382383
@Specialization
383384
double doDP(double left, PInt right) {
384-
return left * right.doubleValue();
385+
return left * right.doubleValueWithOverflow(getRaiseNode());
385386
}
386387

387388
@Specialization
@@ -411,7 +412,7 @@ Object doDP(VirtualFrame frame, PythonNativeObject left, PInt right,
411412
@Cached FromNativeSubclassNode getFloat) {
412413
Double leftPrimitive = getFloat.execute(frame, left);
413414
if (leftPrimitive != null) {
414-
return leftPrimitive * right.doubleValue();
415+
return leftPrimitive * right.doubleValueWithOverflow(getRaiseNode());
415416
} else {
416417
return PNotImplemented.NOT_IMPLEMENTED;
417418
}
@@ -439,7 +440,7 @@ abstract static class PowerNode extends PythonTernaryBuiltinNode {
439440
@Specialization
440441
double doDPi(double left, PInt right, @SuppressWarnings("unused") PNone none,
441442
@Shared("negativeRaise") @Cached BranchProfile negativeRaise) {
442-
return doOperation(left, right.doubleValue(), negativeRaise);
443+
return doOperation(left, right.doubleValueWithOverflow(getRaiseNode()), negativeRaise);
443444
}
444445

445446
/**
@@ -518,14 +519,14 @@ Object doDLComplex(VirtualFrame frame, long left, double right, PNone none,
518519
double doDPi(VirtualFrame frame, PInt left, double right, @SuppressWarnings("unused") PNone none,
519520
@Shared("powCall") @Cached("create(__POW__)") LookupAndCallTernaryNode callPow,
520521
@Shared("negativeRaise") @Cached BranchProfile negativeRaise) throws UnexpectedResultException {
521-
return doDD(frame, left.doubleValue(), right, none, callPow, negativeRaise);
522+
return doDD(frame, left.doubleValueWithOverflow(getRaiseNode()), right, none, callPow, negativeRaise);
522523
}
523524

524525
@Specialization(replaces = "doDPi")
525526
Object doDPiToComplex(VirtualFrame frame, PInt left, double right, @SuppressWarnings("unused") PNone none,
526527
@Shared("powCall") @Cached("create(__POW__)") LookupAndCallTernaryNode callPow,
527528
@Shared("negativeRaise") @Cached BranchProfile negativeRaise) {
528-
return doDDToComplex(frame, left.doubleValue(), right, none, callPow, negativeRaise);
529+
return doDDToComplex(frame, left.doubleValueWithOverflow(getRaiseNode()), right, none, callPow, negativeRaise);
529530
}
530531

531532
@Specialization
@@ -570,7 +571,7 @@ abstract static class FloorDivNode extends FloatBinaryBuiltinNode {
570571
@Specialization
571572
double doDL(double left, PInt right) {
572573
raiseDivisionByZero(right.isZero());
573-
return Math.floor(left / right.doubleValue());
574+
return Math.floor(left / right.doubleValueWithOverflow(getRaiseNode()));
574575
}
575576

576577
@Specialization
@@ -588,7 +589,7 @@ abstract static class FloorDivNode extends FloatBinaryBuiltinNode {
588589
@Specialization
589590
double doPiD(PInt left, double right) {
590591
raiseDivisionByZero(right == 0.0);
591-
return Math.floor(left.doubleValue() / right);
592+
return Math.floor(left.doubleValueWithOverflow(getRaiseNode()) / right);
592593
}
593594

594595
@SuppressWarnings("unused")
@@ -832,7 +833,7 @@ abstract static class DivNode extends FloatBinaryBuiltinNode {
832833

833834
@Specialization
834835
double doDPi(double left, PInt right) {
835-
return left / right.doubleValue();
836+
return left / right.doubleValueWithOverflow(getRaiseNode());
836837
}
837838

838839
@Specialization
@@ -842,7 +843,7 @@ abstract static class DivNode extends FloatBinaryBuiltinNode {
842843

843844
@Specialization
844845
double div(PInt left, double right) {
845-
return left.doubleValue() / right;
846+
return left.doubleValueWithOverflow(getRaiseNode()) / right;
846847
}
847848

848849
@Specialization
@@ -936,7 +937,7 @@ boolean eqDbLn(double a, long b) {
936937

937938
@Specialization
938939
boolean eqDbPI(double a, PInt b) {
939-
return a == b.doubleValue();
940+
return Double.isFinite(a) && a == b.doubleValue();
940941
}
941942

942943
@Specialization
@@ -954,7 +955,7 @@ Object eqPDb(VirtualFrame frame, PythonNativeObject left, long right,
954955
@Specialization
955956
Object eqPDb(VirtualFrame frame, PythonNativeObject left, PInt right,
956957
@Cached FromNativeSubclassNode getFloat) {
957-
return getFloat.execute(frame, left) == right.doubleValue();
958+
return eqDbPI(getFloat.execute(frame, left), right);
958959
}
959960

960961
@Fallback
@@ -980,7 +981,7 @@ boolean neDbLn(double a, long b) {
980981

981982
@Specialization
982983
boolean neDbPI(double a, PInt b) {
983-
return a != b.doubleValue();
984+
return !(Double.isFinite(a) && a == b.doubleValue());
984985
}
985986

986987
@Specialization
@@ -998,7 +999,7 @@ Object eqPDb(VirtualFrame frame, PythonNativeObject left, long right,
998999
@Specialization
9991000
Object eqPDb(VirtualFrame frame, PythonNativeObject left, PInt right,
10001001
@Cached FromNativeSubclassNode getFloat) {
1001-
return getFloat.execute(frame, left) != right.doubleValue();
1002+
return neDbPI(getFloat.execute(frame, left), right);
10021003
}
10031004

10041005
@Fallback

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints/IntBuiltins.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ int doLPiOvf(int left, PInt right) {
408408
try {
409409
return Math.floorDiv(left, right.intValueExact());
410410
} catch (ArithmeticException e) {
411-
return 0;
411+
return left < 0 == right.isNegative() ? 0 : -1;
412412
}
413413
}
414414

@@ -424,7 +424,7 @@ long doLPiOvf(long left, PInt right) {
424424
try {
425425
return Math.floorDiv(left, right.longValueExact());
426426
} catch (ArithmeticException e) {
427-
return 0;
427+
return left < 0 == right.isNegative() ? 0 : -1;
428428
}
429429
}
430430

@@ -2457,6 +2457,15 @@ int get(@SuppressWarnings("unused") Object self) {
24572457
}
24582458
}
24592459

2460+
@GenerateNodeFactory
2461+
@Builtin(name = "as_integer_ratio", minNumOfPositionalArgs = 1, doc = "Return integer ratio.")
2462+
abstract static class AsIntegerRatioNode extends PythonBuiltinNode {
2463+
@Specialization
2464+
Object get(VirtualFrame frame, Object self, @Cached IntNode intNode) {
2465+
return factory().createTuple(new Object[]{intNode.execute(frame, self), 1});
2466+
}
2467+
}
2468+
24602469
@GenerateNodeFactory
24612470
@Builtin(name = SpecialMethodNames.__TRUNC__, minNumOfPositionalArgs = 1, doc = "Truncating an Integral returns itself.")
24622471
abstract static class TruncNode extends IntNode {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints/PInt.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
package com.oracle.graal.python.builtins.objects.ints;
2727

2828
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TypeError;
29+
import static com.oracle.graal.python.runtime.exception.PythonErrorType.OverflowError;
2930

3031
import java.math.BigInteger;
3132

@@ -324,6 +325,19 @@ public static double doubleValue(BigInteger value) {
324325
return value.doubleValue();
325326
}
326327

328+
public double doubleValueWithOverflow(PRaiseNode raise) {
329+
return doubleValueWithOverflow(value, raise);
330+
}
331+
332+
@TruffleBoundary
333+
public static double doubleValueWithOverflow(BigInteger value, PRaiseNode raise) {
334+
double d = value.doubleValue();
335+
if (Double.isInfinite(d)) {
336+
throw raise.raise(OverflowError, ErrorMessages.INT_TOO_LARGE_TO_CONVERT_TO_FLOAT);
337+
}
338+
return d;
339+
}
340+
327341
public int intValue() {
328342
return intValue(value);
329343
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/ErrorMessages.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ public abstract class ErrorMessages {
457457
public static final String TIMEOUT_VALUE_MUST_BE_POSITIVE = "timeout value must be positive";
458458
public static final String TIMEOUT_VALUE_TOO_LARGE = "timeout value is too large";
459459
public static final String TOLERANCE_MUST_NON_NEGATIVE = "tolerances must be non-negative";
460-
public static final String TOO_LARGE_TO_CONVERT_TO = "%s too large to convert to %";
460+
public static final String TOO_LARGE_TO_CONVERT_TO = "%s too large to convert to %s";
461461
public static final String TOO_MANY_ARG = "%s: too many arguments";
462462
public static final String TOO_MANY_VALUES_TO_UNPACK = "too many values to unpack (expected %d)";
463463
public static final String TRAILING_S_IN_STR = "Trailing %s in string";

0 commit comments

Comments
 (0)