diff options
author | Campbell Barton <ideasman42@gmail.com> | 2018-09-03 05:38:19 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2018-09-03 05:43:43 +0300 |
commit | 7ff1750218bf3c2ef4c57f9ea4a12b738f4b7264 (patch) | |
tree | 613c4796c351d6de824bc69716963e21e3817190 | |
parent | 4bb8dba340e0ac570f4a4eb628d0be09c92713b7 (diff) |
PyAPI: add optional imports to expression eval API
Avoids having to use `__import__` to access modules.
-rw-r--r-- | source/blender/editors/interface/interface.c | 4 | ||||
-rw-r--r-- | source/blender/editors/util/numinput.c | 2 | ||||
-rw-r--r-- | source/blender/python/BPY_extern.h | 8 | ||||
-rw-r--r-- | source/blender/python/generic/py_capi_utils.c | 47 | ||||
-rw-r--r-- | source/blender/python/generic/py_capi_utils.h | 11 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_interface.c | 18 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_utils_units.c | 2 |
7 files changed, 62 insertions, 30 deletions
diff --git a/source/blender/editors/interface/interface.c b/source/blender/editors/interface/interface.c index 82ae4de07af..a0ea5bbfe19 100644 --- a/source/blender/editors/interface/interface.c +++ b/source/blender/editors/interface/interface.c @@ -2369,7 +2369,7 @@ static bool ui_set_but_string_eval_num_unit(bContext *C, uiBut *but, const char str_unit_convert, sizeof(str_unit_convert), but->drawstr, ui_get_but_scale_unit(but, 1.0), but->block->unit->system, RNA_SUBTYPE_UNIT_VALUE(unit_type)); - return BPY_execute_string_as_number(C, str_unit_convert, true, r_value); + return BPY_execute_string_as_number(C, NULL, str_unit_convert, true, r_value); } #endif /* WITH_PYTHON */ @@ -2384,7 +2384,7 @@ bool ui_but_string_set_eval_num(bContext *C, uiBut *but, const char *str, double if (str[0] != '\0') { bool is_unit_but = (ui_but_is_float(but) && ui_but_is_unit(but)); /* only enable verbose if we won't run again with units */ - if (BPY_execute_string_as_number(C, str, is_unit_but == false, r_value)) { + if (BPY_execute_string_as_number(C, NULL, str, is_unit_but == false, r_value)) { /* if the value parsed ok without unit conversion this button may still need a unit multiplier */ if (is_unit_but) { char str_new[128]; diff --git a/source/blender/editors/util/numinput.c b/source/blender/editors/util/numinput.c index a139f0e3c87..f25bc6350c1 100644 --- a/source/blender/editors/util/numinput.c +++ b/source/blender/editors/util/numinput.c @@ -498,7 +498,7 @@ bool handleNumInput(bContext *C, NumInput *n, const wmEvent *event) n->unit_sys, n->unit_type[idx]); /* Note: with angles, we always get values as radians here... */ - if (BPY_execute_string_as_number(C, str_unit_convert, false, &val)) { + if (BPY_execute_string_as_number(C, NULL, str_unit_convert, false, &val)) { n->val[idx] = (float)val; n->val_flag[idx] &= ~NUM_INVALID; } diff --git a/source/blender/python/BPY_extern.h b/source/blender/python/BPY_extern.h index 69a64793ba4..5f43f0bf885 100644 --- a/source/blender/python/BPY_extern.h +++ b/source/blender/python/BPY_extern.h @@ -75,9 +75,11 @@ void BPY_thread_restore(BPy_ThreadStatePtr tstate); bool BPY_execute_filepath(struct bContext *C, const char *filepath, struct ReportList *reports); bool BPY_execute_text(struct bContext *C, struct Text *text, struct ReportList *reports, const bool do_jump); -bool BPY_execute_string_as_number(struct bContext *C, const char *expr, const bool verbose, double *r_value); -bool BPY_execute_string_as_intptr(struct bContext *C, const char *expr, const bool verbose, intptr_t *r_value); -bool BPY_execute_string_as_string(struct bContext *C, const char *expr, const bool verbose, char **r_value); + +bool BPY_execute_string_as_number(struct bContext *C, const char *imports[], const char *expr, const bool verbose, double *r_value); +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); diff --git a/source/blender/python/generic/py_capi_utils.c b/source/blender/python/generic/py_capi_utils.c index 327f325f02c..46f80bf79f9 100644 --- a/source/blender/python/generic/py_capi_utils.c +++ b/source/blender/python/generic/py_capi_utils.c @@ -716,6 +716,26 @@ PyObject *PyC_DefaultNameSpace(const char *filename) return PyModule_GetDict(mod_main); } +bool PyC_NameSpace_ImportArray(PyObject *py_dict, const char *imports[]) +{ + for (int i = 0; imports[i]; i++) { + PyObject *name = PyUnicode_FromString(imports[i]); + PyObject *mod = PyImport_ImportModuleLevelObject(name, NULL, NULL, 0, 0); + bool ok = false; + if (mod) { + PyDict_SetItem(py_dict, name, mod); + ok = true; + Py_DECREF(mod); + } + Py_DECREF(name); + + if (!ok) { + return false; + } + } + return true; +} + /* restore MUST be called after this */ void PyC_MainModule_Backup(PyObject **main_mod) { @@ -1076,7 +1096,7 @@ PyObject *PyC_FlagSet_FromBitfield(PyC_FlagSet *items, int flag) * * \note it is caller's responsibility to acquire & release GIL! */ -bool PyC_RunString_AsNumber(const char *expr, const char *filename, double *r_value) +bool PyC_RunString_AsNumber(const char *imports[], const char *expr, const char *filename, double *r_value) { PyObject *py_dict, *mod, *retval; bool ok = true; @@ -1096,9 +1116,10 @@ bool PyC_RunString_AsNumber(const char *expr, const char *filename, double *r_va PyErr_Clear(); } - retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict); - - if (retval == NULL) { + if (imports && (!PyC_NameSpace_ImportArray(py_dict, imports))) { + ok = false; + } + else if ((retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict)) == NULL) { ok = false; } else { @@ -1140,7 +1161,7 @@ bool PyC_RunString_AsNumber(const char *expr, const char *filename, double *r_va return ok; } -bool PyC_RunString_AsIntPtr(const char *expr, const char *filename, intptr_t *r_value) +bool PyC_RunString_AsIntPtr(const char *imports[], const char *expr, const char *filename, intptr_t *r_value) { PyObject *py_dict, *retval; bool ok = true; @@ -1150,9 +1171,10 @@ bool PyC_RunString_AsIntPtr(const char *expr, const char *filename, intptr_t *r_ py_dict = PyC_DefaultNameSpace(filename); - retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict); - - if (retval == NULL) { + if (imports && (!PyC_NameSpace_ImportArray(py_dict, imports))) { + ok = false; + } + else if ((retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict)) == NULL) { ok = false; } else { @@ -1174,7 +1196,7 @@ bool PyC_RunString_AsIntPtr(const char *expr, const char *filename, intptr_t *r_ return ok; } -bool PyC_RunString_AsString(const char *expr, const char *filename, char **r_value) +bool PyC_RunString_AsString(const char *imports[], const char *expr, const char *filename, char **r_value) { PyObject *py_dict, *retval; bool ok = true; @@ -1184,9 +1206,10 @@ bool PyC_RunString_AsString(const char *expr, const char *filename, char **r_val py_dict = PyC_DefaultNameSpace(filename); - retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict); - - if (retval == NULL) { + if (imports && (!PyC_NameSpace_ImportArray(py_dict, imports))) { + ok = false; + } + else if ((retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict)) == NULL) { ok = false; } else { diff --git a/source/blender/python/generic/py_capi_utils.h b/source/blender/python/generic/py_capi_utils.h index 4c1f14f08da..92964fce9d5 100644 --- a/source/blender/python/generic/py_capi_utils.h +++ b/source/blender/python/generic/py_capi_utils.h @@ -77,8 +77,9 @@ const char * PyC_UnicodeAsByte(PyObject *py_str, PyObject **coerce); /* coerc const char * PyC_UnicodeAsByteAndSize(PyObject *py_str, Py_ssize_t *size, PyObject **coerce); /* name namespace function for bpy & bge */ -PyObject * PyC_DefaultNameSpace(const char *filename); -void PyC_RunQuicky(const char *filepath, int n, ...); +PyObject *PyC_DefaultNameSpace(const char *filename); +void PyC_RunQuicky(const char *filepath, int n, ...); +bool PyC_NameSpace_ImportArray(PyObject *py_dict, const char *imports[]); void PyC_MainModule_Backup(PyObject **main_mod); void PyC_MainModule_Restore(PyObject *main_mod); @@ -101,9 +102,9 @@ int PyC_FlagSet_ValueFromID(PyC_FlagSet *item, const char *identifier, int int PyC_FlagSet_ToBitfield(PyC_FlagSet *items, PyObject *value, int *r_value, const char *error_prefix); PyObject *PyC_FlagSet_FromBitfield(PyC_FlagSet *items, int flag); -bool PyC_RunString_AsNumber(const char *expr, const char *filename, double *r_value); -bool PyC_RunString_AsIntPtr(const char *expr, const char *filename, intptr_t *r_value); -bool PyC_RunString_AsString(const char *expr, const char *filename, char **r_value); +bool PyC_RunString_AsNumber(const char **imports, const char *expr, const char *filename, double *r_value); +bool PyC_RunString_AsIntPtr(const char **imports, const char *expr, const char *filename, intptr_t *r_value); +bool PyC_RunString_AsString(const char **imports, const char *expr, const char *filename, char **r_value); int PyC_ParseBool(PyObject *o, void *p); diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c index 9b685d5ba6e..7ca087e4993 100644 --- a/source/blender/python/intern/bpy_interface.c +++ b/source/blender/python/intern/bpy_interface.c @@ -588,7 +588,9 @@ void BPY_DECREF_RNA_INVALIDATE(void *pyob_ptr) /** * \return success */ -bool BPY_execute_string_as_number(bContext *C, const char *expr, const bool verbose, double *r_value) +bool BPY_execute_string_as_number( + bContext *C, const char *imports[], + const char *expr, const bool verbose, double *r_value) { PyGILState_STATE gilstate; bool ok = true; @@ -604,7 +606,7 @@ bool BPY_execute_string_as_number(bContext *C, const char *expr, const bool verb bpy_context_set(C, &gilstate); - ok = PyC_RunString_AsNumber(expr, "<blender button>", r_value); + ok = PyC_RunString_AsNumber(imports, expr, "<expr as number>", r_value); if (ok == false) { if (verbose) { @@ -623,7 +625,9 @@ bool BPY_execute_string_as_number(bContext *C, const char *expr, const bool verb /** * \return success */ -bool BPY_execute_string_as_string(bContext *C, const char *expr, const bool verbose, char **r_value) +bool BPY_execute_string_as_string( + bContext *C, const char *imports[], + const char *expr, const bool verbose, char **r_value) { BLI_assert(r_value && expr); PyGILState_STATE gilstate; @@ -636,7 +640,7 @@ bool BPY_execute_string_as_string(bContext *C, const char *expr, const bool verb bpy_context_set(C, &gilstate); - ok = PyC_RunString_AsString(expr, "<blender button>", r_value); + ok = PyC_RunString_AsString(imports, expr, "<expr as str>", r_value); if (ok == false) { if (verbose) { @@ -657,7 +661,9 @@ bool BPY_execute_string_as_string(bContext *C, const char *expr, const bool verb * * \return success */ -bool BPY_execute_string_as_intptr(bContext *C, const char *expr, const bool verbose, intptr_t *r_value) +bool BPY_execute_string_as_intptr( + bContext *C, const char *imports[], + const char *expr, const bool verbose, intptr_t *r_value) { BLI_assert(r_value && expr); PyGILState_STATE gilstate; @@ -670,7 +676,7 @@ bool BPY_execute_string_as_intptr(bContext *C, const char *expr, const bool verb bpy_context_set(C, &gilstate); - ok = PyC_RunString_AsIntPtr(expr, "<blender button>", r_value); + ok = PyC_RunString_AsIntPtr(imports, expr, "<expr as intptr>", r_value); if (ok == false) { if (verbose) { diff --git a/source/blender/python/intern/bpy_utils_units.c b/source/blender/python/intern/bpy_utils_units.c index 0ef689d1a5a..cbd8b67d1ce 100644 --- a/source/blender/python/intern/bpy_utils_units.c +++ b/source/blender/python/intern/bpy_utils_units.c @@ -204,7 +204,7 @@ static PyObject *bpyunits_to_value(PyObject *UNUSED(self), PyObject *args, PyObj bUnit_ReplaceString(str, (int)str_len, uref, scale, usys, ucat); - if (!PyC_RunString_AsNumber(str, "<bpy_units_api>", &result)) { + if (!PyC_RunString_AsNumber(NULL, str, "<bpy_units_api>", &result)) { if (PyErr_Occurred()) { PyErr_Print(); PyErr_Clear(); |