Skip to content

Commit 663ab3a

Browse files
committed
Fix: generic case of 'FloatNode' did not cover bytes-like objects.
1 parent 5bfae8c commit 663ab3a

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_float.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,17 @@ def __round__(x, n):
9494
assert round(C(), a) == a
9595

9696
def test_create(self):
97+
class Obj:
98+
def __float__(self):
99+
return 1.123
100+
assert [float(x) for x in [Obj(), b"0.123"]] == [1.123, 0.123]
101+
97102
assert float(99) == 99
98103
assert float(999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999) == 1e+132
99104
assert float(b"0.001") == 0.001
100105
assert float("0.001") == 0.001
101106

107+
102108
def test_hex(self):
103109
data = [
104110
('nan', 'nan'),

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -717,15 +717,20 @@ public Object floatFromString(LazyPythonClass cls, String arg) {
717717
}
718718

719719
@Specialization(guards = "!isNativeClass(cls)")
720-
@TruffleBoundary
721720
public Object floatFromBytes(LazyPythonClass cls, PIBytesLike arg) {
722-
double value = convertStringToDouble(new String(getByteArray(arg)));
721+
double value = convertBytesToDouble(arg);
723722
if (isPrimitiveFloat(cls)) {
724723
return value;
725724
}
726725
return factory().createFloat(cls, value);
727726
}
728727

728+
@TruffleBoundary
729+
private double convertBytesToDouble(PIBytesLike arg) {
730+
double value = convertStringToDouble(new String(getByteArray(arg)));
731+
return value;
732+
}
733+
729734
// Taken from Jython PyString's atof() method
730735
// The last statement throw Py.ValueError is modified
731736
@TruffleBoundary
@@ -785,6 +790,8 @@ public Object floatFromNone(LazyPythonClass cls, @SuppressWarnings("unused") PNo
785790
return convertStringToDouble(((PString) obj).getValue());
786791
} else if (obj instanceof PNone) {
787792
return 0.0;
793+
} else if (obj instanceof PIBytesLike) {
794+
return convertBytesToDouble((PIBytesLike) obj);
788795
}
789796
try {
790797
return callFloatNode.executeDouble(obj);

0 commit comments

Comments
 (0)