@@ -119,7 +119,11 @@ public abstract static class ImportNode extends PythonBuiltinNode {
119119 @ Specialization
120120 @ TruffleBoundary
121121 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 );
123127 if (object == null ) {
124128 return PNone .NONE ;
125129 }
@@ -134,6 +138,9 @@ abstract static class EvalInteropNode extends PythonBuiltinNode {
134138 @ Specialization
135139 Object evalString (@ SuppressWarnings ("unused" ) PNone path , String value , String langOrMimeType ) {
136140 Env env = getContext ().getEnv ();
141+ if (!env .isPolyglotAccessAllowed ()) {
142+ throw raise (PythonErrorType .NotImplementedError , "polyglot access is not allowed" );
143+ }
137144 try {
138145 boolean mimeType = isMimeType (langOrMimeType );
139146 String lang = mimeType ? findLanguageByMimeType (env , langOrMimeType ) : langOrMimeType ;
@@ -159,6 +166,9 @@ private void raiseIfInternal(Env env, String lang) {
159166 @ Specialization
160167 Object evalFile (String path , @ SuppressWarnings ("unused" ) PNone string , String langOrMimeType ) {
161168 Env env = getContext ().getEnv ();
169+ if (!env .isPolyglotAccessAllowed ()) {
170+ throw raise (PythonErrorType .NotImplementedError , "polyglot access is not allowed" );
171+ }
162172 try {
163173 boolean mimeType = isMimeType (langOrMimeType );
164174 String lang = mimeType ? findLanguageByMimeType (env , langOrMimeType ) : langOrMimeType ;
@@ -179,6 +189,9 @@ Object evalFile(String path, @SuppressWarnings("unused") PNone string, String la
179189 @ Specialization
180190 Object evalFile (String path , @ SuppressWarnings ("unused" ) PNone string , @ SuppressWarnings ("unused" ) PNone lang ) {
181191 Env env = getContext ().getEnv ();
192+ if (!env .isPolyglotAccessAllowed ()) {
193+ throw raise (PythonErrorType .NotImplementedError , "polyglot access is not allowed" );
194+ }
182195 try {
183196 return getContext ().getEnv ().parse (Source .newBuilder (PythonLanguage .ID , env .getTruffleFile (path )).name (path ).build ()).call ();
184197 } catch (IOException e ) {
@@ -218,30 +231,59 @@ protected boolean isMimeType(String lang) {
218231 }
219232 }
220233
221- @ Builtin (name = "export_value" , minNumOfPositionalArgs = 1 , parameterNames = {"value " , "name " })
234+ @ Builtin (name = "export_value" , minNumOfPositionalArgs = 1 , parameterNames = {"name " , "value " })
222235 @ GenerateNodeFactory
223236 public abstract static class ExportSymbolNode extends PythonBuiltinNode {
224237 @ Child private GetAttributeNode getNameAttributeNode ;
225238 @ Child private CastToStringNode castToStringNode ;
226239
227- @ Specialization
240+ @ Specialization ( guards = "!isString(value)" )
228241 @ 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 );
231248 return value ;
232249 }
233250
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+
234268 @ Specialization
235269 @ TruffleBoundary
236270 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 );
238276 return fun ;
239277 }
240278
241279 @ Specialization
242280 @ TruffleBoundary
243281 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 );
245287 return fun ;
246288 }
247289
@@ -280,7 +322,11 @@ protected static boolean isModule(Object o) {
280322
281323 @ TruffleBoundary
282324 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 );
284330 }
285331 }
286332
0 commit comments