8282import java .util .Set ;
8383import java .util .concurrent .TimeUnit ;
8484
85+ import org .graalvm .nativeimage .ImageInfo ;
86+
8587import com .oracle .graal .python .PythonLanguage ;
8688import com .oracle .graal .python .builtins .Builtin ;
8789import com .oracle .graal .python .builtins .CoreFunctions ;
@@ -278,6 +280,18 @@ public void postInitialize(PythonCore core) {
278280 Map <String , String > getenv = System .getenv ();
279281 PDict environ = core .factory ().createDict ();
280282 for (Entry <String , String > entry : getenv .entrySet ()) {
283+ if (!ImageInfo .inImageCode ()) {
284+ // both _JAVA_OPTIONS and JAVA_TOOL_OPTIONS are adeed during JVM
285+ // startup automatically. We do not want to repeat these, they
286+ // are already in our ExecutableList if we're running on the
287+ // JVM. OTOH, some script may set these explicitly later on. So
288+ // whatever they are right now, we'll empty them, so that any
289+ // subprocess launched later will not inherit them.
290+ if ("_JAVA_OPTIONS" .equals (entry .getKey ()) || "JAVA_TOOL_OPTIONS" .equals (entry .getKey ())) {
291+ continue ;
292+ }
293+ }
294+
281295 String value ;
282296 if ("__PYVENV_LAUNCHER__" .equals (entry .getKey ())) {
283297 // On Mac, the CPython launcher uses this env variable to specify the real Python
@@ -350,6 +364,7 @@ Object doExecuteInternal(PythonModule thisModule, String path, PSequence args) t
350364 PDict environ = (PDict ) thisModule .getAttribute ("environ" );
351365 ProcessBuilder builder = new ProcessBuilder (cmd );
352366 Map <String , String > environment = builder .environment ();
367+ environment .clear ();
353368 environ .entries ().forEach (entry -> {
354369 environment .put (new String (toBytes .execute (null , entry .key )), new String (toBytes .execute (null , entry .value )));
355370 });
@@ -1481,11 +1496,12 @@ PTuple waitpidFallback(VirtualFrame frame, Object pid, Object options,
14811496 }
14821497 }
14831498
1484- @ Builtin (name = "system" , minNumOfPositionalArgs = 1 )
1499+ @ Builtin (name = "system" , minNumOfPositionalArgs = 1 , declaresExplicitSelf = true )
14851500 @ GenerateNodeFactory
14861501 @ TypeSystemReference (PythonArithmeticTypes .class )
14871502 abstract static class SystemNode extends PythonBuiltinNode {
14881503 private static final TruffleLogger LOGGER = PythonLanguage .getLogger (SystemNode .class );
1504+ @ Child private BytesNodes .ToBytesNode toBytes = BytesNodes .ToBytesNode .create ();
14891505
14901506 static final String [] shell ;
14911507 static {
@@ -1536,7 +1552,7 @@ public void finish() {
15361552
15371553 @ TruffleBoundary
15381554 @ Specialization
1539- int system (String cmd ) {
1555+ int system (PythonModule thisModule , String cmd ) {
15401556 PythonContext context = getContext ();
15411557 if (!context .isExecutableAccessAllowed ()) {
15421558 return -1 ;
@@ -1546,6 +1562,13 @@ int system(String cmd) {
15461562 Env env = context .getEnv ();
15471563 try {
15481564 ProcessBuilder pb = new ProcessBuilder (command );
1565+ PDict environ = (PDict ) thisModule .getAttribute ("environ" );
1566+ ProcessBuilder builder = new ProcessBuilder (cmd );
1567+ Map <String , String > environment = pb .environment ();
1568+ environment .clear ();
1569+ environ .entries ().forEach (entry -> {
1570+ environment .put (new String (toBytes .execute (null , entry .key )), new String (toBytes .execute (null , entry .value )));
1571+ });
15491572 pb .directory (new File (env .getCurrentWorkingDirectory ().getPath ()));
15501573 PipePump stdout = null , stderr = null ;
15511574 boolean stdsArePipes = !terminalIsInteractive (context );
0 commit comments