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

Implement PEP 302 optional get_code loader method #13315

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

P403n1x87
Copy link

We implement the optional get_code loader method. This increases compatibility with other tools/libraries that need to manipulate the code object of a module before it is executed.

P403n1x87 and others added 2 commits March 20, 2025 10:58
We implement the optional get_code loader method. This increases compatibility with other tools/libraries that need to manipulate the code object of a module before it is executed.
@@ -126,7 +127,7 @@ def find_spec(
):
return None
else:
fn = spec.origin
self.fn = fn = spec.origin
Copy link
Member

Choose a reason for hiding this comment

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

i see no reason add this random state - the spec is used to get it anyway

Copy link
Author

Choose a reason for hiding this comment

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

This is needed inside get_code as the argument is the fullname of the module rather than the file path. The same thing is done in the FileLoader from the stdlib https://github.com/python/cpython/blob/2433cc79d79d9c1db8e53d4b9bde26e9a47fb0b9/Lib/importlib/_bootstrap_external.py#L924

Copy link
Member

Choose a reason for hiding this comment

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

as the object you changed is not just a loader, but also a MetaPathFinder - it would be necessary to split those objects before the state would be permissible as there would be more than one loader instance with different values

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Author

Choose a reason for hiding this comment

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

it would be necessary to split those objects before the state would be permissible

I could try to create a full name -> origin mapping on the finder instead, to keep the changes small. How does that sound?

@RonnyPfannschmidt
Copy link
Member

unless there is a compelling example where this helps im opposed to the change as it introduces fragile state just to introduce one optional old method

@P403n1x87
Copy link
Author

P403n1x87 commented Mar 20, 2025

unless there is a compelling example where this helps

Here's one concrete example where we have to work around this DataDog/dd-trace-py#12812

optional old method

I'm not sure I understand the meaning of "old" here 🤔 . I think implementing this method is good courtesy to other libraries that might have the need to interact with the code object. Here pytest is making the assumption that it is the only tool to manipulate code object, and therefore feels no need to implement get_code.

@RonnyPfannschmidt
Copy link
Member

please understand that composing a PR about a import behavior changes in the github ux that adds state that may be fragile in multiple steps wit errors and without providing background for the need creates a bias

Copy link
Member

@RonnyPfannschmidt RonnyPfannschmidt 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 good for now - i haven't yet taken a look if we have a place in the test-suite that would allow for a reasonably easy to write test for this

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.

2 participants