From bb2394a29889157a330bd1064d248c04055b117c Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Thu, 15 Aug 2019 15:53:11 +1000 Subject: Fix T68014: Add-on's override Python built-in modules Append addon paths to the sys.path to avoid name collisions with system modules. --- release/scripts/modules/bpy/utils/__init__.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'release/scripts/modules/bpy') diff --git a/release/scripts/modules/bpy/utils/__init__.py b/release/scripts/modules/bpy/utils/__init__.py index b39099158c6..04aaa7bd69d 100644 --- a/release/scripts/modules/bpy/utils/__init__.py +++ b/release/scripts/modules/bpy/utils/__init__.py @@ -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): -- cgit v1.2.3