Skip to content

Commit c7e0197

Browse files
committed
Use CastToPathNode instead of PString::getValue
1 parent 8d8ed89 commit c7e0197

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@
110110
import com.oracle.graal.python.builtins.objects.module.PythonModule;
111111
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
112112
import com.oracle.graal.python.builtins.objects.socket.PSocket;
113-
import com.oracle.graal.python.builtins.objects.str.PString;
114113
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
115114
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
116115
import com.oracle.graal.python.nodes.SpecialMethodNames;
@@ -304,11 +303,6 @@ Object execute(VirtualFrame frame, PythonModule thisModule, String path, PList a
304303
return doExecute(frame, thisModule, path, args);
305304
}
306305

307-
@Specialization
308-
Object execute(VirtualFrame frame, PythonModule thisModule, PString path, PTuple args) {
309-
return execute(frame, thisModule, path.getValue(), args);
310-
}
311-
312306
@Specialization
313307
Object execute(VirtualFrame frame, PythonModule thisModule, String path, PTuple args) {
314308
// in case of execl the PList happens to be in the tuples first entry
@@ -317,8 +311,15 @@ Object execute(VirtualFrame frame, PythonModule thisModule, String path, PTuple
317311
}
318312

319313
@Specialization
320-
Object execute(VirtualFrame frame, PythonModule thisModule, PString path, PList args) {
321-
return doExecute(frame, thisModule, path.getValue(), args);
314+
Object executePath(VirtualFrame frame, PythonModule thisModule, Object path, PTuple args,
315+
@Cached @Shared("castToPath") CastToPathNode castToPathNode) {
316+
return execute(frame, thisModule, castToPathNode.execute(frame, path), args);
317+
}
318+
319+
@Specialization
320+
Object executePath(VirtualFrame frame, PythonModule thisModule, Object path, PList args,
321+
@Cached @Shared("castToPath") CastToPathNode castToPathNode) {
322+
return doExecute(frame, thisModule, castToPathNode.execute(frame, path), args);
322323
}
323324

324325
Object doExecute(VirtualFrame frame, PythonModule thisModule, String path, PSequence args) {
@@ -404,8 +405,9 @@ PNone chdir(VirtualFrame frame, String spath) {
404405
}
405406

406407
@Specialization
407-
PNone chdirPString(VirtualFrame frame, PString spath) {
408-
return chdir(frame, spath.getValue());
408+
PNone chdirPath(VirtualFrame frame, Object path,
409+
@Cached CastToPathNode castToPathNode) {
410+
return chdir(frame, castToPathNode.execute(frame, path));
409411
}
410412
}
411413

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/util/CastToPathNode.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
import com.oracle.truffle.api.dsl.Specialization;
6060
import com.oracle.truffle.api.frame.VirtualFrame;
6161
import com.oracle.truffle.api.nodes.Node;
62-
import com.oracle.truffle.api.profiles.ValueProfile;
6362

6463
/**
6564
* Converts a Python object to a Path string
@@ -105,22 +104,22 @@ String doString(String x) {
105104
}
106105

107106
@Specialization
108-
String doPString(PString x) {
109-
return x.getValue();
107+
String doPString(PString x,
108+
@Cached CastToJavaStringNode castToJavaStringNode) {
109+
return castToJavaStringNode.execute(x);
110110
}
111111

112112
@Specialization(guards = {"!isString(object)", "!isBytes(object)", "!isMemoryView(object)"})
113113
String doObject(VirtualFrame frame, Object object,
114-
@Cached("createClassProfile()") ValueProfile resultTypeProfile,
115114
@Cached PRaiseNode raise,
116-
@Cached("createFsPathCall()") LookupAndCallUnaryNode callFsPath) {
117-
Object profiled = resultTypeProfile.profile(callFsPath.executeObject(frame, object));
118-
if (profiled instanceof String) {
119-
return (String) profiled;
120-
} else if (profiled instanceof PString) {
121-
return doPString((PString) profiled);
115+
@Cached("createFsPathCall()") LookupAndCallUnaryNode callFsPath,
116+
@Cached CastToJavaStringNode castToJavaStringNode) {
117+
Object pathObject = callFsPath.executeObject(frame, object);
118+
String path = castToJavaStringNode.execute(pathObject);
119+
if (path == null) {
120+
throw raise.raise(PythonBuiltinClassType.TypeError, "invalid type %p return from path-like object", pathObject);
122121
}
123-
throw raise.raise(PythonBuiltinClassType.TypeError, "invalid type %p return from path-like object", profiled);
122+
return path;
124123
}
125124

126125
protected LookupAndCallUnaryNode createFsPathCall() {

0 commit comments

Comments
 (0)