diff options
author | Sybren A. Stüvel <sybren@blender.org> | 2021-03-26 12:46:26 +0300 |
---|---|---|
committer | Sybren A. Stüvel <sybren@blender.org> | 2021-03-26 12:46:26 +0300 |
commit | d982ea9a9e449c1cb57d249cf7301956ae333930 (patch) | |
tree | 0a2ad7cfcffd0e3ab848e21d68be45bc6aeff675 /release | |
parent | fda50625cd099b4e65b5e82c7c9a48af256b45fd (diff) |
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.
Diffstat (limited to 'release')
-rw-r--r-- | release/scripts/modules/addon_utils.py | 4 |
1 files changed, 4 insertions, 0 deletions
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: |