25
25
*/
26
26
package com .oracle .graal .python .builtins .modules ;
27
27
28
+ import static com .oracle .graal .python .nodes .BuiltinNames .T_ENVIRON ;
28
29
import static com .oracle .graal .python .nodes .BuiltinNames .T_NT ;
29
30
import static com .oracle .graal .python .nodes .BuiltinNames .T_POSIX ;
30
31
import static com .oracle .graal .python .nodes .StringLiterals .T_DOT ;
@@ -252,7 +253,7 @@ public void initialize(Python3Core core) {
252
253
}
253
254
PythonLanguage language = core .getLanguage ();
254
255
addBuiltinConstant ("_have_functions" , PFactory .createList (language , haveFunctions .toArray ()));
255
- addBuiltinConstant ("environ" , PFactory .createDict (language ));
256
+ addBuiltinConstant (T_ENVIRON , PFactory .createDict (language ));
256
257
257
258
LinkedHashMap <String , Object > sysconfigNames = new LinkedHashMap <>();
258
259
for (IntConstant name : PosixConstants .sysconfigNames ) {
@@ -313,16 +314,12 @@ public void postInitialize(Python3Core core) {
313
314
// we don't want subprocesses to pick it up
314
315
continue ;
315
316
}
316
- Object key , val ;
317
- if (PythonOS .getPythonOS () == PythonOS .PLATFORM_WIN32 ) {
318
- if (entry .getKey ().startsWith ("=" )) {
319
- // Hidden variable, shouldn't be visible to python
320
- continue ;
321
- }
322
- key = toTruffleStringUncached (entry .getKey ());
323
- } else {
324
- key = PFactory .createBytes (language , entry .getKey ().getBytes ());
317
+ if (PythonOS .getPythonOS () == PythonOS .PLATFORM_WIN32 && entry .getKey ().startsWith ("=" )) {
318
+ // Hidden variable, shouldn't be visible to python
319
+ continue ;
325
320
}
321
+ Object key = toEnv (language , entry .getKey ());
322
+ Object val = toEnv (language , entry .getValue ());
326
323
if (pyenvLauncherKey .equals (entry .getKey ())) {
327
324
// On Mac, the CPython launcher uses this env variable to specify the real Python
328
325
// executable. It will be honored by packages like "site". So, if it is set, we
@@ -334,31 +331,24 @@ public void postInitialize(Python3Core core) {
334
331
posixLib .setenv (posixSupport , k , v , true );
335
332
} catch (PosixException ignored ) {
336
333
}
337
- if (PythonOS .getPythonOS () == PythonOS .PLATFORM_WIN32 ) {
338
- val = value ;
339
- } else {
340
- val = PFactory .createBytes (language , value .toJavaStringUncached ().getBytes ());
341
- }
342
- } else {
343
- if (PythonOS .getPythonOS () == PythonOS .PLATFORM_WIN32 ) {
344
- val = toTruffleStringUncached (entry .getValue ());
345
- } else {
346
- val = PFactory .createBytes (language , (entry .getValue ().getBytes ()));
347
- }
334
+ val = toEnv (language , value );
348
335
}
349
336
environ .setItem (key , val );
350
337
}
351
338
if (PythonOS .getPythonOS () == PythonOS .PLATFORM_WIN32 ) {
352
339
// XXX: Until we fix pip
353
- environ .setItem (toTruffleStringUncached ( "PIP_NO_CACHE_DIR" ), toTruffleStringUncached ( "0" ));
340
+ environ .setItem (toEnv ( language , "PIP_NO_CACHE_DIR" ), toEnv ( language , "0" ));
354
341
}
342
+ // XXX: Until a pyo3 version that doesn't have a different maximum version for GraalPy than
343
+ // CPython gets widespread
344
+ environ .setItem (toEnv (language , "UNSAFE_PYO3_SKIP_VERSION_CHECK" ), toEnv (language , "1" ));
355
345
PythonModule posix ;
356
346
if (PythonOS .getPythonOS () == PythonOS .PLATFORM_WIN32 ) {
357
347
posix = core .lookupBuiltinModule (T_NT );
358
348
} else {
359
349
posix = core .lookupBuiltinModule (T_POSIX );
360
350
}
361
- Object environAttr = posix .getAttribute (tsLiteral ( "environ" ) );
351
+ Object environAttr = posix .getAttribute (T_ENVIRON );
362
352
((PDict ) environAttr ).setDictStorage (environ .getDictStorage ());
363
353
364
354
if (posixLib .getBackend (posixSupport ).toJavaStringUncached ().equals ("java" )) {
@@ -370,6 +360,22 @@ public void postInitialize(Python3Core core) {
370
360
}
371
361
}
372
362
363
+ private static Object toEnv (PythonLanguage language , String value ) {
364
+ if (PythonOS .getPythonOS () == PythonOS .PLATFORM_WIN32 ) {
365
+ return toTruffleStringUncached (value );
366
+ } else {
367
+ return PFactory .createBytes (language , value .getBytes ());
368
+ }
369
+ }
370
+
371
+ private static Object toEnv (PythonLanguage language , TruffleString value ) {
372
+ if (PythonOS .getPythonOS () == PythonOS .PLATFORM_WIN32 ) {
373
+ return value ;
374
+ } else {
375
+ return PFactory .createBytes (language , value .toJavaStringUncached ().getBytes ());
376
+ }
377
+ }
378
+
373
379
@ Builtin (name = "putenv" , minNumOfPositionalArgs = 2 , parameterNames = {"name" , "value" })
374
380
@ ArgumentClinic (name = "name" , conversionClass = FsConverterNode .class )
375
381
@ ArgumentClinic (name = "value" , conversionClass = FsConverterNode .class )
0 commit comments