@@ -1843,6 +1843,7 @@ public abstract static class StrNode extends PythonBuiltinNode {
1843
1843
1844
1844
@ CompilationFinal private ConditionProfile isStringProfile ;
1845
1845
@ CompilationFinal private ConditionProfile isPStringProfile ;
1846
+ @ CompilationFinal private CastToJavaStringNode castToJavaStringNode ;
1846
1847
1847
1848
public final Object executeWith (VirtualFrame frame , Object arg ) {
1848
1849
return executeWith (frame , PythonBuiltinClassType .PString , arg , PNone .NO_VALUE , PNone .NO_VALUE );
@@ -1866,8 +1867,17 @@ Object strOneArg(Object strClass, Object obj, @SuppressWarnings("unused") PNone
1866
1867
return asPString (strClass , (String ) result );
1867
1868
}
1868
1869
1869
- // PythonObjectLibrary guarantees that the returned object is an instanceof of 'str'
1870
- return result ;
1870
+ if (isPrimitiveProfile .profileClass (strClass , PythonBuiltinClassType .PString )) {
1871
+ // PythonObjectLibrary guarantees that the returned object is an instanceof of 'str'
1872
+ return result ;
1873
+ } else {
1874
+ try {
1875
+ return asPString (strClass , getCastToJavaStringNode ().execute (result ));
1876
+ } catch (CannotCastException e ) {
1877
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
1878
+ throw new IllegalStateException ("asPstring result not castable to String" );
1879
+ }
1880
+ }
1871
1881
}
1872
1882
1873
1883
@ Specialization (guards = {"!isNativeClass(strClass)" , "!isNoValue(encoding) || !isNoValue(errors)" }, limit = "3" )
@@ -1947,6 +1957,14 @@ private ConditionProfile getIsPStringProfile() {
1947
1957
return isPStringProfile ;
1948
1958
}
1949
1959
1960
+ private CastToJavaStringNode getCastToJavaStringNode () {
1961
+ if (castToJavaStringNode == null ) {
1962
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
1963
+ castToJavaStringNode = insert (CastToJavaStringNode .create ());
1964
+ }
1965
+ return castToJavaStringNode ;
1966
+ }
1967
+
1950
1968
public static StrNode create () {
1951
1969
return BuiltinConstructorsFactory .StrNodeFactory .create (null );
1952
1970
}
0 commit comments