From 35d9f681213c56429829afa01b13ee77d621e9d3 Mon Sep 17 00:00:00 2001 From: Sergey Sharybin Date: Tue, 25 Jul 2017 11:38:33 +0200 Subject: [PATCH 1/2] Python module test: Don't cfail the test when import failure happens in preset --- tests/python/bl_load_py_modules.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/tests/python/bl_load_py_modules.py b/tests/python/bl_load_py_modules.py index 80933cd5b61..7ffececd1d9 100644 --- a/tests/python/bl_load_py_modules.py +++ b/tests/python/bl_load_py_modules.py @@ -178,15 +178,28 @@ def load_modules(): for f in MODULE_SYS_PATHS.get(mod_name_full, ()) ]) - __import__(mod_name_full) - mod_imp = sys.modules[mod_name_full] + try: + __import__(mod_name_full) + mod_imp = sys.modules[mod_name_full] - sys.path[:] = sys_path_back + sys.path[:] = sys_path_back - # check we load what we ask for. - assert(os.path.samefile(mod_imp.__file__, submod_full)) + # check we load what we ask for. + assert(os.path.samefile(mod_imp.__file__, submod_full)) - modules.append(mod_imp) + modules.append(mod_imp) + except Exception as e: + import traceback + # Module might fail to import, but we don't want whole test to fail here. + # Reasoning: + # - This module might be in ignored list (for example, preset or template), + # so failing here will cause false-positive test failure. + # - If this is module which should not be ignored, it is not added to list + # of successfully loaded modules, meaning the test will catch this + # import failure. + # - We want to catch all failures of this script instead of stopping on + # a first big failure. + traceback.print_exc() # # check which filepaths we didn't load From edc6bec9d60204cb81d2e7533402630b076d0d32 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Tue, 25 Jul 2017 20:50:12 +1000 Subject: [PATCH 2/2] PyAPI: Skip user scripts w/ factory-startup Adds bpy.app.factory_startup, used to check if user scripts should be loaded. --- release/scripts/modules/bpy/utils/__init__.py | 29 ++++++++++++++----- source/blender/python/intern/bpy_app.c | 2 ++ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/release/scripts/modules/bpy/utils/__init__.py b/release/scripts/modules/bpy/utils/__init__.py index 703a61f72f5..6d3e807c2b4 100644 --- a/release/scripts/modules/bpy/utils/__init__.py +++ b/release/scripts/modules/bpy/utils/__init__.py @@ -72,6 +72,7 @@ import addon_utils as _addon_utils _user_preferences = _bpy.context.user_preferences _script_module_dirs = "startup", "modules" +_is_factory_startup = _bpy.app.factory_startup def _test_import(module_name, loaded_modules): @@ -145,6 +146,7 @@ def load_scripts(reload_scripts=False, refresh_scripts=False): :type refresh_scripts: bool """ use_time = use_class_register_check = _bpy.app.debug_python + use_user = not _is_factory_startup if use_time: import time @@ -235,7 +237,7 @@ def load_scripts(reload_scripts=False, refresh_scripts=False): from bpy_restrict_state import RestrictBlend with RestrictBlend(): - for base_path in script_paths(): + for base_path in script_paths(use_user=use_user): for path_subdir in _script_module_dirs: path = _os.path.join(base_path, path_subdir) if _os.path.isdir(path): @@ -307,7 +309,7 @@ def script_path_pref(): return _os.path.normpath(path) if path else None -def script_paths(subdir=None, user_pref=True, check_all=False): +def script_paths(subdir=None, user_pref=True, check_all=False, use_user=True): """ Returns a list of valid script paths. @@ -331,13 +333,24 @@ def script_paths(subdir=None, user_pref=True, check_all=False): if check_all: # All possible paths, no duplicates, keep order. - base_paths = ( - *(path for path in (_os.path.join(resource_path(res), "scripts") - for res in ('LOCAL', 'USER', 'SYSTEM')) if path not in base_paths), - *base_paths, - ) + if use_user: + test_paths = ('LOCAL', 'USER', 'SYSTEM') + else: + test_paths = ('LOCAL', 'SYSTEM') - for path in (*base_paths, script_path_user(), script_path_pref()): + base_paths = ( + *(path for path in ( + _os.path.join(resource_path(res), "scripts") + for res in test_paths) if path not in base_paths), + *base_paths, + ) + + if use_user: + test_paths = (*base_paths, script_path_user(), script_path_pref()) + else: + test_paths = (*base_paths, script_path_pref()) + + for path in test_paths: if path: path = _os.path.normpath(path) if path not in scripts and _os.path.isdir(path): diff --git a/source/blender/python/intern/bpy_app.c b/source/blender/python/intern/bpy_app.c index 8b3464173d2..e47bf21f04b 100644 --- a/source/blender/python/intern/bpy_app.c +++ b/source/blender/python/intern/bpy_app.c @@ -89,6 +89,7 @@ static PyStructSequence_Field app_info_fields[] = { {(char *)"version_cycle", (char *)"The release status of this build alpha/beta/rc/release"}, {(char *)"binary_path", (char *)"The location of blenders executable, useful for utilities that spawn new instances"}, {(char *)"background", (char *)"Boolean, True when blender is running without a user interface (started with -b)"}, + {(char *)"factory_startup", (char *)"Boolean, True when blender is running with --factory-startup)"}, /* buildinfo */ {(char *)"build_date", (char *)"The date this blender instance was built"}, @@ -165,6 +166,7 @@ static PyObject *make_app_info(void) SetStrItem(STRINGIFY(BLENDER_VERSION_CYCLE)); SetStrItem(BKE_appdir_program_path()); SetObjItem(PyBool_FromLong(G.background)); + SetObjItem(PyBool_FromLong(G.factory_startup)); /* build info, use bytes since we can't assume _any_ encoding: * see patch [#30154] for issue */