Skip to content

gh-146310: Fix ensurepip to treat empty WHEEL_PKG_DIR as unset#146357

Merged
vstinner merged 3 commits intopython:mainfrom
kimimgo:fix/ensurepip-empty-wheel-pkg-dir-146310
Mar 27, 2026
Merged

gh-146310: Fix ensurepip to treat empty WHEEL_PKG_DIR as unset#146357
vstinner merged 3 commits intopython:mainfrom
kimimgo:fix/ensurepip-empty-wheel-pkg-dir-146310

Conversation

@kimimgo
Copy link
Copy Markdown
Contributor

@kimimgo kimimgo commented Mar 24, 2026

Fixes #146310

Path('') resolves to the current working directory (PosixPath('.')), so when WHEEL_PKG_DIR is an empty string (not None), ensurepip searches CWD for wheel files. This can cause unexpected behavior or crashes if CWD contains files matching pip-*.whl.

Changes

  • Lib/ensurepip/__init__.py: Add truthiness check so empty strings are treated the same as None
  • Lib/test/test_ensurepip.py: Add test verifying empty WHEEL_PKG_DIR does not search CWD

Before/After

# Before: Path('').resolve() -> PosixPath('/current/working/dir')
# Empty string was treated as a valid directory path

# After: Empty string treated as None -> use bundled wheel

Path('') resolves to CWD, so an empty WHEEL_PKG_DIR string caused
ensurepip to search the current working directory for wheel files.
Add a truthiness check to treat empty strings the same as None.
Per @vstinner:
- Simplify to 'if _pkg_dir:' instead of walrus + is not None
- Replace mock-based test with import_fresh_module for both '' and None
- Reword NEWS to describe old behavior instead of fix
@kimimgo
Copy link
Copy Markdown
Contributor Author

kimimgo commented Mar 24, 2026

Thanks @vstinner! Updated all three points:

  • Simplified to if _pkg_dir:
  • Replaced test with import_fresh_module covering both '' and None
  • Reworded NEWS entry

Copy link
Copy Markdown
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

LGTM

@vstinner vstinner added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Mar 25, 2026
@vstinner
Copy link
Copy Markdown
Member

Oh, "macOS / build and test (macos-26-intel)" CI failed with a timeout on test_wm_iconbitmap(): I created #146531 to track this bug and I re-ran the CI.

@vstinner vstinner enabled auto-merge (squash) March 27, 2026 20:29
@vstinner vstinner merged commit 73cc1fd into python:main Mar 27, 2026
91 of 93 checks passed
@miss-islington-app
Copy link
Copy Markdown

Thanks @kimimgo for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Mar 27, 2026
…ythonGH-146357)

Path('') resolves to CWD, so an empty WHEEL_PKG_DIR string caused
ensurepip to search the current working directory for wheel files.
Add a truthiness check to treat empty strings the same as None.
(cherry picked from commit 73cc1fd4f45b4daf2b2f9a6be69148775c7c2bff)

Co-authored-by: Imgyu Kim <kimimgo@gmail.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
@bedevere-app
Copy link
Copy Markdown

bedevere-app bot commented Mar 27, 2026

GH-146534 is a backport of this pull request to the 3.14 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Mar 27, 2026
…ythonGH-146357)

Path('') resolves to CWD, so an empty WHEEL_PKG_DIR string caused
ensurepip to search the current working directory for wheel files.
Add a truthiness check to treat empty strings the same as None.
(cherry picked from commit 73cc1fd)

Co-authored-by: Imgyu Kim <kimimgo@gmail.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Mar 27, 2026
@bedevere-app
Copy link
Copy Markdown

bedevere-app bot commented Mar 27, 2026

GH-146535 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Mar 27, 2026
@vstinner
Copy link
Copy Markdown
Member

Merged, thanks for the fix.

vstinner added a commit that referenced this pull request Mar 27, 2026
…GH-146357) (#146535)

gh-146310: Fix ensurepip to treat empty WHEEL_PKG_DIR as unset (GH-146357)

Path('') resolves to CWD, so an empty WHEEL_PKG_DIR string caused
ensurepip to search the current working directory for wheel files.
Add a truthiness check to treat empty strings the same as None.
(cherry picked from commit 73cc1fd)

Co-authored-by: Imgyu Kim <kimimgo@gmail.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
vstinner added a commit that referenced this pull request Mar 27, 2026
…GH-146357) (#146534)

gh-146310: Fix ensurepip to treat empty WHEEL_PKG_DIR as unset (GH-146357)

Path('') resolves to CWD, so an empty WHEEL_PKG_DIR string caused
ensurepip to search the current working directory for wheel files.
Add a truthiness check to treat empty strings the same as None.
(cherry picked from commit 73cc1fd)

Co-authored-by: Imgyu Kim <kimimgo@gmail.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ensurepip looks for wheels in CWD when WHEEL_PKG_DIR is an empty string

2 participants