From d982ea9a9e449c1cb57d249cf7301956ae333930 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Fri, 26 Mar 2021 10:46:26 +0100 Subject: Fix error when an addon has no `__init__.py` When an addon has been removed, but its `.pyc` files are still there, the Python module can still be loaded. However, because `__init__.py` is missing, it becomes a namespace instead of a module, and its `__file__` will be set to `None`. As a result, it's impossible to get the mtime from the file (because there is none). This should not influence any regularly uninstalled add-on, as that would just remove the add-on's directory; I ran into the problem when switching Git branches caused an add-on's Python files to disappear while keeping the `__pycache__` directory around. --- release/scripts/modules/addon_utils.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/release/scripts/modules/addon_utils.py b/release/scripts/modules/addon_utils.py index 83bed69d8d2..387691f9f05 100644 --- a/release/scripts/modules/addon_utils.py +++ b/release/scripts/modules/addon_utils.py @@ -349,6 +349,10 @@ def enable(module_name, *, default_set=False, persistent=False, handle_error=Non # 1) try import try: mod = __import__(module_name) + if mod.__file__ is None: + # This can happen when the addon has been removed but there are + # residual `.pyc` files left behind. + raise ImportError(name=module_name) mod.__time__ = os.path.getmtime(mod.__file__) mod.__addon_enabled__ = False except Exception as ex: -- cgit v1.2.3