diff --git a/rpm/__init__.py b/rpm/__init__.py index d9e5582..967a66f 100644 --- a/rpm/__init__.py +++ b/rpm/__init__.py @@ -20,6 +20,10 @@ logger = logging.getLogger(PROJECT_NAME) +class ShimAlreadyInitializingError(Exception): + pass + + def get_system_sitepackages() -> List[str]: """ Gets a list of sitepackages directories of system Python interpreter(s). @@ -71,7 +75,7 @@ def try_path(path: str) -> bool: return False -def init_module() -> None: +def initialize() -> None: """ Initializes the shim. Tries to load system RPM module and replace itself with it. """ @@ -81,6 +85,8 @@ def init_module() -> None: if try_path(path): logger.debug("Import successfull") return + except ShimAlreadyInitializingError: + continue except Exception as e: logger.error(f"Exception: {e}") continue @@ -88,4 +94,11 @@ def init_module() -> None: raise ImportError("Failed to import system RPM module") -init_module() +# avoid repeated initialization of the shim module +try: + _shim_module_initializing_ +except NameError: + _shim_module_initializing_: bool = True + initialize() +else: + raise ShimAlreadyInitializingError