Commit: 7efac2b0b09d9e76d9fc573ec7936a1c6440c067 Author: Campbell Barton Date: Sat Nov 10 10:55:04 2018 +1100 Branches: master https://developer.blender.org/rB7efac2b0b09d9e76d9fc573ec7936a1c6440c067
PyAPI: add imports arg to BPY_execute_string Allows for avoiding `__import__` in expressions, was already supported for BPY_execute_string_as_* API calls. =================================================================== M source/blender/editors/space_script/script_edit.c M source/blender/freestyle/intern/system/PythonInterpreter.h M source/blender/python/BPY_extern.h M source/blender/python/intern/bpy_interface.c M source/blender/windowmanager/intern/wm_files.c M source/creator/creator_args.c =================================================================== diff --git a/source/blender/editors/space_script/script_edit.c b/source/blender/editors/space_script/script_edit.c index 6bfb51d07c6..5a761d1cabf 100644 --- a/source/blender/editors/space_script/script_edit.c +++ b/source/blender/editors/space_script/script_edit.c @@ -125,7 +125,7 @@ static int script_reload_exec(bContext *C, wmOperator *op) /* TODO, this crashes on netrender and keying sets, need to look into why * disable for now unless running in debug mode */ WM_cursor_wait(1); - BPY_execute_string(C, "__import__('bpy').utils.load_scripts(reload_scripts=True)"); + BPY_execute_string(C, (const char *[]){"bpy", NULL}, "bpy.utils.load_scripts(reload_scripts=True)"); WM_cursor_wait(0); WM_event_add_notifier(C, NC_WINDOW, NULL); return OPERATOR_FINISHED; diff --git a/source/blender/freestyle/intern/system/PythonInterpreter.h b/source/blender/freestyle/intern/system/PythonInterpreter.h index 4f5e94ef7a0..cb49e7718a0 100644 --- a/source/blender/freestyle/intern/system/PythonInterpreter.h +++ b/source/blender/freestyle/intern/system/PythonInterpreter.h @@ -112,7 +112,7 @@ public: BKE_reports_clear(reports); - if (!BPY_execute_string(_context, str.c_str())) { + if (!BPY_execute_string(_context, NULL, str.c_str())) { BPy_errors_to_report(reports); cerr << "\nError executing Python script from PythonInterpreter::interpretString" << endl; cerr << "Name: " << name << endl; diff --git a/source/blender/python/BPY_extern.h b/source/blender/python/BPY_extern.h index 5f43f0bf885..955b1c4e4ed 100644 --- a/source/blender/python/BPY_extern.h +++ b/source/blender/python/BPY_extern.h @@ -80,8 +80,8 @@ bool BPY_execute_string_as_number(struct bContext *C, const char *imports[], con bool BPY_execute_string_as_intptr(struct bContext *C, const char *imports[], const char *expr, const bool verbose, intptr_t *r_value); bool BPY_execute_string_as_string(struct bContext *C, const char *imports[], const char *expr, const bool verbose, char **r_value); -bool BPY_execute_string_ex(struct bContext *C, const char *expr, bool use_eval); -bool BPY_execute_string(struct bContext *C, const char *expr); +bool BPY_execute_string_ex(struct bContext *C, const char *imports[], const char *expr, bool use_eval); +bool BPY_execute_string(struct bContext *C, const char *imports[], const char *expr); void BPY_text_free_code(struct Text *text); void BPY_modules_update(struct bContext *C); // XXX - annoying, need this for pointers that get out of date diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c index 123c938b921..0c7f3a7f68d 100644 --- a/source/blender/python/intern/bpy_interface.c +++ b/source/blender/python/intern/bpy_interface.c @@ -692,7 +692,9 @@ bool BPY_execute_string_as_intptr( return ok; } -bool BPY_execute_string_ex(bContext *C, const char *expr, bool use_eval) +bool BPY_execute_string_ex( + bContext *C, const char *imports[], + const char *expr, bool use_eval) { BLI_assert(expr); PyGILState_STATE gilstate; @@ -714,13 +716,18 @@ bool BPY_execute_string_ex(bContext *C, const char *expr, bool use_eval) bmain_back = bpy_import_main_get(); bpy_import_main_set(CTX_data_main(C)); - retval = PyRun_String(expr, use_eval ? Py_eval_input : Py_file_input, py_dict, py_dict); + if (imports && (!PyC_NameSpace_ImportArray(py_dict, imports))) { + Py_DECREF(py_dict); + retval = NULL; + } + else { + retval = PyRun_String(expr, use_eval ? Py_eval_input : Py_file_input, py_dict, py_dict); + } bpy_import_main_set(bmain_back); if (retval == NULL) { ok = false; - BPy_errors_to_report(CTX_wm_reports(C)); } else { @@ -734,9 +741,11 @@ bool BPY_execute_string_ex(bContext *C, const char *expr, bool use_eval) return ok; } -bool BPY_execute_string(bContext *C, const char *expr) +bool BPY_execute_string( + bContext *C, const char *imports[], + const char *expr) { - return BPY_execute_string_ex(C, expr, true); + return BPY_execute_string_ex(C, imports, expr, true); } void BPY_modules_load_user(bContext *C) diff --git a/source/blender/windowmanager/intern/wm_files.c b/source/blender/windowmanager/intern/wm_files.c index 87ca7310971..ef951010ba5 100644 --- a/source/blender/windowmanager/intern/wm_files.c +++ b/source/blender/windowmanager/intern/wm_files.c @@ -473,10 +473,14 @@ static void wm_file_read_post(bContext *C, const bool is_startup_file, const boo if (use_userdef) { /* Only run when we have a template path found. */ if (BKE_appdir_app_template_any()) { - BPY_execute_string(C, "__import__('bl_app_template_utils').reset()"); + BPY_execute_string( + C, (const char *[]){"bl_app_template_utils", NULL}, + "bl_app_template_utils.reset()"); } /* sync addons, these may have changed from the defaults */ - BPY_execute_string(C, "__import__('addon_utils').reset_all()"); + BPY_execute_string( + C, (const char *[]){"addon_utils", NULL}, + "addon_utils.reset_all()"); } BPY_python_reset(C); addons_loaded = true; diff --git a/source/creator/creator_args.c b/source/creator/creator_args.c index f8fe57b05a2..74bcc129f20 100644 --- a/source/creator/creator_args.c +++ b/source/creator/creator_args.c @@ -1815,7 +1815,7 @@ static int arg_handle_python_expr_run(int argc, const char **argv, void *data) /* workaround for scripts not getting a bpy.context.scene, causes internal errors elsewhere */ if (argc > 1) { bool ok; - BPY_CTX_SETUP(ok = BPY_execute_string_ex(C, argv[1], false)); + BPY_CTX_SETUP(ok = BPY_execute_string_ex(C, NULL, argv[1], false)); if (!ok && app_state.exit_code_on_error.python) { printf("\nError: script failed, expr: '%s', exiting.\n", argv[1]); exit(app_state.exit_code_on_error.python); @@ -1841,7 +1841,7 @@ static int arg_handle_python_console_run(int UNUSED(argc), const char **argv, vo #ifdef WITH_PYTHON bContext *C = data; - BPY_CTX_SETUP(BPY_execute_string(C, "__import__('code').interact()")); + BPY_CTX_SETUP(BPY_execute_string(C, (const char *[]){"code", NULL}, "code.interact()")); return 0; #else @@ -1897,7 +1897,7 @@ static int arg_handle_addons_set(int argc, const char **argv, void *data) BLI_snprintf(str, slen, script_str, argv[1]); BLI_assert(strlen(str) + 1 == slen); - BPY_CTX_SETUP(BPY_execute_string_ex(C, str, false)); + BPY_CTX_SETUP(BPY_execute_string_ex(C, NULL, str, false)); free(str); #else UNUSED_VARS(argv, data); _______________________________________________ Bf-blender-cvs mailing list Bf-blender-cvs@blender.org https://lists.blender.org/mailman/listinfo/bf-blender-cvs