[Python-bugs-list] [ python-Bugs-563338 ] Getting traceback in embedded python.

noreply@sourceforge.net noreply@sourceforge.net
Tue, 02 Jul 2002 16:43:17 -0700


Bugs item #563338, was opened at 2002-06-02 02:55
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=563338&group_id=5470

Category: Documentation
Group: Not a Bug
Status: Closed
Resolution: Invalid
Priority: 5
Submitted By: Bjorn Pettersen (bpettersen)
Assigned to: Fred L. Drake, Jr. (fdrake)
Summary: Getting traceback in embedded python.

Initial Comment:
It would be nice if there was an API method for getting 
the traceback as a string for use in embedded Python 
applications. Since I couldn't find any such function I 
created the following, maybe it could be added to the 
embedding docs?

class PyException : public std::exception {
  std::string msg;
public:
  PyException(const std::string& m) : msg(m) {}
  const char* what() { return msg.c_str(); }
};

std::string getTraceback() {
  std::string result;
  PyObject *exception, *v, *traceback;
  PyErr_Fetch(&exception, &v, &traceback);
  PyErr_NormalizeException(&exception, &v, 
&traceback);

  /*
    import traceback
    lst = traceback.format_exception(exception, v, 
traceback)
  */
  PyObject* tbstr = PyString_FromString("traceback");
  PyObject* tbmod = PyImport_Import(tbstr);
  if (!tbmod) 
    throw PyException("Unable to import traceback 
module. Is your Python installed?");
  PyObject* tbdict = PyModule_GetDict(tbmod);
  PyObject* formatFunc = PyDict_GetItemString
(tbdict, "format_exception");
  if (!formatFunc) 
    throw PyException("Can't find 
traceback.format_exception");
  if (!traceback) {
    traceback = Py_None;
    Py_INCREF(Py_None);
  }
  PyObject* args = Py_BuildValue("(OOO)", exception, 
v, traceback);
  PyObject* lst = PyObject_CallObject(formatFunc, 
args);

  for (int i=0; i<PyList_GET_SIZE(lst); i++) {
    result += PyString_AsString(PyList_GetItem(lst, i));
  }

  Py_DECREF(args);
  Py_DECREF(lst);
  return result;
}


----------------------------------------------------------------------

>Comment By: Mark Hammond (mhammond)
Date: 2002-07-03 09:43

Message:
Logged In: YES 
user_id=14198

For the sake of completeness, I am attaching some pure C
code that does something similar.  The code is longer as it
uses CStringIO to extract the final traceback string.  The
key advantage is that it is pure C so could be included in
Python itself.

----------------------------------------------------------------------

Comment By: Fred L. Drake, Jr. (fdrake)
Date: 2002-07-03 07:12

Message:
Logged In: YES 
user_id=3066

Agreed with suggestion that we reject this.

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2002-06-06 05:40

Message:
Logged In: YES 
user_id=6380

I suggest that we reject this reques, but I'll let Fred do that.

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2002-06-02 23:09

Message:
Logged In: YES 
user_id=21627

Another approach is to use PyTraceBack_Print, passing a
file-like object of your choice (e.g. a cStringIO object).

I don't think this belongs into the embedding tutorial -
anybody who understands the API can derive code similar to
yours just from the equivalent Python code.

Instead, I recommend that you add your snippet into the
Python Cookbook, at

http://aspn.activestate.com/ASPN/Python/Cookbook/

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2002-06-02 23:09

Message:
Logged In: YES 
user_id=21627

Another approach is to use PyTraceBack_Print, passing a
file-like object of your choice (e.g. a cStringIO object).

I don't think this belongs into the embedding tutorial -
anybody who understands the API can derive code similar to
yours just from the equivalent Python code.

Instead, I recommend that you add your snippet into the
Python Cookbook, at

http://aspn.activestate.com/ASPN/Python/Cookbook/

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=563338&group_id=5470