Repository navigation
pytester: honor explicit env in popen() - #15474
Mohit-Gaur75 wants to merge 2 commits into
Conversation
Fixes pytest-dev#15471. Only build the default os.environ copy with cwd prepended to PYTHONPATH when no explicit env is provided (env is None), so caller-supplied env mappings are passed through to subprocess.Popen untouched. Assisted-by: Gemini 4 Hydrogen
RonnyPfannschmidt
left a comment
There was a problem hiding this comment.
🤖 Written by Claude Opus 5.5 via Claude Code for the pytest maintainers; I prompted it, it did the work, I read it.
The src change is fine. Both new tests fail on every Linux/macOS job (ValueError: flush of closed file): popen() defaults to CLOSE_STDIN, which closes proc.stdin, and POSIX communicate() then flushes it. Windows takes a different path, which is why it passed for you. Pass stdin=subprocess.DEVNULL as the #15471 repro does. Keep only test_popen_explicit_env, without the env_snapshot check (nothing mutates the dict). Drop test_popen_env_none_uses_default: that path is unchanged and already covered.
Generated by Claude Code
Pass stdin=subprocess.DEVNULL so proc.communicate() does not flush a closed stdin pipe on POSIX, drop the redundant env_snapshot assertion, and drop test_popen_env_none_uses_default. Assisted-by: Gemini 4 Hydrogen
|
Thanks for catching that POSIX
|
Summary
Update
Pytester.popen()so that a caller-suppliedenvmapping is honored instead of being overwritten by a copy ofos.environ.Related Issue
Fixes #15471
Root Cause
Pytester.popen()insrc/_pytest/pytester.pyunconditionally createdenv = os.environ.copy(), prependedos.getcwd()toPYTHONPATH, and assignedkw["env"] = env. Any explicitenvpassed via**kwwas overwritten without warning.Solution
os.environcopy withos.getcwd()prepended toPYTHONPATHwhenkw.get("env") is None.envmapping is provided, pass it straight through tosubprocess.Popenwithout mutating the caller's dictionary.Pytester.popen()docstring to reflect thatcwdis added toPYTHONPATHwhen no explicitenvis provided.test_popen_explicit_envintesting/test_pytester.py(passingstdin=subprocess.DEVNULL).changelog/15471.bugfix.rstand updatedAUTHORS.Testing
python -m pytest testing/test_pytester.py: 54 passed, 1 skipped, 1 xfailed.python -m ruff check src/_pytest/pytester.py testing/test_pytester.py: All checks passed.python -m ruff format --check src/_pytest/pytester.py testing/test_pytester.py: 2 files already formatted.python -m mypy src/_pytest/pytester.py testing/test_pytester.py: Success, no issues found in 2 source files.Impact
Fixes
pytester.popen(..., env=...)dropping explicit environments. Backward-compatible for all calls omittingenvor passingenv=None.closes #XYZWto the PR description and/or commits.Co-authored-by/Assisted-by:commit trailers.changelogdirectory (changelog/15471.bugfix.rst).AUTHORSin alphabetical order.