diff options
Diffstat (limited to 'source/blender/python/generic/py_capi_utils.c')
-rw-r--r-- | source/blender/python/generic/py_capi_utils.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/source/blender/python/generic/py_capi_utils.c b/source/blender/python/generic/py_capi_utils.c index a2dc27f18e2..2e789d6d4b3 100644 --- a/source/blender/python/generic/py_capi_utils.c +++ b/source/blender/python/generic/py_capi_utils.c @@ -1070,4 +1070,41 @@ bool PyC_RunString_AsNumber(const char *expr, const char *filename, double *r_va return ok; } +bool PyC_RunString_AsString(const char *expr, const char *filename, char **r_value) +{ + PyObject *py_dict, *retval; + bool ok = true; + PyObject *main_mod = NULL; + + PyC_MainModule_Backup(&main_mod); + + py_dict = PyC_DefaultNameSpace(filename); + + retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict); + + if (retval == NULL) { + ok = false; + } + else { + const char *val; + Py_ssize_t val_len; + + val = _PyUnicode_AsStringAndSize(retval, &val_len); + if (val == NULL && PyErr_Occurred()) { + ok = false; + } + else { + char *val_alloc = MEM_mallocN(val_len + 1, __func__); + memcpy(val_alloc, val, val_len + 1); + *r_value = val_alloc; + } + + Py_DECREF(retval); + } + + PyC_MainModule_Restore(main_mod); + + return ok; +} + #endif /* #ifndef MATH_STANDALONE */ |