gh-92205: set loader for namespace packages PathFinder.find_spec by FFY00 · Pull Request #92279 · python/cpython

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open

FFY00 wants to merge 2 commits into python:main

base: main
Choose a base branch

from FFY00:gh-92205

Conversation

FFY00

Member

@FFY00 FFY00 commented

May 3, 2022

edited by ericsnowcurrently

Loading

…_namespace_changed

Previously, we were blocking the frozen imports and forceing the source
version to be used, but we did not fix up sys.meta_path or
sys.path_hooks, causing the frozen importers to leak into the source
version of the test.

ericsnowcurrently

Choose a reason for hiding this comment

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

mostly LGTM (just a few small tweaks before this can be merged)

Comment on lines 504 to 507

try:
module.__loader__ = loader
module.__loader__ = spec.loader
except AttributeError:
pass

Choose a reason for hiding this comment

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

It's an unlikely case, but spec might not have a loader attribute. So we should probably keep the existing

outside the try block.

# should also be None for consistency. While a bit of a hack,
# this is the best place to ensure this consistency.
#
# See # https://docs.python.org/3/library/importlib.html#importlib.abc.Loader.load_module

Choose a reason for hiding this comment

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

While you're here:

# See # https://docs.python.org/3/library/importlib.html#importlib.abc.Loader.load_module
# See https://docs.python.org/3/library/importlib.html#importlib.abc.Loader.load_module
@@ -560,6 +538,19 @@ def _init_module_attrs(spec, module, *, override=False):
module.__cached__ = spec.cached
except AttributeError:
pass
# A backward compatibility hack.
if _bootstrap_external and isinstance(spec.loader, _bootstrap_external.NamespaceLoader):

Choose a reason for hiding this comment

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

if _bootstrap_external and isinstance(spec.loader, _bootstrap_external.NamespaceLoader):
elif _bootstrap_external and isinstance(spec.loader, _bootstrap_external.NamespaceLoader):
@@ -560,6 +538,19 @@ def _init_module_attrs(spec, module, *, override=False):
module.__cached__ = spec.cached
except AttributeError:
pass
# A backward compatibility hack.

Choose a reason for hiding this comment

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

I'm pretty sure you've dropped the backward compatibility part, so we can drop this comment.

# A backward compatibility hack.

Choose a reason for hiding this comment

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

The backward compatibility hack this is referring is setting __file__ to None on namespace packages, which we are keeping.

@@ -1508,6 +1508,7 @@ def find_spec(cls, fullname, path=None, target=None):
# can create the namespace package.
spec.origin = None
spec.submodule_search_locations = _NamespacePath(fullname, namespace_path, cls._get_spec)
spec.loader = NamespaceLoader(fullname, namespace_path, cls._get_spec)

Choose a reason for hiding this comment

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

(Nothing to do here.)

I just noticed that PathEntryFinder.find_spec() is expected to return a spec with loader set to None, rather than NamespaceLoader (which is mostly fine). See #92896.

@bedevere-bot

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

warsaw

Choose a reason for hiding this comment

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

I'm finding it difficult to follow all the changes, but am I reading this right that this will change the __spec__.loader attribute on namespace packages (not namespace package portions) to a NamespaceLoader instance? I think that breaks backward compatibility and wasn't how I read the description.

I thought __spec__.loader for namespace packages is not always getting set. Setting it to None is perfectly reasonable and backward compatible for namespace packages, but it should always be None and not just missing in some cases.