Skip to content

Commit 6bc05ea

Browse files
committed
Add missing specialization to 'float' constructor.
1 parent c8935f8 commit 6bc05ea

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ def __round__(x, n):
9696
def test_create(self):
9797
assert float(99) == 99
9898
assert float(999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999) == 1e+132
99+
assert float(b"0.001") == 0.001
100+
assert float("0.001") == 0.001
99101

100102
def test_hex(self):
101103
data = [

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
import com.oracle.graal.python.builtins.objects.PEllipsis;
6767
import com.oracle.graal.python.builtins.objects.PNone;
6868
import com.oracle.graal.python.builtins.objects.PNotImplemented;
69+
import com.oracle.graal.python.builtins.objects.bytes.BytesNodes;
6970
import com.oracle.graal.python.builtins.objects.bytes.BytesUtils;
7071
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
7172
import com.oracle.graal.python.builtins.objects.bytes.PIBytesLike;
@@ -557,6 +558,8 @@ public Object reversed(PythonClass cls, Object sequence,
557558
@Builtin(name = FLOAT, minNumOfPositionalArgs = 1, maxNumOfPositionalArgs = 2, constructsClass = PythonBuiltinClassType.PFloat)
558559
@GenerateNodeFactory
559560
public abstract static class FloatNode extends PythonBuiltinNode {
561+
@Child private BytesNodes.ToBytesNode toByteArrayNode;
562+
560563
private final ConditionProfile isPrimitiveProfile = ConditionProfile.createBinaryProfile();
561564

562565
protected boolean isPrimitiveFloat(Object cls) {
@@ -616,6 +619,16 @@ public Object floatFromString(PythonClass cls, String arg) {
616619
return factory().createFloat(cls, value);
617620
}
618621

622+
@Specialization(guards = "!isNativeClass(cls)")
623+
@TruffleBoundary
624+
public Object floatFromBytes(PythonClass cls, PIBytesLike arg) {
625+
double value = convertStringToDouble(new String(getByteArray(arg)));
626+
if (isPrimitiveFloat(cls)) {
627+
return value;
628+
}
629+
return factory().createFloat(cls, value);
630+
}
631+
619632
// Taken from Jython PyString's atof() method
620633
// The last statement throw Py.ValueError is modified
621634
@TruffleBoundary
@@ -721,6 +734,14 @@ Object doPythonObject(PythonNativeClass cls, Object obj,
721734
public Object floatFromObject(@SuppressWarnings("unused") Object cls, Object arg) {
722735
throw raise(TypeError, "can't convert %s to float", arg.getClass().getSimpleName());
723736
}
737+
738+
private byte[] getByteArray(PIBytesLike pByteArray) {
739+
if (toByteArrayNode == null) {
740+
CompilerDirectives.transferToInterpreterAndInvalidate();
741+
toByteArrayNode = insert(BytesNodes.ToBytesNode.create());
742+
}
743+
return toByteArrayNode.execute(pByteArray);
744+
}
724745
}
725746

726747
// frozenset([iterable])
@@ -779,7 +800,7 @@ private HashingCollectionNodes.SetItemNode getSetItemNode() {
779800
@GenerateNodeFactory
780801
public abstract static class IntNode extends PythonBuiltinNode {
781802

782-
@Child private SequenceStorageNodes.ToByteArrayNode toByteArrayNode;
803+
@Child private BytesNodes.ToBytesNode toByteArrayNode;
783804

784805
@TruffleBoundary(transferToInterpreterOnException = false)
785806
private Object stringToInt(String num, int base) {
@@ -1126,9 +1147,9 @@ protected static boolean isHandledType(Object obj) {
11261147
private byte[] getByteArray(PIBytesLike pByteArray) {
11271148
if (toByteArrayNode == null) {
11281149
CompilerDirectives.transferToInterpreterAndInvalidate();
1129-
toByteArrayNode = insert(SequenceStorageNodes.ToByteArrayNode.create());
1150+
toByteArrayNode = insert(BytesNodes.ToBytesNode.create());
11301151
}
1131-
return toByteArrayNode.execute(pByteArray.getSequenceStorage());
1152+
return toByteArrayNode.execute(pByteArray);
11321153
}
11331154

11341155
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes/BytesNodes.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,19 @@ public abstract static class ToBytesNode extends PNodeWithContext {
135135

136136
public abstract byte[] execute(Object obj);
137137

138-
@Specialization(rewriteOn = PException.class)
139-
byte[] doBytes(PIBytesLike bytes) {
140-
return getToByteArrayNode().execute(bytes.getSequenceStorage());
138+
@Specialization
139+
byte[] doBytes(PBytes bytes,
140+
@Cached("createBinaryProfile()") ConditionProfile exceptionProfile) {
141+
return doBytesLike(bytes, exceptionProfile);
141142
}
142143

143-
@Specialization(replaces = "doBytes")
144-
byte[] doBytesErro(PIBytesLike bytes,
144+
@Specialization
145+
byte[] doByteArray(PByteArray byteArray,
145146
@Cached("createBinaryProfile()") ConditionProfile exceptionProfile) {
147+
return doBytesLike(byteArray, exceptionProfile);
148+
}
149+
150+
private byte[] doBytesLike(PIBytesLike bytes, ConditionProfile exceptionProfile) {
146151
try {
147152
return getToByteArrayNode().execute(bytes.getSequenceStorage());
148153
} catch (PException e) {

0 commit comments

Comments
 (0)