Prevent sys.prefix from leaking into child process on macOS#1648
Merged
Conversation
Closed
6 tasks
gaborbernat
requested changes
Feb 21, 2020
Signed-off-by: Bernat Gabor <bgabor8@bloomberg.net>
Contributor
Author
|
Thanks for merging! ...and sorry, did not get around to writing test and changelog earlier. Updated PR description with repro and analysis. |
rdesgroppes
added a commit
to DataDog/datadog-agent
that referenced
this pull request
Aug 14, 2025
The present change is a follow-up of #39563, where it was found that: > We need to have a default value here because these unit tests are also > called from `macos` runners but [`system` is patched as > `linux`](https://github.com/DataDog/datadog-agent/pull/39563/files#diff-407acb07e932b50660fc4999612d12a05d6046a1716a53125e1010050c898cebR33). > So even though the unit tests are run in macos runners(run on EC2) > they try to access vault and dont have K8 `POD_NAMESPACE` env var set. It turns out the temporary fix introduced by #5010 in 2020 is no longer needed: - upstream bug report: pypa/virtualenv#1643 - upstream fix: pypa/virtualenv#1648 - released with `virtualenv` v20.0.5: https://virtualenv.pypa.io/en/latest/changelog.html#v20-0-5-2020-02-21 This therefore reverts commit d16a8a4: "[omnibus] Fix MacOS build conflicting with system Python 3", and adjusts consequences accordingly: - the `POD_NAMESPACE` environment variable must be present (no default) for `linux`, and only for `linux`, - patches in tests get tailored to the very needs. (3 `platforms` => 3 "sub" tests)
rdesgroppes
added a commit
to DataDog/datadog-agent
that referenced
this pull request
Aug 14, 2025
The present change is a follow-up of #39563, where it was found that: > We need to have a default value here because these unit tests are also > called from `macos` runners but [`system` is patched as > `linux`](https://github.com/DataDog/datadog-agent/pull/39563/files#diff-407acb07e932b50660fc4999612d12a05d6046a1716a53125e1010050c898cebR33). > So even though the unit tests are run in macos runners(run on EC2) > they try to access vault and dont have K8 `POD_NAMESPACE` env var set. It turns out the temporary fix introduced by #5010 in 2020 is no longer needed: - upstream bug report: pypa/virtualenv#1643 - upstream fix: pypa/virtualenv#1648 - released with `virtualenv` v20.0.5: https://virtualenv.pypa.io/en/latest/changelog.html#v20-0-5-2020-02-21 This therefore reverts commit d16a8a4: "[omnibus] Fix MacOS build conflicting with system Python 3", and adjusts consequences accordingly: - the `POD_NAMESPACE` environment variable must be present (no default) for `linux`, and only for `linux`, - patches in tests get tailored to the very needs, with 3 `platforms` to cover implying 3 "sub" tests, - covering `win32` unearthes unmatched command patterns due to suffixes (`.bat`, `.exe`) being used for cetain commands (like `aws.exe` and `omnibus.bat`, unlike `git`) which requires further adaptation, especially to understand which patterns are not found.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1643
This PR fixes a bug on macOS where virtualenv, when run inside a virtual environment, identifies every Python interpreter as the interpreter used to generate the virtual environment.
There is a related upstream bug at bpo22490, with an open PR at python/cpython#9516.
Setup
~/Library/Application\ Support/virtualenv/./usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/bin/python3.7(via Homebrew)(This is usually accessed as
/usr/local/opt/python/bin/python3.7.)~/.pyenv/versions/3.8.1/bin/python3.8(via pyenv)Repro
Step 3 fails with the following error message:
Analysis
Here is a step-by-step analysis.
/tmp/venv/bin/virtualenv -p python3.8 /tmp/venv-3.8./tmp/venv/bin/python, a symlink to the framework build of Python 3.7./tmp/venv/bin/python) in the environment variable__PYVENV_LAUNCHER__.~/.pyenv/shims/python3.8 /tmp/venv/lib/.../virtualenv/discovery/py_info.pyto obtain information about the interpreter.~/.pyenv/versions/3.8.1/bin/python3.8(via~/.pyenv/libexec/pyenv-exec).~/.pyenv/versions/3.8.1/lib/python3.8/site.pyduring initialization.site.pyscript sees that the platform isdarwinand the__PYVENV_LAUNCHER__environment variable is set. It setssys._base_executableto the value of__PYVENV_LAUNCHER__, andsys.prefixto the grand-parent directory of this path.sys.prefixis now/tmp/venv, whilesys.base_prefixstill points to the previous value ofsys.prefix,~/.pyenv/versions/3.8.1.sys.prefixandsys.base_prefixdiffer, and assumes that the interpreter is running in a virtual environment. It determines the system executable usingsys._base_executable,/tmp/venv/bin/python./tmp/venv/bin/pythonto identify the interpreter, overriding the information previously gathered from the pyenv-provided Python 3.8. The interpreter is identified as Python 3.7.References to source code:
Step 7:
https://github.com/python/cpython/blob/d4d17fd2cf69e7c8f4cd03fbf2d575370945b952/Lib/site.py#L460-L461
Step 8:
virtualenv/src/virtualenv/discovery/py_info.py
Lines 94 to 107 in 5b88149
Step 9:
virtualenv/src/virtualenv/discovery/py_info.py
Lines 284 to 294 in 5b88149
virtualenv/src/virtualenv/discovery/py_info.py
Lines 309 to 326 in 5b88149
Thanks for contributing a pull request, see checklist all is good!
docs/changelogfolder