diff options
author | Campbell Barton <ideasman42@gmail.com> | 2020-08-17 11:16:54 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2020-08-17 11:32:20 +0300 |
commit | 0967935b42bc73e65b910cabf3428485f113f1f5 (patch) | |
tree | 044d366c5a8b2e744cecc5cb12a0b61523c5d0a1 /source/blender/python | |
parent | 7341ceb674b2fc5c01d4328f398516ef8f358ae5 (diff) |
Cleanup: split BPY_run_string_ex into two functions
Using a boolean to select between eval/exec behavior wasn't very
readable.
Diffstat (limited to 'source/blender/python')
-rw-r--r-- | source/blender/python/BPY_extern_run.h | 10 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_interface_run.c | 26 |
2 files changed, 28 insertions, 8 deletions
diff --git a/source/blender/python/BPY_extern_run.h b/source/blender/python/BPY_extern_run.h index 67ac56f2798..5f12ada4ff2 100644 --- a/source/blender/python/BPY_extern_run.h +++ b/source/blender/python/BPY_extern_run.h @@ -37,6 +37,12 @@ bool BPY_run_text(struct bContext *C, struct ReportList *reports, const bool do_jump); +/* Use the 'eval' for simple single-line expressions, + * otherwise 'exec' for full multi-line scripts. */ +bool BPY_run_string_exec(struct bContext *C, const char *imports[], const char *expr); +bool BPY_run_string_eval(struct bContext *C, const char *imports[], const char *expr); + +/* Run, evaluating to fixed type result. */ bool BPY_run_string_as_number(struct bContext *C, const char *imports[], const char *expr, @@ -59,10 +65,6 @@ bool BPY_run_string_as_string(struct bContext *C, const char *report_prefix, char **r_value); -bool BPY_run_string_ex(struct bContext *C, const char *imports[], const char *expr, bool use_eval); - -bool BPY_run_string(struct bContext *C, const char *imports[], const char *expr); - #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/source/blender/python/intern/bpy_interface_run.c b/source/blender/python/intern/bpy_interface_run.c index f936be2edbf..9f9382b633c 100644 --- a/source/blender/python/intern/bpy_interface_run.c +++ b/source/blender/python/intern/bpy_interface_run.c @@ -208,7 +208,14 @@ bool BPY_run_text(bContext *C, struct Text *text, struct ReportList *reports, co return python_script_exec(C, NULL, text, reports, do_jump); } -bool BPY_run_string_ex(bContext *C, const char *imports[], const char *expr, bool use_eval) +/** + * \param mode: Passed to #PyRun_String, matches Python's `compile` functions mode argument. + * #Py_eval_input for `eval`, #Py_file_input for `exec`. + */ +static bool bpy_run_string_impl(bContext *C, + const char *imports[], + const char *expr, + const int mode) { BLI_assert(expr); PyGILState_STATE gilstate; @@ -231,7 +238,7 @@ bool BPY_run_string_ex(bContext *C, const char *imports[], const char *expr, boo retval = NULL; } else { - retval = PyRun_String(expr, use_eval ? Py_eval_input : Py_file_input, py_dict, py_dict); + retval = PyRun_String(expr, mode, py_dict, py_dict); } if (retval == NULL) { @@ -249,9 +256,20 @@ bool BPY_run_string_ex(bContext *C, const char *imports[], const char *expr, boo return ok; } -bool BPY_run_string(bContext *C, const char *imports[], const char *expr) +/** + * Run an expression, matches: `exec(compile(..., "eval"))` + */ +bool BPY_run_string_eval(bContext *C, const char *imports[], const char *expr) +{ + return bpy_run_string_impl(C, imports, expr, Py_eval_input); +} + +/** + * Run an entire script, matches: `exec(compile(..., "exec"))` + */ +bool BPY_run_string_exec(bContext *C, const char *imports[], const char *expr) { - return BPY_run_string_ex(C, imports, expr, true); + return bpy_run_string_impl(C, imports, expr, Py_file_input); } /** \} */ |