fix: tolerate None __file__ in _distutils_hack frame detector - #5316
Open
wahajahmed010 wants to merge 1 commit into
Open
fix: tolerate None __file__ in _distutils_hack frame detector#5316wahajahmed010 wants to merge 1 commit into
wahajahmed010 wants to merge 1 commit into
Conversation
On Python 3.14+, a frame's f_globals can carry a None __file__ (for
example, during pip startup, where the pip shim is loaded via a
__script__.py with __file__ set to None). The previous code used
frame.f_globals.get('__file__', '').endswith('setup.py'), which
crashes with AttributeError: 'NoneType' object has no attribute
'endswith' when __file__ is present but None. Treat None and other
non-string values the same as missing. pypa#5263
Contributor
|
Tick the box to add this pull request to the merge queue (same as
|
Contributor
|
Hi @wahajahmed010 thank you very much for the contribution. Would you be able to provide a real world reproducer that does not rely on a mock/stub for us to assess this issue? You mentioned changes on Python 3.14+... Do you have a reference for that? I checked the release notes and I couldn't find any specific changes in |
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.
Summary
_distutils_hack.DistutilsMetaFinder.frame_file_is_setupcan crash during pip startup on Python 3.14+ when a frame'sf_globalscarries a__file__set toNone. The previous implementation usedframe.f_globals.get('__file__', '').endswith('setup.py'), which only handles the missing-key case; when the key is present and the value isNone, the.endswithcall raisesAttributeError: 'NoneType' object has no attribute 'endswith'. This is the same root cause behind the report in #5263: with a legacy setuptools installed,pipitself becomes unusable because the shim can't be imported.The fix treats
Noneand other non-string values the same as a missing__file__(i.e. "not a setup.py frame"). The frame walker inpip_imported_during_buildtherefore completes without raising, and the rest of_distutils_hackcontinues to work as before.How to reproduce (without my fix)
With this branch loaded instead, the same call returns
Falsecleanly.Pull Request Checklist
test_frame_file_is_setup_handles_missing_fileinsetuptools/tests/test_distutils_adoption.py, covering missing key,None, a realsetup.pypath, and a non-matching path)newsfragments/5263.bugfix.rstCloses #5263