Skip to content

Commit

Permalink
env: prevent warning from being emitted on venv creation (#420)
Browse files Browse the repository at this point in the history
* env: prevent warning from being emitted on venv creation

After python/cpython@6811fda
the venv module produces spurious warnings for venv paths which contain
DOS-encoded parts e.g. "USER~1" in "C:\Users\USER~1".
`tempfile.gettempdir()` returns legacy paths like these for
user temp dirs.

MRE:

    python -c "import tempfile
    import venv

    venv.create(tempfile.mkdtemp())"

    Actual environment location may have moved due to redirects, links or junctions.
    Requested location: "C:\Users\RUNNER~1\AppData\Local\Temp\tmpfoobar\Scripts\python.exe"
    Actual location:    "C:\Users\runneradmin\AppData\Local\Temp\tmpfoobar\Scripts\python.exe"

Closes #413.

* tests: fix logging line nos

* fixup! env: prevent warning from being emitted on venv creation

* tests: fix line nos, again
  • Loading branch information
layday authored Jan 5, 2022
1 parent cb91293 commit 3e5737d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/build/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ def __enter__(self) -> IsolatedEnv:
:return: The isolated build environment
"""
self._path = tempfile.mkdtemp(prefix='build-env-')
# Call ``realpath`` to prevent spurious warning from being emitted
# that the venv location has changed on Windows. The username is
# DOS-encoded in the output of tempfile - the location is the same
# but the representation of it is different, which confuses venv.
# Ref: https://bugs.python.org/issue46171
self._path = os.path.realpath(tempfile.mkdtemp(prefix='build-env-'))
try:
# use virtualenv when available (as it's faster than venv)
if _should_use_virtualenv():
Expand Down
2 changes: 1 addition & 1 deletion tests/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def test_isolated_env_log(mocker, caplog, package_test_flit):
('INFO', 'Installing packages in isolated environment... (something)'),
]
if sys.version_info >= (3, 8): # stacklevel
assert [(record.lineno) for record in caplog.records] == [105, 102, 193]
assert [(record.lineno) for record in caplog.records] == [105, 107, 198]


@pytest.mark.isolated
Expand Down

0 comments on commit 3e5737d

Please sign in to comment.