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

Avoid repeated initialization of the shim module #3

Merged
merged 2 commits into from
Feb 13, 2023
Merged

Conversation

nforro
Copy link
Member

@nforro nforro commented Feb 10, 2023

When the shim module tries to reload itself, it repeats the loop of trying all the sitepackages directories, and because importlib.reload() is noop when called recursively, it ends up raising ImportError that is visible to the user.
Even though execution returns to the original loop and the system RPM module is loaded successfully, the error message produced is very confusing.

Improve that by avoiding repeated initialization of the shim module. Also make sure that importlib.reload(), when loading the shim module for the second time, throws a specific exception that doesn't trigger any log message, to make things even less confusing.

When the shim module tries to reload itself, it repeats the loop of
trying all the sitepackages directories, and because importlib.reload()
is noop when called recursively, it ends up raising ImportError that is
visible to the user. Even though execution returns to the original loop
and the system RPM module is loaded successfully, the error message
produced is very confusing.

Improve that by avoiding repeated initialization of the shim module.
Also make sure that importlib.reload(), when loading the shim module for
the second time, throws a specific exception that doesn't trigger any
log message, to make things even less confusing.

Signed-off-by: Nikola Forró <[email protected]>
Importing the shim should be silent. If the system RPM module fails
to import, ImportError will be raised, and that makes it clear that
the process failed.

Signed-off-by: Nikola Forró <[email protected]>
Copy link
Member

@TomasTomecek TomasTomecek left a comment

Choose a reason for hiding this comment

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

this is magic

@nforro
Copy link
Member Author

nforro commented Feb 13, 2023

this is magic

The trick with the global variable to prevent repeated initialization comes from the documentation:
https://docs.python.org/3/library/importlib.html#importlib.reload

The fact that importlib.reload() does nothing when called recursively isn't documented anywhere, but it's the only reason the initial implementation worked without ending up with infinite recursion 🙂

@nforro nforro added the mergeit Zuul, merge it! label Feb 13, 2023
@nforro
Copy link
Member Author

nforro commented Feb 13, 2023

Zuul is still not enabled for this project, merging manually.

@nforro nforro merged commit 5317d3d into main Feb 13, 2023
@delete-merged-branch delete-merged-branch bot deleted the initialization branch February 13, 2023 09:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mergeit Zuul, merge it!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants