6767import com .oracle .graal .python .builtins .objects .PEllipsis ;
6868import com .oracle .graal .python .builtins .objects .PNone ;
6969import com .oracle .graal .python .builtins .objects .PNotImplemented ;
70+ import com .oracle .graal .python .builtins .objects .bytes .BytesNodes ;
7071import com .oracle .graal .python .builtins .objects .bytes .BytesUtils ;
7172import com .oracle .graal .python .builtins .objects .bytes .PBytes ;
7273import com .oracle .graal .python .builtins .objects .bytes .PIBytesLike ;
@@ -558,6 +559,8 @@ public Object reversed(PythonClass cls, Object sequence,
558559 @ Builtin (name = FLOAT , minNumOfPositionalArgs = 1 , maxNumOfPositionalArgs = 2 , constructsClass = PythonBuiltinClassType .PFloat )
559560 @ GenerateNodeFactory
560561 public abstract static class FloatNode extends PythonBuiltinNode {
562+ @ Child private BytesNodes .ToBytesNode toByteArrayNode ;
563+
561564 private final ConditionProfile isPrimitiveProfile = ConditionProfile .createBinaryProfile ();
562565
563566 protected boolean isPrimitiveFloat (Object cls ) {
@@ -617,6 +620,16 @@ public Object floatFromString(PythonClass cls, String arg) {
617620 return factory ().createFloat (cls , value );
618621 }
619622
623+ @ Specialization (guards = "!isNativeClass(cls)" )
624+ @ TruffleBoundary
625+ public Object floatFromBytes (PythonClass cls , PIBytesLike arg ) {
626+ double value = convertStringToDouble (new String (getByteArray (arg )));
627+ if (isPrimitiveFloat (cls )) {
628+ return value ;
629+ }
630+ return factory ().createFloat (cls , value );
631+ }
632+
620633 // Taken from Jython PyString's atof() method
621634 // The last statement throw Py.ValueError is modified
622635 @ TruffleBoundary
@@ -722,6 +735,14 @@ Object doPythonObject(PythonNativeClass cls, Object obj,
722735 public Object floatFromObject (@ SuppressWarnings ("unused" ) Object cls , Object arg ) {
723736 throw raise (TypeError , "can't convert %s to float" , arg .getClass ().getSimpleName ());
724737 }
738+
739+ private byte [] getByteArray (PIBytesLike pByteArray ) {
740+ if (toByteArrayNode == null ) {
741+ CompilerDirectives .transferToInterpreterAndInvalidate ();
742+ toByteArrayNode = insert (BytesNodes .ToBytesNode .create ());
743+ }
744+ return toByteArrayNode .execute (pByteArray );
745+ }
725746 }
726747
727748 // frozenset([iterable])
@@ -780,7 +801,7 @@ private HashingCollectionNodes.SetItemNode getSetItemNode() {
780801 @ GenerateNodeFactory
781802 public abstract static class IntNode extends PythonBuiltinNode {
782803
783- @ Child private SequenceStorageNodes . ToByteArrayNode toByteArrayNode ;
804+ @ Child private BytesNodes . ToBytesNode toByteArrayNode ;
784805
785806 @ TruffleBoundary (transferToInterpreterOnException = false )
786807 private Object stringToInt (String num , int base ) {
@@ -980,30 +1001,32 @@ public Object createInt(PythonClass cls, String arg, @SuppressWarnings("unused")
9801001 @ Specialization (guards = "isPrimitiveInt(cls)" , rewriteOn = NumberFormatException .class )
9811002 @ TruffleBoundary
9821003 int parseInt (Object cls , PIBytesLike arg , int keywordArg ) throws NumberFormatException {
983- return parseInt (cls , new String ( getByteArray ( arg ) ), keywordArg );
1004+ return parseInt (cls , toString ( arg ), keywordArg );
9841005 }
9851006
9861007 @ Specialization (guards = "isPrimitiveInt(cls)" , rewriteOn = NumberFormatException .class )
9871008 @ TruffleBoundary
9881009 long parseLong (Object cls , PIBytesLike arg , int keywordArg ) throws NumberFormatException {
989- return parseLong (cls , new String ( getByteArray ( arg ) ), keywordArg );
1010+ return parseLong (cls , toString ( arg ), keywordArg );
9901011 }
9911012
992- @ Specialization (rewriteOn = NumberFormatException .class )
993- @ TruffleBoundary
994- Object parseBytes (PythonClass cls , PIBytesLike arg , int base ) {
995- return parsePInt (cls , new String (getByteArray (arg )), base );
996- }
997-
998- @ Specialization (replaces = "parseBytes" )
999- Object parseBytesError (PythonClass cls , PIBytesLike arg , int base ) {
1013+ @ Specialization
1014+ Object parseBytesError (PythonClass cls , PIBytesLike arg , int base ,
1015+ @ Cached ("create()" ) BranchProfile errorProfile ) {
10001016 try {
1001- return parseBytes (cls , arg , base );
1017+ return parsePInt (cls , toString ( arg ) , base );
10021018 } catch (NumberFormatException e ) {
1019+ errorProfile .enter ();
10031020 throw raise (ValueError , "invalid literal for int() with base %s: %s" , base , arg );
10041021 }
10051022 }
10061023
1024+ @ Specialization (guards = "isNoValue(base)" )
1025+ Object parseBytesError (PythonClass cls , PIBytesLike arg , @ SuppressWarnings ("unused" ) PNone base ,
1026+ @ Cached ("create()" ) BranchProfile errorProfile ) {
1027+ return parseBytesError (cls , arg , 10 , errorProfile );
1028+ }
1029+
10071030 @ Specialization (guards = "isPrimitiveInt(cls)" , rewriteOn = NumberFormatException .class )
10081031 int parseInt (Object cls , PString arg , int keywordArg ) throws NumberFormatException {
10091032 return parseInt (cls , arg .getValue (), keywordArg );
@@ -1020,6 +1043,11 @@ Object parsePInt(PythonClass cls, PString arg, int keywordArg) {
10201043 return parsePInt (cls , arg .getValue (), keywordArg );
10211044 }
10221045
1046+ @ Specialization (guards = "isNoValue(base)" )
1047+ Object parsePInt (PythonClass cls , PString arg , PNone base ) {
1048+ return createInt (cls , arg .getValue (), base );
1049+ }
1050+
10231051 @ Specialization (guards = "isPrimitiveInt(cls)" , rewriteOn = NumberFormatException .class )
10241052 @ TruffleBoundary
10251053 int parseInt (@ SuppressWarnings ("unused" ) Object cls , String arg , int keywordArg ) throws NumberFormatException {
@@ -1078,7 +1106,7 @@ Object fail(PythonClass cls, Object arg, Object keywordArg) {
10781106 throw raise (TypeError , "int() can't convert non-string with explicit base" );
10791107 }
10801108
1081- @ Specialization (guards = {"isNoValue(keywordArg)" , "!isNoValue(obj)" })
1109+ @ Specialization (guards = {"isNoValue(keywordArg)" , "!isNoValue(obj)" , "!isHandledType(obj)" })
10821110 public Object createInt (PythonClass cls , Object obj , PNone keywordArg ,
10831111 @ Cached ("create(__INT__)" ) LookupAndCallUnaryNode callIntNode ,
10841112 @ Cached ("create(__TRUNC__)" ) LookupAndCallUnaryNode callTruncNode ,
@@ -1113,12 +1141,21 @@ public Object createInt(PythonClass cls, Object obj, PNone keywordArg,
11131141 }
11141142 }
11151143
1116- private byte [] getByteArray (PIBytesLike pByteArray ) {
1144+ protected static boolean isHandledType (Object obj ) {
1145+ return PGuards .isInteger (obj ) || obj instanceof Double || obj instanceof Boolean || PGuards .isString (obj ) || PGuards .isBytes (obj );
1146+ }
1147+
1148+ private String toString (PIBytesLike pByteArray ) {
11171149 if (toByteArrayNode == null ) {
11181150 CompilerDirectives .transferToInterpreterAndInvalidate ();
1119- toByteArrayNode = insert (SequenceStorageNodes . ToByteArrayNode .create ());
1151+ toByteArrayNode = insert (BytesNodes . ToBytesNode .create ());
11201152 }
1121- return toByteArrayNode .execute (pByteArray .getSequenceStorage ());
1153+ return toString (toByteArrayNode .execute (pByteArray ));
1154+ }
1155+
1156+ @ TruffleBoundary
1157+ private static String toString (byte [] barr ) {
1158+ return new String (barr );
11221159 }
11231160
11241161 }
0 commit comments