Getting params in callback function.

Stephen Kellett snail at objmedia.demon.co.uk
Fri Mar 5 09:29:15 EST 2004


Hello everyone. I've written a tracing function in C which is called
when Python executes each function and line and exceptions etc.

I don't know much about Python (I've been using assembly, C++ and Java
in varying amounts since 1983), so I'd very much appreciate some help
trying to get this information. If this is not the correct newsgroup to
ask this question, please suggest an alternative.

How can I determine what the parameters to a function are when the
callback is called when Python enters a function?

I've included a code snippet below for clarity.

int pythonCallback(PyObject *obj,
                 PyFrameObject *frame,
                 int            what,
                 PyObject       *arg)
{
        switch(what)
        {
        case PyTrace_CALL:
                {
                        // entering a function, arg is always NULL

                        // my attempt at getting the params
                        // I thought they may be here, but this code
fails
                        // I initially thought of getting the type name
                        // and if that worked I could move on to getting
                        // the value, but even getting the type name
failed

                        int     i;

                        wprintf(_T("Num locals: %d\n"),
frame->f_nlocals);
                        for(i = 0; i < frame->f_nlocals; i++)
                        {
                                PyObject        *po;
                                char            *objTypeName;

                                po = frame->f_localsplus[i];
                                objTypeName = po->ob_type->tp_name;
                                wprintf(_T("Local %d: type: %s\n"), i,
                                        objTypeName);
                        }
                }
                break;

        case PyTrace_EXCEPTION:
                {
                        // exiting a function via an exception
                        // arg is Exception information as returned by
                        // sys.exc_info()
                }
                break;

        case PyTrace_LINE:
                {
                        // line number event, arg is always NULL
                }
                break;

        case PyTrace_RETURN:
                {
                        // leaving a function
                        // arg is Value being returned to the caller
                }
                break;

        default:
                {
                        // unknown operation
                }
                break;
        }

        return 0;
}

Thank you for reading, hope you can shed some light on this.

Stephen
-- 
Stephen Kellett
Object Media Limited    http://www.objmedia.demon.co.uk
RSI Information:        http://www.objmedia.demon.co.uk/rsi.html



More information about the Python-list mailing list