@@ -119,7 +119,11 @@ public abstract static class ImportNode extends PythonBuiltinNode {
119
119
@ Specialization
120
120
@ TruffleBoundary
121
121
public Object importSymbol (String name ) {
122
- Object object = getContext ().getEnv ().importSymbol (name );
122
+ Env env = getContext ().getEnv ();
123
+ if (!env .isPolyglotAccessAllowed ()) {
124
+ throw raise (PythonErrorType .NotImplementedError , "polyglot access is not allowed" );
125
+ }
126
+ Object object = env .importSymbol (name );
123
127
if (object == null ) {
124
128
return PNone .NONE ;
125
129
}
@@ -134,6 +138,9 @@ abstract static class EvalInteropNode extends PythonBuiltinNode {
134
138
@ Specialization
135
139
Object evalString (@ SuppressWarnings ("unused" ) PNone path , String value , String langOrMimeType ) {
136
140
Env env = getContext ().getEnv ();
141
+ if (!env .isPolyglotAccessAllowed ()) {
142
+ throw raise (PythonErrorType .NotImplementedError , "polyglot access is not allowed" );
143
+ }
137
144
try {
138
145
boolean mimeType = isMimeType (langOrMimeType );
139
146
String lang = mimeType ? findLanguageByMimeType (env , langOrMimeType ) : langOrMimeType ;
@@ -159,6 +166,9 @@ private void raiseIfInternal(Env env, String lang) {
159
166
@ Specialization
160
167
Object evalFile (String path , @ SuppressWarnings ("unused" ) PNone string , String langOrMimeType ) {
161
168
Env env = getContext ().getEnv ();
169
+ if (!env .isPolyglotAccessAllowed ()) {
170
+ throw raise (PythonErrorType .NotImplementedError , "polyglot access is not allowed" );
171
+ }
162
172
try {
163
173
boolean mimeType = isMimeType (langOrMimeType );
164
174
String lang = mimeType ? findLanguageByMimeType (env , langOrMimeType ) : langOrMimeType ;
@@ -179,6 +189,9 @@ Object evalFile(String path, @SuppressWarnings("unused") PNone string, String la
179
189
@ Specialization
180
190
Object evalFile (String path , @ SuppressWarnings ("unused" ) PNone string , @ SuppressWarnings ("unused" ) PNone lang ) {
181
191
Env env = getContext ().getEnv ();
192
+ if (!env .isPolyglotAccessAllowed ()) {
193
+ throw raise (PythonErrorType .NotImplementedError , "polyglot access is not allowed" );
194
+ }
182
195
try {
183
196
return getContext ().getEnv ().parse (Source .newBuilder (PythonLanguage .ID , env .getTruffleFile (path )).name (path ).build ()).call ();
184
197
} catch (IOException e ) {
@@ -218,30 +231,59 @@ protected boolean isMimeType(String lang) {
218
231
}
219
232
}
220
233
221
- @ Builtin (name = "export_value" , minNumOfPositionalArgs = 1 , parameterNames = {"value " , "name " })
234
+ @ Builtin (name = "export_value" , minNumOfPositionalArgs = 1 , parameterNames = {"name " , "value " })
222
235
@ GenerateNodeFactory
223
236
public abstract static class ExportSymbolNode extends PythonBuiltinNode {
224
237
@ Child private GetAttributeNode getNameAttributeNode ;
225
238
@ Child private CastToStringNode castToStringNode ;
226
239
227
- @ Specialization
240
+ @ Specialization ( guards = "!isString(value)" )
228
241
@ TruffleBoundary
229
- public Object exportSymbol (Object value , String name ) {
230
- getContext ().getEnv ().exportSymbol (name , value );
242
+ public Object exportSymbolKeyValue (String name , Object value ) {
243
+ Env env = getContext ().getEnv ();
244
+ if (!env .isPolyglotAccessAllowed ()) {
245
+ throw raise (PythonErrorType .NotImplementedError , "polyglot access is not allowed" );
246
+ }
247
+ env .exportSymbol (name , value );
231
248
return value ;
232
249
}
233
250
251
+ @ Specialization (guards = "!isString(value)" )
252
+ @ TruffleBoundary
253
+ public Object exportSymbolValueKey (Object value , String name ) {
254
+ PythonLanguage .getLogger ().warning ("[deprecation] polyglot.export_value(value, name) is deprecated " +
255
+ "and will be removed. Please swap the arguments." );
256
+ return exportSymbolKeyValue (name , value );
257
+ }
258
+
259
+ @ Specialization (guards = "isString(arg1)" )
260
+ @ TruffleBoundary
261
+ public Object exportSymbolAmbiguous (Object arg1 , String arg2 ) {
262
+ PythonLanguage .getLogger ().warning ("[deprecation] polyglot.export_value(str, str) is ambiguous. In the future, this will " +
263
+ "default to using the first argument as the name and the second as value, but now it " +
264
+ "uses the first argument as value and the second as the name." );
265
+ return exportSymbolValueKey (arg1 , arg2 );
266
+ }
267
+
234
268
@ Specialization
235
269
@ TruffleBoundary
236
270
public Object exportSymbol (PFunction fun , @ SuppressWarnings ("unused" ) PNone name ) {
237
- getContext ().getEnv ().exportSymbol (fun .getName (), fun );
271
+ Env env = getContext ().getEnv ();
272
+ if (!env .isPolyglotAccessAllowed ()) {
273
+ throw raise (PythonErrorType .NotImplementedError , "polyglot access is not allowed" );
274
+ }
275
+ env .exportSymbol (fun .getName (), fun );
238
276
return fun ;
239
277
}
240
278
241
279
@ Specialization
242
280
@ TruffleBoundary
243
281
public Object exportSymbol (PBuiltinFunction fun , @ SuppressWarnings ("unused" ) PNone name ) {
244
- getContext ().getEnv ().exportSymbol (fun .getName (), fun );
282
+ Env env = getContext ().getEnv ();
283
+ if (!env .isPolyglotAccessAllowed ()) {
284
+ throw raise (PythonErrorType .NotImplementedError , "polyglot access is not allowed" );
285
+ }
286
+ env .exportSymbol (fun .getName (), fun );
245
287
return fun ;
246
288
}
247
289
@@ -280,7 +322,11 @@ protected static boolean isModule(Object o) {
280
322
281
323
@ TruffleBoundary
282
324
private void export (String name , Object obj ) {
283
- getContext ().getEnv ().exportSymbol (name , obj );
325
+ Env env = getContext ().getEnv ();
326
+ if (!env .isPolyglotAccessAllowed ()) {
327
+ throw raise (PythonErrorType .NotImplementedError , "polyglot access is not allowed" );
328
+ }
329
+ env .exportSymbol (name , obj );
284
330
}
285
331
}
286
332
0 commit comments