Skip to content

Commit 2cbf274

Browse files
committed
Don't use POL in PyObjectStrAsObjectNode
1 parent 8ca4412 commit 2cbf274

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/lib/PyObjectStrAsJavaStringNode.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
package com.oracle.graal.python.lib;
4242

4343
import com.oracle.graal.python.nodes.PNodeWithContext;
44+
import com.oracle.graal.python.nodes.util.CannotCastException;
4445
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
46+
import com.oracle.truffle.api.CompilerDirectives;
4547
import com.oracle.truffle.api.dsl.Cached;
4648
import com.oracle.truffle.api.dsl.GenerateUncached;
4749
import com.oracle.truffle.api.dsl.Specialization;
@@ -50,8 +52,7 @@
5052

5153
/**
5254
* Equivalent of CPython's {@code PyObject_Str}. Converts object to a string using its
53-
* {@code __str__} special method. Falls back to calling {@link PyObjectReprAsObjectNode} on the
54-
* value.
55+
* {@code __str__} special method.
5556
* <p>
5657
* The output is always coerced to a Java {@link String}
5758
*
@@ -61,11 +62,20 @@
6162
public abstract class PyObjectStrAsJavaStringNode extends PNodeWithContext {
6263
public abstract String execute(Frame frame, Object object);
6364

65+
@Specialization
66+
static String str(String obj) {
67+
return obj;
68+
}
69+
6470
@Specialization
6571
static String str(VirtualFrame frame, Object obj,
6672
@Cached PyObjectStrAsObjectNode strNode,
6773
@Cached CastToJavaStringNode cast) {
68-
return cast.execute(strNode.execute(frame, obj));
74+
try {
75+
return cast.execute(strNode.execute(frame, obj));
76+
} catch (CannotCastException e) {
77+
throw CompilerDirectives.shouldNotReachHere("PyObjectStrAsObjectNode result not convertible to string");
78+
}
6979
}
7080

7181
public static PyObjectStrAsJavaStringNode create() {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/lib/PyObjectStrAsObjectNode.java

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,28 @@
4040
*/
4141
package com.oracle.graal.python.lib;
4242

43-
import com.oracle.graal.python.builtins.objects.function.PArguments;
44-
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
43+
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TypeError;
44+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__STR__;
45+
46+
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
47+
import com.oracle.graal.python.builtins.objects.PNone;
4548
import com.oracle.graal.python.builtins.objects.str.PString;
49+
import com.oracle.graal.python.nodes.ErrorMessages;
4650
import com.oracle.graal.python.nodes.PNodeWithContext;
51+
import com.oracle.graal.python.nodes.PRaiseNode;
52+
import com.oracle.graal.python.nodes.call.special.CallUnaryMethodNode;
53+
import com.oracle.graal.python.nodes.call.special.LookupSpecialMethodNode;
54+
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
55+
import com.oracle.graal.python.nodes.object.GetClassNode;
4756
import com.oracle.truffle.api.dsl.Cached;
4857
import com.oracle.truffle.api.dsl.GenerateUncached;
4958
import com.oracle.truffle.api.dsl.Specialization;
5059
import com.oracle.truffle.api.frame.Frame;
5160
import com.oracle.truffle.api.frame.VirtualFrame;
52-
import com.oracle.truffle.api.library.CachedLibrary;
53-
import com.oracle.truffle.api.profiles.ConditionProfile;
5461

5562
/**
5663
* Equivalent of CPython's {@code PyObject_Str}. Converts object to a string using its
57-
* {@code __str__} special method. Falls back to calling {@link PyObjectReprAsObjectNode} on the
58-
* value.
64+
* {@code __str__} special method.
5965
* <p>
6066
* The output can be either a {@link String} or a {@link PString}.
6167
*
@@ -65,14 +71,28 @@
6571
public abstract class PyObjectStrAsObjectNode extends PNodeWithContext {
6672
public abstract Object execute(Frame frame, Object object);
6773

68-
@Specialization(limit = "3")
74+
@Specialization
75+
static Object str(String obj) {
76+
return obj;
77+
}
78+
79+
@Specialization(guards = "!isJavaString(obj)")
6980
static Object str(VirtualFrame frame, Object obj,
70-
@Cached ConditionProfile gotState,
71-
@CachedLibrary("obj") PythonObjectLibrary objLib) {
72-
if (gotState.profile(frame != null)) {
73-
return objLib.asPStringWithState(obj, PArguments.getThreadState(frame));
81+
@Cached GetClassNode getClassNode,
82+
@Cached LookupSpecialMethodNode.Dynamic lookupStr,
83+
@Cached CallUnaryMethodNode callStr,
84+
@Cached GetClassNode getResultClassNode,
85+
@Cached IsSubtypeNode isSubtypeNode,
86+
@Cached PRaiseNode raiseNode) {
87+
Object type = getClassNode.execute(obj);
88+
Object strDescr = lookupStr.execute(frame, type, __STR__, obj, false);
89+
// All our objects should have __str__
90+
assert strDescr != PNone.NONE;
91+
Object result = callStr.executeObject(frame, strDescr, obj);
92+
if (result instanceof String || isSubtypeNode.execute(getResultClassNode.execute(result), PythonBuiltinClassType.PString)) {
93+
return result;
7494
} else {
75-
return objLib.asPString(obj);
95+
throw raiseNode.raise(TypeError, ErrorMessages.RETURNED_NON_STRING, __STR__, result);
7696
}
7797
}
7898

0 commit comments

Comments
 (0)