diff options
author | Campbell Barton <ideasman42@gmail.com> | 2009-03-19 01:22:58 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2009-03-19 01:22:58 +0300 |
commit | 3aab50f775e4928d3ee525a999e2b4c995ae9ae4 (patch) | |
tree | 68c902e2336dd3e12f41e85cda9ade1f7025e9a0 /source/blender/python/intern/bpy_util.c | |
parent | 43d4e3fa7efca3ae2cedfd35fa344bf2ef298a7f (diff) |
* removed warnings and fixed some python refcount errors
* operator class names
- Changed 'name' to '__label__' (since __name__ is already used for the class name)
- Changed 'properties' to '__props__'
* added a PyObject_GetAttrStringArgs(), utility function which Id like to see in pythons C api.
PyObject_GetAttrStringArgs(pyob, "someattr", "foo", "bar") /* pyob.someattr.foo.bar */
Diffstat (limited to 'source/blender/python/intern/bpy_util.c')
-rw-r--r-- | source/blender/python/intern/bpy_util.c | 80 |
1 files changed, 54 insertions, 26 deletions
diff --git a/source/blender/python/intern/bpy_util.c b/source/blender/python/intern/bpy_util.c index 724c99098f4..47ac739eac0 100644 --- a/source/blender/python/intern/bpy_util.c +++ b/source/blender/python/intern/bpy_util.c @@ -175,39 +175,67 @@ void PyLineSpit(void) { void BPY_getFileAndNum(char **filename, int *lineno) { PyObject *getframe, *frame; - PyObject *f_lineno, *f_code, *co_filename; + PyObject *f_lineno= NULL, *co_filename= NULL; 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; - } - } + if (getframe==NULL) { + return; + } + + frame = PyObject_CallObject(getframe, NULL); + if (frame==NULL) + return; + + if (filename) { + co_filename= PyObject_GetAttrStringArgs(frame, 1, "f_code", "co_filename"); + if (co_filename==NULL) { + PyErr_SetString(PyExc_SystemError, "Could not access sys._getframe().f_code.co_filename"); + Py_DECREF(frame); + return; } + + *filename = _PyUnicode_AsString(co_filename); + Py_DECREF(co_filename); } - Py_XDECREF(co_filename); - Py_XDECREF(f_lineno); - Py_XDECREF(f_code); - Py_XDECREF(frame); + if (lineno) { + f_lineno= PyObject_GetAttrString(frame, "f_lineno"); + if (f_lineno==NULL) { + PyErr_SetString(PyExc_SystemError, "Could not access sys._getframe().f_lineno"); + Py_DECREF(frame); + return; + } + + *lineno = (int)PyLong_AsSsize_t(f_lineno); + Py_DECREF(f_lineno); + } +} + +/* Would be nice if python had this built in */ +PyObject *PyObject_GetAttrStringArgs(PyObject *o, Py_ssize_t n, ...) +{ + Py_ssize_t i; + PyObject *item= o; + char *attr; + + va_list vargs; + + va_start(vargs, n); + for (i=0; i<n; i++) { + attr = va_arg(vargs, char *); + item = PyObject_GetAttrString(item, attr); + + if (item) + Py_DECREF(item); + else /* python will set the error value here */ + break; + + } + va_end(vargs); - PyErr_SetString(PyExc_SystemError, "Could not access sys._getframe().f_code.co_filename"); + Py_INCREF(item); /* final value has is increfed, to match PyObject_GetAttrString */ + return item; } |