diff options
Diffstat (limited to 'source/blender/python/intern/bpy_util.c')
-rw-r--r-- | source/blender/python/intern/bpy_util.c | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/source/blender/python/intern/bpy_util.c b/source/blender/python/intern/bpy_util.c index 63112295d18..31d15d8a69e 100644 --- a/source/blender/python/intern/bpy_util.c +++ b/source/blender/python/intern/bpy_util.c @@ -161,3 +161,43 @@ void PyObSpit(char *name, PyObject *var) { } fprintf(stderr, "\n"); } + +void BPY_getFileAndNum(char **filename, int *lineno) +{ + PyObject *getframe, *frame; + PyObject *f_lineno, *f_code, *co_filename; + + if (filename) *filename= NULL; + if (lineno) *lineno = -1; + + getframe = PySys_GetObject("_getframe"); // borrowed + if (getframe) { + frame = PyObject_CallObject(getframe, NULL); + if (frame) { + f_lineno= PyObject_GetAttrString(frame, "f_lineno"); + f_code= PyObject_GetAttrString(frame, "f_code"); + if (f_lineno && f_code) { + co_filename= PyObject_GetAttrString(f_code, "co_filename"); + if (co_filename) { + + if (filename) *filename = _PyUnicode_AsString(co_filename); + if (lineno) *lineno = (int)PyLong_AsSsize_t(f_lineno); + + Py_DECREF(f_lineno); + Py_DECREF(f_code); + Py_DECREF(co_filename); + Py_DECREF(frame); + + return; + } + } + } + } + + Py_XDECREF(co_filename); + Py_XDECREF(f_lineno); + Py_XDECREF(f_code); + Py_XDECREF(frame); + + PyErr_SetString(PyExc_SystemError, "Could not access sys._getframe().f_code.co_filename"); +} |