Skip to content

Implement PEP 302 optional get_code loader method #13315

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
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/_pytest/assertion/rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def __init__(self, config: Config) -> None:
self._basenames_to_check_rewrite = {"conftest"}
self._marked_for_rewrite_cache: dict[str, bool] = {}
self._session_paths_checked = False
self.fn: str | None = None

def set_session(self, session: Session | None) -> None:
self.session = session
Expand Down Expand Up @@ -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?


if not self._should_rewrite(name, fn, state):
return None
Expand All @@ -143,14 +144,11 @@ def create_module(
) -> types.ModuleType | None:
return None # default behaviour is fine

def exec_module(self, module: types.ModuleType) -> None:
assert module.__spec__ is not None
assert module.__spec__.origin is not None
fn = Path(module.__spec__.origin)
def get_code(self, fullname: str) -> types.CodeType:
assert self.fn is not None
fn = Path(self.fn)
state = self.config.stash[assertstate_key]

self._rewritten_names[module.__name__] = fn

# The requested module looks like a test file, so rewrite it. This is
# the most magical part of the process: load the source, rewrite the
# asserts, and load the rewritten source. We also cache the rewritten
Expand Down Expand Up @@ -183,7 +181,15 @@ def exec_module(self, module: types.ModuleType) -> None:
self._writing_pyc = False
else:
state.trace(f"found cached rewritten pyc for {fn}")
exec(co, module.__dict__)

return co

def exec_module(self, module: types.ModuleType) -> None:
module_name = module.__name__

self._rewritten_names[module_name] = fn

exec(self.get_code(module_name), module.__dict__)

def _early_rewrite_bailout(self, name: str, state: AssertionState) -> bool:
"""A fast way to get out of rewriting modules.
Expand Down
Loading