Skip to content
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

py: resolve module paths without executing modules #58

Merged
merged 1 commit into from
Jan 13, 2025

Conversation

FFY00
Copy link
Contributor

@FFY00 FFY00 commented Jan 8, 2025

Fixes #57

Copy link
Collaborator

@virtuald virtuald left a comment

Choose a reason for hiding this comment

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

This seems like a better way to solve the problem, but I don't have time to run it locally. Since you actually wrote tests, I suspect it works. :)

module = importlib.import_module(name)
return list(module.__path__)
try:
with unittest.mock.patch.dict('sys.modules', sys.modules.copy()):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hm, this is an interesting approach. I can't think of a good reason why not to do it, but it smells really fishy. I presume that this is to ensure that any side effects of loaders aren't kept around either, but... it feels mildly problematic and racy, but maybe that's fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, we need it because we don't want to leave an unexecuted module in sys.modules. Initially I wanted to refrain from writting anything to sys.modules at all, but unfortunately NamespacePath looks for the parent in sys.modules when we access __path__.

https://github.com/python/cpython/blob/1b39b502d33c68f52fd775c4e6c2174baddd40bd/Lib/importlib/_bootstrap_external.py#L1112

That said, I remembered about https://docs.python.org/3/library/importlib.html#importlib.util.LazyLoader, which instead of executing the module in exec_module, it changes the module class into a custom type that postpones execution until an attribute is accessed. Unfortunately, it still doesn't let us access the types.ModuleType attributes without executing, but this can be overcome with a custom implementation.

@FFY00 FFY00 force-pushed the fix-57 branch 2 times, most recently from a7888d4 to 9dc6543 Compare January 13, 2025 00:24
@FFY00 FFY00 enabled auto-merge (squash) January 13, 2025 00:30
@FFY00 FFY00 merged commit a1ee4da into pypackaging-native:main Jan 13, 2025
18 checks passed
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.

broken packages can prevent pkgconf from functioning
2 participants