Skip to content

Commit e77bcef

Browse files
committed
remove _JAVA_OPTIONS and JAVA_TOOL_OPTIONS from our environment
1 parent c52ef23 commit e77bcef

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@
8282
import java.util.Set;
8383
import java.util.concurrent.TimeUnit;
8484

85+
import org.graalvm.nativeimage.ImageInfo;
86+
8587
import com.oracle.graal.python.PythonLanguage;
8688
import com.oracle.graal.python.builtins.Builtin;
8789
import 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);

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import com.oracle.graal.python.builtins.objects.PNone;
6060
import com.oracle.graal.python.builtins.objects.bytes.BytesNodes;
6161
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
62+
import com.oracle.graal.python.builtins.objects.dict.PDict;
6263
import com.oracle.graal.python.builtins.objects.function.PArguments;
6364
import com.oracle.graal.python.builtins.objects.list.PList;
6465
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
@@ -182,6 +183,7 @@ private synchronized int forkExec(PList args, @SuppressWarnings("unused") PList
182183
}
183184

184185
Map<String, String> environment = pb.environment();
186+
environment.clear();
185187
for (Object keyValue : env.getSequenceStorage().getInternalArray()) {
186188
if (keyValue instanceof PBytes) {
187189
// NOTE: passing 'null' frame means we took care of the global state in the
@@ -229,6 +231,20 @@ private synchronized int forkExec(PList args, @SuppressWarnings("unused") PList
229231
}
230232
}
231233

234+
@TruffleBoundary
235+
private PList createEnvList() {
236+
PDict environ = (PDict) getContext().getCore().lookupBuiltinModule("posix").getAttribute("environ");
237+
ArrayList<Object> envList = new ArrayList<>();
238+
environ.entries().forEach(entry -> {
239+
StringBuilder sb = new StringBuilder();
240+
sb.append(new String(toBytes.execute(null, entry.key)));
241+
sb.append("=");
242+
sb.append(new String(toBytes.execute(null, entry.value)));
243+
envList.add(factory().createBytes(sb.toString().getBytes()));
244+
});
245+
return factory().createList(envList.toArray());
246+
}
247+
232248
@Specialization(replaces = "forkExec")
233249
int forkExecDefault(VirtualFrame frame, Object args, Object executable_list, Object close_fds,
234250
Object fdsToKeep, Object cwd, Object env,
@@ -251,7 +267,7 @@ int forkExecDefault(VirtualFrame frame, Object args, Object executable_list, Obj
251267

252268
PList actualEnv;
253269
if (env instanceof PNone) {
254-
actualEnv = factory().createList();
270+
actualEnv = createEnvList();
255271
} else {
256272
actualEnv = castEnv.execute(frame, env);
257273
}

0 commit comments

Comments
 (0)