[C++-sig] debuging

David Abrahams dave at boost-consulting.com
Mon Nov 25 17:59:27 CET 2002


"Bjorn Pettersen" <BPettersen at NAREX.com> writes:

>> From: Mike Rovner [mailto:mike at bindkey.com] 
>> 
>> Howdy, folks.
>> 
>> How do you debug BPL applications?
>> 
>> I just got 'TypeError: bad argument type for built-in 
>> operation' and have absolutely no idea what to do next.
>
> Sounds like you need to get the traceback... Here's how I do it (you'll
> probably want to modify it to throw a more standard exception ;-)

Wow, yeah! Throwing the result of a new-expression is a very bad
practice!

Anyway, I'd like to use a modified version of this code in the
Boost.Python tests if you don't mind...

OK with you, Bjorn?

> -- bjorn
>
>     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)
>             for line in lst:
>                 result += line
>         */
>         PyObject* tbstr = PyString_FromString("traceback");
>         PyObject* tbmod = PyImport_Import(tbstr);
>         if (!tbmod) throw new NException("Unable to import traceback
> module. Is your Python installed?", N_EX_NULL);
>         PyObject* tbdict = PyModule_GetDict(tbmod);
>         PyObject* formatFunc = PyDict_GetItemString(tbdict,
> "format_exception");
>         if (!formatFunc) throw new NException("Can't find
> traceback.format_exception", N_EX_NULL);
>         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;
>     }

-- 
                       David Abrahams
   dave at boost-consulting.com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution





More information about the Cplusplus-sig mailing list