Modifying "traceback" objects

Michael Hudson mwh21 at cam.ac.uk
Tue Apr 11 07:22:00 EDT 2000


Randall Hopper <aa8vb at yahoo.com> writes:

>      Is it possible to add frames to the current traceback object from
> inside a C extension?

I'd say it would be difficult.  What would you put in the "f_code" field?

Would

PyThreadState *tstate = PyThreadState_GET();
PyFrameObject* f = PyFrame_New(
		tstate,			/*back*/
		co,			/*code*/
		globals,		/*globals*/
		locals);		/*locals*/
if (f == NULL)
	return NULL;

tstate->frame = f;

be sufficient? (If you can cook up values for co, globals & locals).

Maybe:

int
PushFrame(char* func_name,char* filename,int lineno)
{
    PyThreadState *tstate = PyThreadState_GET();
    PyFrameObject* f;
    PyObject *emptytuple;
    PyObject *emptydict;
    PyObject *co;

    emptydict = PyDict_New();
    emptytuple = PyTuple_New(0);

    co = PyCode_New(0,0,0,0,"",
                    emptytuple,emptytuple,emptytuple,
                    filename,func_name,lineno,"");

    if (co == NULL) return 0;

    f = PyFrame_New(
                tstate,                 /*back*/
                co,                     /*code*/
                emptydict,              /*globals*/
                emptydict);             /*locals*/

    Py_DECREF(emptydict);
    Py_DECREF(emptytuple);
    Py_DECREF(emptytuple);

    if (f == NULL) return 0;

    tstate->frame = f;

    return 0;
}

to be used like:

    ...
    if (!PushFrame("frob",__FILE__,__LINE__)) return NULL;
    ...

though the above is monumentally untested (does it compile? I don't
know!).

Cheers,
M.

-- 
  On the other hand,  the following areas are subject to boycott
  in reaction to the rampant impurity of design or execution, as
  determined after a period of study, in no particular order: 
    ...                            http://www.naggum.no/profile.html



More information about the Python-list mailing list