Skip to content

Commit 93feecd

Browse files
committed
Make sure we convert unexpected Java types before they reach the Python object space
1 parent 5914356 commit 93feecd

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
112112
import com.oracle.graal.python.nodes.interop.InteropBehavior;
113113
import com.oracle.graal.python.nodes.interop.InteropBehaviorMethod;
114+
import com.oracle.graal.python.nodes.interop.PForeignToPTypeNode;
114115
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
115116
import com.oracle.graal.python.nodes.util.CannotCastException;
116117
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
@@ -191,7 +192,8 @@ public void initialize(Python3Core core) {
191192
public abstract static class ImportNode extends PythonBuiltinNode {
192193
@Specialization
193194
@TruffleBoundary
194-
Object importSymbol(TruffleString name) {
195+
Object importSymbol(TruffleString name,
196+
@Cached PForeignToPTypeNode convert) {
195197
Env env = getContext().getEnv();
196198
if (!env.isPolyglotBindingsAccessAllowed()) {
197199
throw PRaiseNode.raiseUncached(this, PythonErrorType.NotImplementedError, ErrorMessages.POLYGLOT_ACCESS_NOT_ALLOWED);
@@ -200,7 +202,7 @@ Object importSymbol(TruffleString name) {
200202
if (object == null) {
201203
return PNone.NONE;
202204
}
203-
return object;
205+
return convert.executeConvert(object);
204206
}
205207
}
206208

@@ -209,7 +211,8 @@ Object importSymbol(TruffleString name) {
209211
abstract static class EvalInteropNode extends PythonBuiltinNode {
210212
@TruffleBoundary
211213
@Specialization
212-
Object evalString(@SuppressWarnings("unused") PNone path, TruffleString tvalue, TruffleString tlangOrMimeType) {
214+
Object evalString(@SuppressWarnings("unused") PNone path, TruffleString tvalue, TruffleString tlangOrMimeType,
215+
@Shared @Cached PForeignToPTypeNode convert) {
213216
Env env = getContext().getEnv();
214217
if (!env.isPolyglotEvalAllowed()) {
215218
throw PRaiseNode.raiseUncached(this, PythonErrorType.NotImplementedError, ErrorMessages.POLYGLOT_ACCESS_NOT_ALLOWED);
@@ -224,7 +227,7 @@ Object evalString(@SuppressWarnings("unused") PNone path, TruffleString tvalue,
224227
if (mimeType) {
225228
newBuilder = newBuilder.mimeType(langOrMimeType);
226229
}
227-
return env.parsePublic(newBuilder.build()).call();
230+
return convert.executeConvert(env.parsePublic(newBuilder.build()).call());
228231
} catch (RuntimeException e) {
229232
throw PRaiseNode.raiseUncached(this, NotImplementedError, e);
230233
}
@@ -239,7 +242,8 @@ private void raiseIfInternal(Env env, String lang) {
239242

240243
@TruffleBoundary
241244
@Specialization
242-
Object evalFile(TruffleString tpath, @SuppressWarnings("unused") PNone string, TruffleString tlangOrMimeType) {
245+
Object evalFile(TruffleString tpath, @SuppressWarnings("unused") PNone string, TruffleString tlangOrMimeType,
246+
@Shared @Cached PForeignToPTypeNode convert) {
243247
Env env = getContext().getEnv();
244248
if (!env.isPolyglotEvalAllowed()) {
245249
throw PRaiseNode.raiseUncached(this, PythonErrorType.NotImplementedError, ErrorMessages.POLYGLOT_ACCESS_NOT_ALLOWED);
@@ -254,7 +258,7 @@ Object evalFile(TruffleString tpath, @SuppressWarnings("unused") PNone string, T
254258
if (mimeType) {
255259
newBuilder = newBuilder.mimeType(langOrMimeType);
256260
}
257-
return getContext().getEnv().parsePublic(newBuilder.name(path).build()).call();
261+
return convert.executeConvert(getContext().getEnv().parsePublic(newBuilder.name(path).build()).call());
258262
} catch (IOException e) {
259263
throw PRaiseNode.raiseUncached(this, OSError, ErrorMessages.S, e);
260264
} catch (RuntimeException e) {
@@ -264,14 +268,15 @@ Object evalFile(TruffleString tpath, @SuppressWarnings("unused") PNone string, T
264268

265269
@TruffleBoundary
266270
@Specialization
267-
Object evalFile(TruffleString tpath, @SuppressWarnings("unused") PNone string, @SuppressWarnings("unused") PNone lang) {
271+
Object evalFile(TruffleString tpath, @SuppressWarnings("unused") PNone string, @SuppressWarnings("unused") PNone lang,
272+
@Shared @Cached PForeignToPTypeNode convert) {
268273
Env env = getContext().getEnv();
269274
if (!env.isPolyglotEvalAllowed()) {
270275
throw PRaiseNode.raiseUncached(this, PythonErrorType.NotImplementedError, ErrorMessages.POLYGLOT_ACCESS_NOT_ALLOWED);
271276
}
272277
try {
273278
String path = tpath.toJavaStringUncached();
274-
return getContext().getEnv().parsePublic(Source.newBuilder(PythonLanguage.ID, env.getPublicTruffleFile(path)).name(path).build()).call();
279+
return convert.executeConvert(getContext().getEnv().parsePublic(Source.newBuilder(PythonLanguage.ID, env.getPublicTruffleFile(path)).name(path).build()).call());
275280
} catch (IOException e) {
276281
throw PRaiseNode.raiseUncached(this, OSError, ErrorMessages.S, e);
277282
} catch (RuntimeException e) {

0 commit comments

Comments
 (0)