Fix T68014: Add-on's override Python built-in modules

Append addon paths to the sys.path to avoid name
collisions with system modules.
This commit is contained in:
Campbell Barton 2019-08-15 15:53:11 +10:00
parent 7c258a8ad1
commit bb2394a298
Notes: blender-bot 2023-02-14 19:11:05 +01:00
Referenced by issue blender/blender-addons#68014, Keymap area in Preferences empty
2 changed files with 13 additions and 7 deletions

View File

@ -42,7 +42,7 @@ addons_fake_modules = {}
def _initialize():
path_list = paths()
for path in path_list:
_bpy.utils._sys_path_ensure(path)
_bpy.utils._sys_path_ensure_append(path)
for addon in _preferences.addons:
enable(addon.module)

View File

@ -119,11 +119,17 @@ def _test_import(module_name, loaded_modules):
return mod
def _sys_path_ensure(path):
if path not in _sys.path: # reloading would add twice
# Reloading would add twice.
def _sys_path_ensure_prepend(path):
if path not in _sys.path:
_sys.path.insert(0, path)
def _sys_path_ensure_append(path):
if path not in _sys.path:
_sys.path.append(path)
def modules_from_path(path, loaded_modules):
"""
Load all modules in a path and return them as a list.
@ -253,7 +259,7 @@ def load_scripts(reload_scripts=False, refresh_scripts=False):
for path_subdir in _script_module_dirs:
path = _os.path.join(base_path, path_subdir)
if _os.path.isdir(path):
_sys_path_ensure(path)
_sys_path_ensure_prepend(path)
# Only add to 'sys.modules' unless this is 'startup'.
if path_subdir == "startup":
@ -385,13 +391,13 @@ def refresh_script_paths():
for path_subdir in _script_module_dirs:
path = _os.path.join(base_path, path_subdir)
if _os.path.isdir(path):
_sys_path_ensure(path)
_sys_path_ensure_prepend(path)
for path in _addon_utils.paths():
_sys_path_ensure(path)
_sys_path_ensure_append(path)
path = _os.path.join(path, "modules")
if _os.path.isdir(path):
_sys_path_ensure(path)
_sys_path_ensure_append(path)
def app_template_paths(subdir=None):