Stack trace in C

Discussion in 'Python' started by Andre Poenitz, Jul 25, 2006.

  1. Bear with me - I am new to Python. (And redirect me to a more suitable
    newsgroup in case this one is not appropriate.)

    I am trying to embed Python into a C++ application and want to get back
    a backtrace in case of errors in the python code.

    This works well with e.g.

    import sys

    def u3():
    xxxxxx() # raise an error

    def u2():
    u3()

    def u1():
    u2()

    def f():
    try:
    u1()
    except NameError:
    type, value, tb = sys.exc_info()
    #f = tb.tb_frame
    #while f:
    # print f.f_lineno, f.f_code.co_name
    # f = f.f_back
    #print "==="
    while tb:
    f = tb.tb_frame
    print f.f_lineno, f.f_code.co_name
    tb = tb.tb_next

    def d1():
    f()

    def d2():
    d1()

    d2()


    on the python side.

    However, I want to do that on the C side.


    So far I have code similar to


    std::eek:stringstream msg;

    [...]
    #if 1
    // Branch 1
    PyObject * exc_type = 0;
    PyObject * exc_value = 0;
    PyObject * exc_traceback = 0;
    PyErr_Fetch(&exc_type, &exc_value, &exc_traceback);
    PyObject * tb_frame = PyObject_GetAttrString(exc_traceback, "tb_frame");
    #else
    // Branch 2
    PyObject * pName = PyString_FromString("sys");
    PyObject * pModule = PyImport_Import(pName);
    PyObject * pFunc = PyObject_GetAttrString(pModule, "exc_info");
    PyObject * pResult = PyObject_CallObject(pFunc, NULL);
    PyObject * tb_frame = PySequence_GetItem(pResult, 2);
    #endif
    while (tb_frame != Py_None) {
    PyObject * f_code = PyObject_GetAttrString(tb_frame, "f_code");
    PyObject * f_lineno = PyObject_GetAttrString(tb_frame, "f_lineno");
    int lineno = PyInt_AsLong(f_lineno);
    msg << " in line " << lineno << "\n";
    PyObject * tmp1 = PyObject_GetAttrString(tb_frame, "f_back");
    PyObject * tmp2 = PyObject_GetAttrString(tb_frame, "tb_next");
    // Now, neither tmp1 nor tmp2 is usable
    // tb_frame = tmpX;
    }
    }


    [Plus some reference counting/error checking code I left out]

    The 'Branch 1' version works for a sinmgle frame, but then tmp1 will be
    None and tmp2 0 at the end of the iteration and so no further frames
    are found.

    The 'Branch 2' version creates immediatly tb_frame as None and
    does not even enter the loop body once.

    So what am I doing wrong? How do I get the equivalent of the python
    code in C?

    Andre'
     
    Andre Poenitz, Jul 25, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rukmal Fernando
    Replies:
    4
    Views:
    862
    Richard Grimes [MVP]
    Oct 27, 2003
  2. Matthias S.
    Replies:
    1
    Views:
    666
    Daniel Fisher\(lennybacon\)
    Nov 30, 2005
  3. Jiho Han
    Replies:
    0
    Views:
    531
    Jiho Han
    Aug 22, 2006
  4. =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=

    Adding my messages to the trace.axd trace?

    =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=, Apr 6, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    602
    =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=
    Apr 6, 2007
  5. Chris Newby
    Replies:
    0
    Views:
    776
    Chris Newby
    Jul 28, 2008
Loading...

Share This Page