diff options
author | Campbell Barton <ideasman42@gmail.com> | 2018-08-31 07:21:32 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2018-08-31 07:21:32 +0300 |
commit | 98800aa4e0f7b4aa169e799e804b1ba658be6e69 (patch) | |
tree | 213282115533eca6c53d2c0f7bd864aec11b2e60 /source/blender/python/generic/py_capi_utils.c | |
parent | 18d135d05c53885eb7460e1c95cc8ebbdb771102 (diff) |
C/Python API: Add PyC_RunString_AsIntPtr
Utility to get an int or pointer from a Python expression.
Diffstat (limited to 'source/blender/python/generic/py_capi_utils.c')
-rw-r--r-- | source/blender/python/generic/py_capi_utils.c | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/source/blender/python/generic/py_capi_utils.c b/source/blender/python/generic/py_capi_utils.c index d2d78167fae..d8d6b5c7ce7 100644 --- a/source/blender/python/generic/py_capi_utils.c +++ b/source/blender/python/generic/py_capi_utils.c @@ -784,7 +784,7 @@ bool PyC_IsInterpreterActive(void) } /* Would be nice if python had this built in - * See: http://wiki.blender.org/index.php/Dev:Doc/Tools/Debugging/PyFromC + * See: https://wiki.blender.org/wiki/Tools/Debugging/PyFromC */ void PyC_RunQuicky(const char *filepath, int n, ...) { @@ -1140,6 +1140,40 @@ 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) +{ + 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 { + intptr_t val; + + val = (intptr_t)PyLong_AsVoidPtr(retval); + if (val == 0 && PyErr_Occurred()) { + ok = false; + } + else { + *r_value = val; + } + + Py_DECREF(retval); + } + + PyC_MainModule_Restore(main_mod); + + return ok; +} + bool PyC_RunString_AsString(const char *expr, const char *filename, char **r_value) { PyObject *py_dict, *retval; |