Re: retrieve traceback in embedded python of Python3.0?

Discussion in 'Python' started by BigHand, Mar 7, 2009.

  1. BigHand

    BigHand Guest

    On Mar 7, 11:40 am, BigHand <> wrote:
    > Guys:
    > I have a embedded python in MFC app. to execute a py script of a.py,
    > the is only one line in a.py, it "a()" , normally ,excute this script
    > file ,you will get a
    >  "the exception type is<class 'NameError'> "
    > "The exception value is name 'a' is not defined "
    >
    > Python3.0 with VS2005.
    > here is the brief code:
    > Py_Initialize();
    > PyObject *m, *d, *v;
    > m = PyImport_AddModule("__main__");
    > d = PyModule_GetDict(m);
    > v = PyRun_File(fp, pStr, Py_file_input, d, d);       //~~~the py
    > script is a.py
    > PyObject *exc_type = NULL, *exc_value = NULL, *exc_tb = NULL;
    > PyErr_Fetch(&exc_type, &exc_value, &exc_tb);  //~~~after fetch , the
    > exc_type, exc_value, exc_tb are not "NULL"
    > PyObject * modTB = PyImport_ImportModule("traceback");
    > PyObject* pyUStr = PyUnicode_FromString("format_exception");
    > PyObject* listTB = PyObject_CallMethodObjArgs(modTB,  pyUStr,
    > exc_type, exc_value, exc_tb, NULL);
    >
    > in the PyObject_CallMethodObjArgs(modTB,  pyUStr, exc_type, exc_value,
    > exc_tb, NULL), I get modTB, pyUStr, exc_type, exc_value, exc_tb are
    > not NULL, but the listTB is always NULL, I can retrieve the list...
    >
    > any body could enlight me?


    it's "I can't retrieve the traceback list."
    BigHand, Mar 7, 2009
    #1
    1. Advertising

  2. En Sat, 07 Mar 2009 01:43:05 -0200, BigHand <> escribió:
    > On Mar 7, 11:40 am, BigHand <> wrote:
    >> Guys:
    >> I have a embedded python in MFC app. to execute a py script of a.py,
    >> the is only one line in a.py, it "a()" , normally ,excute this script
    >> file ,you will get a
    >>  "the exception type is<class 'NameError'> "
    >> "The exception value is name 'a' is not defined "
    >>
    >> Python3.0 with VS2005.
    >> here is the brief code:
    >> Py_Initialize();
    >> PyObject *m, *d, *v;
    >> m = PyImport_AddModule("__main__");
    >> d = PyModule_GetDict(m);
    >> v = PyRun_File(fp, pStr, Py_file_input, d, d);       //~~~the py
    >> script is a.py
    >> PyObject *exc_type = NULL, *exc_value = NULL, *exc_tb = NULL;
    >> PyErr_Fetch(&exc_type, &exc_value, &exc_tb);  //~~~after fetch , the
    >> exc_type, exc_value, exc_tb are not "NULL"
    >> PyObject * modTB = PyImport_ImportModule("traceback");
    >> PyObject* pyUStr = PyUnicode_FromString("format_exception");
    >> PyObject* listTB = PyObject_CallMethodObjArgs(modTB,  pyUStr,
    >> exc_type, exc_value, exc_tb, NULL);
    >>
    >> in the PyObject_CallMethodObjArgs(modTB,  pyUStr, exc_type, exc_value,
    >> exc_tb, NULL), I get modTB, pyUStr, exc_type, exc_value, exc_tb are
    >> not NULL, but the listTB is always NULL, I can retrieve the list...
    >>
    >> any body could enlight me?

    >
    > it's "I can't retrieve the traceback list."


    PyErr_Print or PyTraceback_Print aren't suitable for you? Those functions
    are much easier to use from C code, while the traceback module is intended
    to be used in Python code.

    --
    Gabriel Genellina
    Gabriel Genellina, Mar 7, 2009
    #2
    1. Advertising

  3. BigHand

    BigHand Guest

    On Mar 7, 9:22 pm, "Gabriel Genellina" <> wrote:
    > En Sat, 07 Mar 2009 01:43:05 -0200, BigHand <> escribió:
    >
    >
    >
    > > On Mar 7, 11:40 am, BigHand <> wrote:
    > >> Guys:
    > >> I have a embedded python in MFC app. to execute a py script of a.py,
    > >> the is only one line in a.py, it "a()" , normally ,excute this script
    > >> file ,you will get a
    > >>  "the exception type is<class 'NameError'> "
    > >> "The exception value is name 'a' is not defined "

    >
    > >> Python3.0 with VS2005.
    > >> here is the brief code:
    > >> Py_Initialize();
    > >> PyObject *m, *d, *v;
    > >> m = PyImport_AddModule("__main__");
    > >> d = PyModule_GetDict(m);
    > >> v = PyRun_File(fp, pStr, Py_file_input, d, d);       //~~~the py
    > >> script is a.py
    > >> PyObject *exc_type = NULL, *exc_value = NULL, *exc_tb = NULL;
    > >> PyErr_Fetch(&exc_type, &exc_value, &exc_tb);  //~~~after fetch , the
    > >> exc_type, exc_value, exc_tb are not "NULL"
    > >> PyObject * modTB = PyImport_ImportModule("traceback");
    > >> PyObject* pyUStr = PyUnicode_FromString("format_exception");
    > >> PyObject* listTB = PyObject_CallMethodObjArgs(modTB,  pyUStr,
    > >> exc_type, exc_value, exc_tb, NULL);

    >
    > >> in the PyObject_CallMethodObjArgs(modTB,  pyUStr, exc_type, exc_value,
    > >> exc_tb, NULL), I get modTB, pyUStr, exc_type, exc_value, exc_tb are
    > >> not NULL, but the listTB is always NULL, I can retrieve the list...

    >
    > >> any body could enlight me?

    >
    > > it's "I can't retrieve the traceback list."

    >
    > PyErr_Print or PyTraceback_Print aren't suitable for you? Those functions
    > are much easier to use from C code, while the traceback module is intended
    > to be used in Python code.
    >
    > --
    > Gabriel Genellina


    Hello,Gabriel,

    I gave up using the format_exception, and use calling the python
    fucntion of print_tb.
    BigHand, Mar 9, 2009
    #3
    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. Skink

    traceback from embedded python

    Skink, Sep 17, 2005, in forum: Python
    Replies:
    0
    Views:
    398
    Skink
    Sep 17, 2005
  2. BigHand
    Replies:
    0
    Views:
    196
    BigHand
    Mar 7, 2009
  3. Jack Bates
    Replies:
    0
    Views:
    276
    Jack Bates
    May 2, 2011
  4. Andrew Berg
    Replies:
    0
    Views:
    330
    Andrew Berg
    Jun 16, 2012
  5. Steven D'Aprano
    Replies:
    1
    Views:
    61
    Steven D'Aprano
    Apr 28, 2014
Loading...

Share This Page