test_shutil.test_environ_path_cwd fails when NoDefaultCurrentDirectoryInExePath is set
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
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
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 Lib/test/test_shutil.py 中的 TestWhich.test_environ_path_cwd 和 TestWhichBytes.test_environ_path_cwd 开始,然后对比 TestWhich.test_win_path_needs_curdir。在设置了 NoDefaultCurrentDirectoryInExePath 的 Windows 上运行受影响的测试模块;当这些测试不再依赖该环境变量,并且在两种环境中都通过时,即表示完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- operating-systems, testing-qa
- Issue 类型
- 缺陷
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 活跃度
- 停滞
- 描述清晰度
- 描述清楚
- 新手友好度
- 30/100