Skip to content

Commit 56b78ca

Browse files
committed
Migrate away from asPString
1 parent 2cbf274 commit 56b78ca

18 files changed

+70
-131
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/PythonBuiltinClassType.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
import com.oracle.graal.python.nodes.PRaiseNode;
6060
import com.oracle.graal.python.runtime.GilNode;
6161
import com.oracle.graal.python.runtime.PythonContext;
62-
import com.oracle.graal.python.util.PythonUtils;
6362
import com.oracle.truffle.api.CompilerAsserts;
6463
import com.oracle.truffle.api.CompilerDirectives;
6564
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
@@ -744,14 +743,6 @@ public static boolean isExceptionType(PythonBuiltinClassType type) {
744743
return type.isException;
745744
}
746745

747-
/**
748-
* Must be kept in sync with TypeBuiltins.ReprNode
749-
*/
750-
@ExportMessage
751-
String asPStringWithState(@SuppressWarnings("unused") ThreadState state) {
752-
return PythonUtils.format("<class '%s'>", getPrintName());
753-
}
754-
755746
@ExportMessage
756747
public Object lookupAndCallSpecialMethodWithState(ThreadState state, String methodName, Object[] arguments,
757748
@CachedLibrary("this") PythonObjectLibrary plib,

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@
203203
import com.oracle.graal.python.lib.PyNumberIndexNode;
204204
import com.oracle.graal.python.lib.PyObjectIsTrueNode;
205205
import com.oracle.graal.python.lib.PyObjectSizeNode;
206+
import com.oracle.graal.python.lib.PyObjectStrAsObjectNode;
206207
import com.oracle.graal.python.nodes.BuiltinNames;
207208
import com.oracle.graal.python.nodes.ErrorMessages;
208209
import com.oracle.graal.python.nodes.PGuards;
@@ -1888,17 +1889,18 @@ Object strNoArgs(Object strClass, PNone arg, Object encoding, Object errors) {
18881889
}
18891890

18901891
@Specialization(guards = {"!isNativeClass(strClass)", "!isNoValue(obj)", "isNoValue(encoding)", "isNoValue(errors)"})
1891-
Object strOneArg(Object strClass, Object obj, @SuppressWarnings("unused") PNone encoding, @SuppressWarnings("unused") PNone errors,
1892-
@CachedLibrary(limit = "2") PythonObjectLibrary lib) {
1893-
Object result = lib.asPString(obj);
1892+
Object strOneArg(VirtualFrame frame, Object strClass, Object obj, @SuppressWarnings("unused") PNone encoding, @SuppressWarnings("unused") PNone errors,
1893+
@Cached PyObjectStrAsObjectNode strNode) {
1894+
Object result = strNode.execute(frame, obj);
18941895

18951896
// try to return a primitive if possible
18961897
if (getIsStringProfile().profile(result instanceof String)) {
18971898
return asPString(strClass, (String) result);
18981899
}
18991900

19001901
if (isPrimitiveProfile.profileClass(strClass, PythonBuiltinClassType.PString)) {
1901-
// PythonObjectLibrary guarantees that the returned object is an instanceof of 'str'
1902+
// PyObjectStrAsObjectNode guarantees that the returned object is an instanceof of
1903+
// 'str'
19021904
return result;
19031905
} else {
19041906
try {
@@ -1943,12 +1945,12 @@ private Object decodeBytes(VirtualFrame frame, Object strClass, PBytes obj, Obje
19431945
* into a natively allocated subtype structure
19441946
*/
19451947
@Specialization(guards = {"isNativeClass(cls)", "isSubtypeOfString(frame, isSubtype, cls)", "isNoValue(encoding)", "isNoValue(errors)"})
1946-
static Object doNativeSubclass(@SuppressWarnings("unused") VirtualFrame frame, Object cls, Object obj, @SuppressWarnings("unused") Object encoding,
1948+
static Object doNativeSubclass(VirtualFrame frame, Object cls, Object obj, @SuppressWarnings("unused") Object encoding,
19471949
@SuppressWarnings("unused") Object errors,
19481950
@Cached @SuppressWarnings("unused") IsSubtypeNode isSubtype,
1949-
@CachedLibrary(limit = "2") PythonObjectLibrary lib,
1951+
@Cached PyObjectStrAsObjectNode strNode,
19501952
@Cached CExtNodes.StringSubtypeNew subtypeNew) {
1951-
return subtypeNew.call(cls, lib.asPString(obj));
1953+
return subtypeNew.call(cls, strNode.execute(frame, obj));
19521954
}
19531955

19541956
protected static boolean isSubtypeOfString(VirtualFrame frame, IsSubtypeNode isSubtypeNode, Object cls) {

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@
128128
import com.oracle.graal.python.lib.PyObjectAsciiNode;
129129
import com.oracle.graal.python.lib.PyObjectReprAsObjectNode;
130130
import com.oracle.graal.python.lib.PyObjectSizeNode;
131+
import com.oracle.graal.python.lib.PyObjectStrAsJavaStringNode;
132+
import com.oracle.graal.python.lib.PyObjectStrAsObjectNode;
131133
import com.oracle.graal.python.nodes.BuiltinNames;
132134
import com.oracle.graal.python.nodes.ErrorMessages;
133135
import com.oracle.graal.python.nodes.GraalPythonTranslationErrorNode;
@@ -831,6 +833,7 @@ PCode generic(VirtualFrame frame, Object wSource, Object wFilename, Object wMode
831833
@Cached CastToJavaStringNode castStr,
832834
@Cached CastToJavaIntExactNode castInt,
833835
@Cached CodecsModuleBuiltins.HandleDecodingErrorNode handleDecodingErrorNode,
836+
@Cached PyObjectStrAsJavaStringNode asStrNode,
834837
@CachedLibrary("wSource") InteropLibrary interopLib,
835838
@CachedLibrary(limit = "4") PythonObjectLibrary lib,
836839
@Cached WarnNode warnNode) {
@@ -872,7 +875,7 @@ PCode generic(VirtualFrame frame, Object wSource, Object wFilename, Object wMode
872875
}
873876
checkOptimize(optimize, kwOptimize);
874877
}
875-
String source = sourceAsString(wSource, filename, interopLib, lib, handleDecodingErrorNode);
878+
String source = sourceAsString(frame, wSource, filename, interopLib, lib, handleDecodingErrorNode, asStrNode);
876879
checkSource(source);
877880
return compile(source, filename, mode, flags, kwDontInherit, optimize);
878881
}
@@ -896,7 +899,8 @@ private void checkFlags(int flags) {
896899
}
897900

898901
// modeled after _Py_SourceAsString
899-
String sourceAsString(Object source, String filename, InteropLibrary interopLib, PythonObjectLibrary pyLib, CodecsModuleBuiltins.HandleDecodingErrorNode handleDecodingErrorNode) {
902+
String sourceAsString(VirtualFrame frame, Object source, String filename, InteropLibrary interopLib, PythonObjectLibrary pyLib,
903+
CodecsModuleBuiltins.HandleDecodingErrorNode handleDecodingErrorNode, PyObjectStrAsJavaStringNode asStrNode) {
900904
if (interopLib.isString(source)) {
901905
try {
902906
return interopLib.asString(source);
@@ -921,7 +925,7 @@ String sourceAsString(Object source, String filename, InteropLibrary interopLib,
921925
handleDecodingErrorNode.execute(decoder, "strict", source);
922926
throw CompilerDirectives.shouldNotReachHere();
923927
} catch (PException e) {
924-
throw raiseInvalidSyntax(filename, "(unicode error) %s", pyLib.asPString(e.getEscapedException()));
928+
throw raiseInvalidSyntax(filename, "(unicode error) %s", asStrNode.execute(frame, e.getEscapedException()));
925929
}
926930
}
927931
return decoder.getString();
@@ -1529,23 +1533,23 @@ public abstract static class PrintNode extends PythonBuiltinNode {
15291533
PNone printNoKeywords(VirtualFrame frame, Object[] values, @SuppressWarnings("unused") PNone sep, @SuppressWarnings("unused") PNone end, @SuppressWarnings("unused") PNone file,
15301534
@SuppressWarnings("unused") PNone flush,
15311535
@CachedLibrary(limit = "3") PythonObjectLibrary lib,
1532-
@CachedLibrary(limit = "3") PythonObjectLibrary valueLib) {
1536+
@Cached PyObjectStrAsObjectNode strNode) {
15331537
Object stdout = getStdout();
1534-
return printAllGiven(frame, values, DEFAULT_SEPARATOR, DEFAULT_END, stdout, false, lib, valueLib);
1538+
return printAllGiven(frame, values, DEFAULT_SEPARATOR, DEFAULT_END, stdout, false, lib, strNode);
15351539
}
15361540

15371541
@Specialization(guards = {"!isNone(file)", "!isNoValue(file)"})
15381542
PNone printAllGiven(VirtualFrame frame, Object[] values, String sep, String end, Object file, boolean flush,
15391543
@CachedLibrary(limit = "3") PythonObjectLibrary lib,
1540-
@CachedLibrary(limit = "3") PythonObjectLibrary valueLib) {
1544+
@Cached PyObjectStrAsObjectNode strNode) {
15411545
int lastValue = values.length - 1;
15421546
Object writeMethod = lib.lookupAttributeStrict(file, frame, "write");
15431547
for (int i = 0; i < lastValue; i++) {
1544-
lib.callObject(writeMethod, frame, valueLib.asPString(values[i]));
1548+
lib.callObject(writeMethod, frame, strNode.execute(frame, values[i]));
15451549
lib.callObject(writeMethod, frame, sep);
15461550
}
15471551
if (lastValue >= 0) {
1548-
lib.callObject(writeMethod, frame, valueLib.asPString(values[lastValue]));
1552+
lib.callObject(writeMethod, frame, strNode.execute(frame, values[lastValue]));
15491553
}
15501554
lib.callObject(writeMethod, frame, end);
15511555
if (flush) {
@@ -1562,7 +1566,7 @@ PNone printGeneric(VirtualFrame frame, Object[] values, Object sepIn, Object end
15621566
@Cached("createIfTrueNode()") CoerceToBooleanNode castFlush,
15631567
@Cached PRaiseNode raiseNode,
15641568
@CachedLibrary(limit = "4") PythonObjectLibrary lib,
1565-
@CachedLibrary(limit = "3") PythonObjectLibrary valueLib) {
1569+
@Cached PyObjectStrAsObjectNode strNode) {
15661570
String sep;
15671571
try {
15681572
sep = sepIn instanceof PNone ? DEFAULT_SEPARATOR : castSep.execute(sepIn);
@@ -1589,7 +1593,7 @@ PNone printGeneric(VirtualFrame frame, Object[] values, Object sepIn, Object end
15891593
} else {
15901594
flush = castFlush.executeBoolean(frame, flushIn);
15911595
}
1592-
return printAllGiven(frame, values, sep, end, file, flush, lib, valueLib);
1596+
return printAllGiven(frame, values, sep, end, file, flush, lib, strNode);
15931597
}
15941598

15951599
private Object getStdout() {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@
4949
import com.oracle.graal.python.builtins.objects.PNone;
5050
import com.oracle.graal.python.builtins.objects.module.PythonModule;
5151
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
52+
import com.oracle.graal.python.lib.PyObjectStrAsJavaStringNode;
5253
import com.oracle.graal.python.nodes.PGuards;
5354
import com.oracle.graal.python.nodes.PNodeWithRaise;
5455
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
55-
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
5656
import com.oracle.graal.python.runtime.PythonContext;
5757
import com.oracle.truffle.api.dsl.Cached;
5858
import com.oracle.truffle.api.dsl.CachedContext;
@@ -88,10 +88,10 @@ public abstract static class GetPreferredEncoding extends PNodeWithRaise {
8888
static String getpreferredencoding(VirtualFrame frame,
8989
@CachedContext(PythonLanguage.class) PythonContext context,
9090
@CachedLibrary(limit = "2") PythonObjectLibrary lib,
91-
@Cached CastToJavaStringNode castToJavaStringNode) {
91+
@Cached PyObjectStrAsJavaStringNode strNode) {
9292
PythonModule codecs = context.getCore().lookupBuiltinModule("_codecs_truffle");
9393
Object e = lib.lookupAndCallRegularMethod(codecs, frame, "_getpreferredencoding");
94-
return castToJavaStringNode.execute(lib.asPString(e));
94+
return strNode.execute(frame, e);
9595
}
9696
}
9797

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/lzma/LZMANodes.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,14 @@
101101
import com.oracle.graal.python.builtins.objects.ints.PInt;
102102
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
103103
import com.oracle.graal.python.lib.PyObjectSizeNode;
104+
import com.oracle.graal.python.lib.PyObjectStrAsJavaStringNode;
104105
import com.oracle.graal.python.nodes.PGuards;
105106
import com.oracle.graal.python.nodes.PNodeWithRaise;
106107
import com.oracle.graal.python.nodes.PRaiseNode;
107108
import com.oracle.graal.python.nodes.subscript.GetItemNode;
108109
import com.oracle.graal.python.nodes.util.CannotCastException;
109110
import com.oracle.graal.python.nodes.util.CastToJavaLongExactNode;
110111
import com.oracle.graal.python.nodes.util.CastToJavaLongLossyNode;
111-
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
112112
import com.oracle.graal.python.runtime.NFILZMASupport;
113113
import com.oracle.graal.python.runtime.NativeLibrary;
114114
import com.oracle.graal.python.runtime.PythonContext;
@@ -269,13 +269,12 @@ public OptionsState execute(Object key, OptionsState s) {
269269

270270
@Specialization(limit = "2")
271271
OptionsState doit(Object key, OptionsState s,
272-
@Cached CastToJavaStringNode castStr,
272+
@Cached PyObjectStrAsJavaStringNode strNode,
273273
@Cached CastToJavaLongLossyNode toLong,
274274
@Cached ConditionProfile errProfile,
275-
@CachedLibrary("key") PythonObjectLibrary lib,
276275
@CachedLibrary("s.dictStorage") HashingStorageLibrary hlib,
277276
@Cached PRaiseNode raise) {
278-
String skey = castStr.execute(lib.asPString(key));
277+
String skey = strNode.execute(null, key);
279278
int idx = getOptionIndex(skey, s);
280279
if (errProfile.profile(idx == -1)) {
281280
throw raise.raise(ValueError, "Invalid filter specifier for %s filter", s.filterType);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/PythonAbstractObject.java

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
import static com.oracle.graal.python.nodes.SpecialMethodNames.__NEXT__;
6565
import static com.oracle.graal.python.nodes.SpecialMethodNames.__SETITEM__;
6666
import static com.oracle.graal.python.nodes.SpecialMethodNames.__SET__;
67-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__STR__;
6867

6968
import java.time.LocalDate;
7069
import java.time.LocalTime;
@@ -168,7 +167,6 @@
168167
import com.oracle.truffle.api.library.CachedLibrary;
169168
import com.oracle.truffle.api.library.ExportLibrary;
170169
import com.oracle.truffle.api.library.ExportMessage;
171-
import com.oracle.truffle.api.library.ExportMessage.Ignore;
172170
import com.oracle.truffle.api.nodes.ExplodeLoop;
173171
import com.oracle.truffle.api.nodes.Node;
174172
import com.oracle.truffle.api.object.DynamicObject;
@@ -756,26 +754,6 @@ public String asPathWithState(ThreadState state,
756754
}
757755
}
758756

759-
@ExportMessage
760-
public Object asPStringWithState(ThreadState state,
761-
@CachedLibrary("this") PythonObjectLibrary lib,
762-
@Shared("getClass") @Cached GetClassNode getClassNode,
763-
@Cached IsSubtypeNode isSubtypeNode,
764-
@Shared("raise") @Cached PRaiseNode raise) {
765-
return asPString(lib, this, state, isSubtypeNode, getClassNode, raise);
766-
}
767-
768-
@Ignore
769-
public static Object asPString(PythonObjectLibrary lib, Object receiver, ThreadState state,
770-
IsSubtypeNode isSubtypeNode, GetClassNode getClassNode, PRaiseNode raise) throws PException {
771-
Object result = lib.lookupAndCallSpecialMethodWithState(receiver, state, __STR__);
772-
773-
if (!isSubtypeNode.execute(getClassNode.execute(result), PythonBuiltinClassType.PString)) {
774-
throw raise.raise(TypeError, ErrorMessages.RETURNED_NON_STRING, "__str__", result);
775-
}
776-
return result;
777-
}
778-
779757
@ExportMessage
780758
public int asFileDescriptorWithState(ThreadState state,
781759
@CachedLibrary("this") PythonObjectLibrary lib,
@@ -855,7 +833,7 @@ public static Object lookupAttributeImpl(VirtualFrame frame, Object receiver, St
855833

856834
@ExportMessage
857835
public Object lookupAttributeOnTypeInternal(String name, boolean strict,
858-
@Shared("getClass") @Cached GetClassNode getClassNode,
836+
@Cached GetClassNode getClassNode,
859837
@Exclusive @Cached LookupAttributeOnTypeNode lookup) {
860838
return lookup.execute(getClassNode.execute(this), name, strict);
861839
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/deque/DequeBuiltins.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
import com.oracle.graal.python.lib.PyObjectIsTrueNode;
9898
import com.oracle.graal.python.lib.PyObjectLookupAttr;
9999
import com.oracle.graal.python.lib.PyObjectSizeNode;
100+
import com.oracle.graal.python.lib.PyObjectStrAsJavaStringNode;
100101
import com.oracle.graal.python.nodes.ErrorMessages;
101102
import com.oracle.graal.python.nodes.PGuards;
102103
import com.oracle.graal.python.nodes.PRaiseNode;
@@ -119,7 +120,6 @@
119120
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
120121
import com.oracle.graal.python.nodes.util.CannotCastException;
121122
import com.oracle.graal.python.nodes.util.CastToJavaIntExactNode;
122-
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
123123
import com.oracle.graal.python.runtime.PythonContext;
124124
import com.oracle.graal.python.runtime.exception.PException;
125125
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
@@ -131,6 +131,7 @@
131131
import com.oracle.truffle.api.dsl.Specialization;
132132
import com.oracle.truffle.api.frame.VirtualFrame;
133133
import com.oracle.truffle.api.library.CachedLibrary;
134+
import com.oracle.truffle.api.nodes.EncapsulatingNodeReference;
134135
import com.oracle.truffle.api.nodes.Node;
135136
import com.oracle.truffle.api.profiles.ConditionProfile;
136137

@@ -921,23 +922,26 @@ abstract static class DequeReprNode extends PythonUnaryBuiltinNode {
921922

922923
@Specialization
923924
@TruffleBoundary
924-
static Object repr(PDeque self) {
925+
Object repr(PDeque self) {
925926
PythonContext ctxt = PythonLanguage.getContext();
926927
if (!ctxt.reprEnter(self)) {
927928
return "[...]";
928929
}
930+
EncapsulatingNodeReference ref = EncapsulatingNodeReference.getCurrent();
931+
Node outerNode = ref.set(this);
929932
try {
930933
Object[] items = self.data.toArray();
931934
PList asList = PythonObjectFactory.getUncached().createList(items);
932935
int maxLength = self.getMaxLength();
933936
StringBuilder sb = new StringBuilder(GetNameNode.getUncached().execute(GetClassNode.getUncached().execute(self)));
934-
sb.append('(').append(CastToJavaStringNode.getUncached().execute(PythonObjectLibrary.getUncached().asPString(asList)));
937+
sb.append('(').append(PyObjectStrAsJavaStringNode.getUncached().execute(null, asList));
935938
if (maxLength != -1) {
936939
sb.append(", maxlen=").append(maxLength);
937940
}
938941
sb.append(')');
939942
return sb.toString();
940943
} finally {
944+
ref.set(outerNode);
941945
ctxt.reprLeave(self);
942946
}
943947
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/object/DefaultPythonBooleanExports.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,6 @@ static boolean bO(Boolean receiver, Object other, PythonObjectLibrary oLib, Thre
233233
}
234234
}
235235

236-
@ExportMessage
237-
static String asPStringWithState(Boolean receiver, @SuppressWarnings("unused") ThreadState state) {
238-
return receiver ? "True" : "False";
239-
}
240-
241236
@ExportMessage
242237
static int asFileDescriptorWithState(Boolean x, @SuppressWarnings("unused") ThreadState threadState) {
243238
return x ? 1 : 0;

0 commit comments

Comments
 (0)