Skip to content

pytester: honor explicit env in popen() - #15474

Open
Mohit-Gaur75 wants to merge 2 commits into
pytest-dev:mainfrom
Mohit-Gaur75:fix/pytester-popen-env
Open

Mohit-Gaur75 wants to merge 2 commits into
pytest-dev:mainfrom
Mohit-Gaur75:fix/pytester-popen-env

Conversation

@Mohit-Gaur75

@Mohit-Gaur75 Mohit-Gaur75 commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Update Pytester.popen() so that a caller-supplied env mapping is honored instead of being overwritten by a copy of os.environ.

Related Issue

Fixes #15471

Root Cause

Pytester.popen() in src/_pytest/pytester.py unconditionally created env = os.environ.copy(), prepended os.getcwd() to PYTHONPATH, and assigned kw["env"] = env. Any explicit env passed via **kw was overwritten without warning.

Solution

  • Only construct the default os.environ copy with os.getcwd() prepended to PYTHONPATH when kw.get("env") is None.
  • When an explicit env mapping is provided, pass it straight through to subprocess.Popen without mutating the caller's dictionary.
  • Updated the Pytester.popen() docstring to reflect that cwd is added to PYTHONPATH when no explicit env is provided.
  • Added regression test test_popen_explicit_env in testing/test_pytester.py (passing stdin=subprocess.DEVNULL).
  • Added changelog/15471.bugfix.rst and updated AUTHORS.

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 omitting env or passing env=None.


  • Include documentation when adding new features. (N/A — bugfix; docstring updated.)
  • Include new tests or update existing tests when applicable.
  • Allow maintainers to push and squash when merging my commits. Please uncheck this if you prefer to squash the commits yourself.
  • Add text like closes #XYZW to the PR description and/or commits.
  • If AI agents were used, they are credited in Co-authored-by / Assisted-by: commit trailers.
  • Create a new changelog file in the changelog directory (changelog/15471.bugfix.rst).
  • Add yourself to AUTHORS in alphabetical order.

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
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Nov 27, 2026

@RonnyPfannschmidt RonnyPfannschmidt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 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
@Mohit-Gaur75

Copy link
Copy Markdown
Contributor Author

Thanks for catching that POSIX communicate() failure and reviewing! Updated in commit eef2a91:

  • Added stdin=subprocess.DEVNULL to test_popen_explicit_env.
  • Removed the env_snapshot check and dropped test_popen_env_none_uses_default.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pytester.popen() silently ignores an explicit env=

2 participants