From 7ff1750218bf3c2ef4c57f9ea4a12b738f4b7264 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Mon, 3 Sep 2018 12:38:19 +1000 Subject: PyAPI: add optional imports to expression eval API Avoids having to use `__import__` to access modules. --- source/blender/python/BPY_extern.h | 8 +++-- source/blender/python/generic/py_capi_utils.c | 47 +++++++++++++++++++------- source/blender/python/generic/py_capi_utils.h | 11 +++--- source/blender/python/intern/bpy_interface.c | 18 ++++++---- source/blender/python/intern/bpy_utils_units.c | 2 +- 5 files changed, 59 insertions(+), 27 deletions(-) (limited to 'source/blender/python') 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, "", r_value); + ok = PyC_RunString_AsNumber(imports, expr, "", 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, "", r_value); + ok = PyC_RunString_AsString(imports, expr, "", 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, "", r_value); + ok = PyC_RunString_AsIntPtr(imports, expr, "", 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, "", &result)) { + if (!PyC_RunString_AsNumber(NULL, str, "", &result)) { if (PyErr_Occurred()) { PyErr_Print(); PyErr_Clear(); -- cgit v1.2.3