Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Prev Previous commit
Next Next commit
try to fix Windows tests
  • Loading branch information
picnixz committed Jan 1, 2026
commit 5bd7d39475abc722ef89bfae9d76d33bbe15a7c4
56 changes: 45 additions & 11 deletions Lib/test/test_os/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -2624,18 +2624,12 @@ def test_execve_invalid_env(self):
with self.assertRaises(ValueError):
os.execve(args[0], args, newenv)

def test_execve_env_concurrent_mutation_with_fspath(self):
@unittest.skipIf(os.name == "nt", "POSIX-specific test")
def test_execve_env_concurrent_mutation_with_fspath_posix(self):
# Prevent crash when mutating environment during parsing.
# Regression test for https://github.com/python/cpython/issues/143309.

if os.name == "nt":
# See https://github.com/python/cpython/pull/143314
# to understand why we cannot use spaces in strings
# when using subprocess and os.execve() on Windows.
message = "123456"
else:
message = "hello from execve"

message = "hello from execve"
code = """
import os, sys

Expand All @@ -2647,14 +2641,54 @@ def __fspath__(self):
mutated = KEYS = VALUES = [MyPath()]

class MyEnv:
def __getitem__(self): raise RuntimeError("must not be called")
def __len__(self): return 1
def __getitem__(self): return 1
def keys(self): return KEYS
def values(self): return VALUES

args = [sys.executable, '-c', "print({message!r})"]
os.execve(args[0], args, MyEnv())
""".format(message=message)
""".format(message="hello from execve")
Comment thread
picnixz marked this conversation as resolved.
Outdated

# Use '__cleanenv' to signal to assert_python_ok() not
# to do a copy of os.environ on its own.
rc, out, _ = assert_python_ok('-c', code, __cleanenv=True)
self.assertEqual(rc, 0)
self.assertIn(bytes(message, "ascii"), out)

@unittest.skipUnless(os.name == "nt", "Windows-specific test")
def test_execve_env_concurrent_mutation_with_fspath_windows(self):
# See https://github.com/python/cpython/pull/143314
# to understand why we cannot use spaces in strings
# when using subprocess and os.execve() on Windows.
message = "123456"

code = """
import os, sys

assert list(os.environ.keys()) == ["SYSTEMROOT"]
assert list(os.environ.values()) == [{SYSTEMROOT!r}]

class MyPath:
def __fspath__(self):
mutated1.clear()
mutated2.clear()
return b"pwn"

# Python requires "SYSTEMROOT" to be set so we make sure
# that the side effect happens afterwards.
mutated1 = KEYS = ["SYSTEMROOT", MyPath()]
mutated2 = VALUES = [{SYSTEMROOT!r}, b"ed"]

class MyEnv:
def __getitem__(self): raise RuntimeError("must not be called")
def __len__(self): return 2
def keys(self): return KEYS
def values(self): return VALUES

args = [sys.executable, '-c', "print({message!r})"]
os.execve(args[0], args, MyEnv())
""".format(message=message, SYSTEMROOT=os.environ["SYSTEMROOT"])

# Use '__cleanenv' to signal to assert_python_ok() not
# to do a copy of os.environ on its own.
Expand Down
Loading