packit / rpm-shim

Python RPM shim module for use in virtualenvs
MIT License
2 stars 4 forks source link

Avoid repeated initialization of the shim module #3

Closed nforro closed 1 year ago

nforro commented 1 year ago

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.

nforro commented 1 year ago

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 :slightly_smiling_face:

nforro commented 1 year ago

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