|
66 | 66 | import com.oracle.graal.python.builtins.objects.PEllipsis;
|
67 | 67 | import com.oracle.graal.python.builtins.objects.PNone;
|
68 | 68 | import com.oracle.graal.python.builtins.objects.PNotImplemented;
|
| 69 | +import com.oracle.graal.python.builtins.objects.bytes.BytesNodes; |
69 | 70 | import com.oracle.graal.python.builtins.objects.bytes.BytesUtils;
|
70 | 71 | import com.oracle.graal.python.builtins.objects.bytes.PBytes;
|
71 | 72 | import com.oracle.graal.python.builtins.objects.bytes.PIBytesLike;
|
@@ -557,6 +558,8 @@ public Object reversed(PythonClass cls, Object sequence,
|
557 | 558 | @Builtin(name = FLOAT, minNumOfPositionalArgs = 1, maxNumOfPositionalArgs = 2, constructsClass = PythonBuiltinClassType.PFloat)
|
558 | 559 | @GenerateNodeFactory
|
559 | 560 | public abstract static class FloatNode extends PythonBuiltinNode {
|
| 561 | + @Child private BytesNodes.ToBytesNode toByteArrayNode; |
| 562 | + |
560 | 563 | private final ConditionProfile isPrimitiveProfile = ConditionProfile.createBinaryProfile();
|
561 | 564 |
|
562 | 565 | protected boolean isPrimitiveFloat(Object cls) {
|
@@ -616,6 +619,16 @@ public Object floatFromString(PythonClass cls, String arg) {
|
616 | 619 | return factory().createFloat(cls, value);
|
617 | 620 | }
|
618 | 621 |
|
| 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 | + |
619 | 632 | // Taken from Jython PyString's atof() method
|
620 | 633 | // The last statement throw Py.ValueError is modified
|
621 | 634 | @TruffleBoundary
|
@@ -721,6 +734,14 @@ Object doPythonObject(PythonNativeClass cls, Object obj,
|
721 | 734 | public Object floatFromObject(@SuppressWarnings("unused") Object cls, Object arg) {
|
722 | 735 | throw raise(TypeError, "can't convert %s to float", arg.getClass().getSimpleName());
|
723 | 736 | }
|
| 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 | + } |
724 | 745 | }
|
725 | 746 |
|
726 | 747 | // frozenset([iterable])
|
@@ -779,7 +800,7 @@ private HashingCollectionNodes.SetItemNode getSetItemNode() {
|
779 | 800 | @GenerateNodeFactory
|
780 | 801 | public abstract static class IntNode extends PythonBuiltinNode {
|
781 | 802 |
|
782 |
| - @Child private SequenceStorageNodes.ToByteArrayNode toByteArrayNode; |
| 803 | + @Child private BytesNodes.ToBytesNode toByteArrayNode; |
783 | 804 |
|
784 | 805 | @TruffleBoundary(transferToInterpreterOnException = false)
|
785 | 806 | private Object stringToInt(String num, int base) {
|
@@ -1126,9 +1147,9 @@ protected static boolean isHandledType(Object obj) {
|
1126 | 1147 | private byte[] getByteArray(PIBytesLike pByteArray) {
|
1127 | 1148 | if (toByteArrayNode == null) {
|
1128 | 1149 | CompilerDirectives.transferToInterpreterAndInvalidate();
|
1129 |
| - toByteArrayNode = insert(SequenceStorageNodes.ToByteArrayNode.create()); |
| 1150 | + toByteArrayNode = insert(BytesNodes.ToBytesNode.create()); |
1130 | 1151 | }
|
1131 |
| - return toByteArrayNode.execute(pByteArray.getSequenceStorage()); |
| 1152 | + return toByteArrayNode.execute(pByteArray); |
1132 | 1153 | }
|
1133 | 1154 |
|
1134 | 1155 | }
|
|
0 commit comments