pyapi, use direct access to the frame rather then python attributes.

This commit is contained in:
2011-02-22 07:57:18 +00:00
parent 91357ae2ea
commit 1e120098fc
2 changed files with 21 additions and 38 deletions

View File

@@ -21,6 +21,7 @@
*/ */
#include <Python.h> #include <Python.h>
#include <frameobject.h>
#include "py_capi_utils.h" #include "py_capi_utils.h"
@@ -56,35 +57,18 @@ void PyC_LineSpit(void) {
void PyC_FileAndNum(const char **filename, int *lineno) void PyC_FileAndNum(const char **filename, int *lineno)
{ {
PyObject *getframe, *frame; PyFrameObject *frame;
PyObject *f_lineno= NULL, *co_filename= NULL;
if (filename) *filename= NULL; if (filename) *filename= NULL;
if (lineno) *lineno = -1; if (lineno) *lineno = -1;
getframe = PySys_GetObject("_getframe"); // borrowed if (!(frame= PyThreadState_GET()->frame)) {
if (getframe==NULL) {
PyErr_Clear();
return;
}
frame = PyObject_CallObject(getframe, NULL);
if (frame==NULL) {
PyErr_Clear();
return; return;
} }
/* when executing a script */ /* when executing a script */
if (filename) { if (filename) {
co_filename= PyC_Object_GetAttrStringArgs(frame, 2, "f_code", "co_filename"); *filename = _PyUnicode_AsString(frame->f_code->co_filename);
if (co_filename==NULL) {
PyErr_SetString(PyExc_RuntimeError, "Could not access sys._getframe().f_code.co_filename");
Py_DECREF(frame);
return;
}
*filename = _PyUnicode_AsString(co_filename);
Py_DECREF(co_filename);
} }
/* when executing a module */ /* when executing a module */
@@ -105,20 +89,9 @@ void PyC_FileAndNum(const char **filename, int *lineno)
} }
} }
if (lineno) { if (lineno) {
f_lineno= PyObject_GetAttrString(frame, "f_lineno"); *lineno = PyFrame_GetLineNumber(frame);
if (f_lineno==NULL) {
PyErr_SetString(PyExc_RuntimeError, "Could not access sys._getframe().f_lineno");
Py_DECREF(frame);
return;
} }
*lineno = (int)PyLong_AsSsize_t(f_lineno);
Py_DECREF(f_lineno);
}
Py_DECREF(frame);
} }
/* Would be nice if python had this built in */ /* Would be nice if python had this built in */

View File

@@ -3937,6 +3937,16 @@ static PyObject * pyrna_func_call(PyObject *self, PyObject *args, PyObject *kw)
return NULL; return NULL;
} }
/* for testing */
/*
{
const char *fn;
int lineno;
PyC_FileAndNum(&fn, &lineno);
printf("pyrna_func_call > %.200s.%.200s : %.200s:%d\n", RNA_struct_identifier(self_ptr->type), RNA_function_identifier(self_func), fn, lineno);
}
*/
/* include the ID pointer for pyrna_param_to_py() so we can include the /* include the ID pointer for pyrna_param_to_py() so we can include the
* ID pointer on return values, this only works when returned values have * ID pointer on return values, this only works when returned values have
* the same ID as the functions. */ * the same ID as the functions. */