python / python/cpython

test_shutil.test_environ_path_cwd fails when NoDefaultCurrentDirectoryInExePath is set

Open
#156,376 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

OS-windows tests
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug description:

test.test_shutil.TestWhich.test_environ_path_cwd and
test.test_shutil.TestWhichBytes.test_environ_path_cwd fail on Windows when the
environment variable NoDefaultCurrentDirectoryInExePath is set in the
environment the test suite runs in:

FAIL: test_environ_path_cwd (test.test_shutil.TestWhich.test_environ_path_cwd)
----------------------------------------------------------------------
AssertionError: '@test_21832_tmpæ.Exe' != '.\@test_21832_tmpæ.Exe'
- @test_21832_tmpæ.Exe
+ .\@test_21832_tmpæ.Exe

The test asserts that shutil.which() finds the file in the current directory,
expecting a .\-prefixed result on Windows. However, shutil.which() only
inserts the current directory into the search path when
_win_path_needs_curdir() returns true (Lib/shutil.py), and that delegates to
_winapi.NeedCurrentDirectoryForExePath(). The underlying
NeedCurrentDirectoryForExePathW() API consults
NoDefaultCurrentDirectoryInExePath, so when that variable is present the
current directory is not searched and the assertion fails.

The test does use os_helper.EnvironmentVarGuard, but only to set PATH; it
does not neutralise NoDefaultCurrentDirectoryInExePath, so an ambient value
leaks into the test. TestWhich.test_win_path_needs_curdir in the same file
already accounts for this mechanism by patching
_winapi.NeedCurrentDirectoryForExePath.

Reproducer on Windows:

set NoDefaultCurrentDirectoryInExePath=1
python -m test test_shutil

Unsetting the variable inside the existing EnvironmentVarGuard makes the test
independent of the ambient environment. I have a fix and will open a PR.

CPython versions tested on:

3.16 (main, commit 04242c027fe)

Operating systems tested on:

Windows 11

Linked PRs
  • gh-156377

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in Lib/test/test_shutil.py with TestWhich.test_environ_path_cwd and TestWhichBytes.test_environ_path_cwd, then compare TestWhich.test_win_path_needs_curdir. Run the affected test module on Windows with NoDefaultCurrentDirectoryInExePath set; done means these tests no longer depend on that ambient variable and pass in both environments.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
operating-systems, testing-qa
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.