Skip to content

Commit 436a0d9

Browse files
committed
[GH-527] os.environ was not inherited with Java posix backend
1 parent 34fa858 commit 436a0d9

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_subprocess.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,21 @@ def test_java_asserts(self):
162162
result = subprocess.run([sys.executable, "-c", "import __graalpython__; not __graalpython__.java_assert()"])
163163
assert result.returncode == 0
164164

165+
def test_subprocess_inherits_environ(self):
166+
import os
167+
import subprocess
168+
prev = os.environ.get("FOOBAR")
169+
try:
170+
expected_value = f"42{prev}".strip()
171+
os.environ["FOOBAR"] = expected_value
172+
out = subprocess.check_output([sys.executable, '-c', "import os; print(os.environ['FOOBAR'])"]).decode().strip()
173+
assert out == expected_value, f"{out!r} != {expected_value!r}"
174+
finally:
175+
if prev:
176+
os.environ["FOOBAR"] = prev
177+
else:
178+
del os.environ["FOOBAR"]
179+
165180
@unittest.skipUnless(sys.implementation.name == 'graalpy', "GraalPy-specific test")
166181
@unittest.skipIf(sys.platform == 'win32', "TODO the cmd replacement breaks the test")
167182
def test_graal_python_args(self):

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/EmulatedPosixSupport.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2304,6 +2304,8 @@ public int forkExec(Object[] executables, Object[] args, Object cwd, Object[] en
23042304
throw createUnsupportedFeature("Only key=value environment variables are supported in fork_exec");
23052305
}
23062306
}
2307+
} else {
2308+
envMap = new HashMap<>(environ);
23072309
}
23082310

23092311
String[] argStrings;

0 commit comments

Comments
 (0)