fix pkg_resources
import failures w/ py3-only loaders
#2918
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 of changes
#1563 introduced a subtle bug in the fallback code for
pkg_resources
enumeration. The original code that only usedfind_module
later accounts for aNone
loader return value (ie, the requested module couldn't be found), but the new code blindly dots into the return fromfind_spec
(which can also beNone
) to grab itsloader
. When this occurs, it inadvertently trips theAttributeError
that was supposed to be there for py2 loaders that don't implementfind_spec
, causingfind_module
to be called even on a py3-native loader. Loaders that still implement the deprecated 2.x methods will (correctly) returnNone
, triggering the old fallback path, all is well. However, new loaders that don't implementfind_module
will blow up thepkg_resources
enumeration with anotherAttributeError
.The inadvertent trip of the py2 fallback happens a lot even with just the built-in loaders (eg, instrument the fallback exception handler with a
print
or something, then installstraight.plugin
in a fresh venv andimport pkg_resources
)- it works by accident until the builtin loaders start droppingfind_module
(as is planned for Python 3.11+), at which point it gets ugly fast.This PR minimizes the try block's surface area to only the necessary loader method call, and defensively checks for a non-empty response from
find_spec
before trying to grab itsloader
(otherwise setting the loader toNone
to restore the previous early exit behavior for modules that can't be loaded.This one's pretty tricky to add tests for without wiring up a bespoke py3 loader. Happy to add a changelog entry if the fix otherwise looks good...
Pull Request Checklist
changelog.d/
.(See documentation for details)