[C++-sig] PyErr_Print() causing segmentation fault

Patrick Hartling patrick at vrac.iastate.edu
Tue Jun 24 22:49:53 CEST 2003

I am trying to make the bridge between my Python and C++ code more 
robust, and I have run into a strange problem with PyErr_Print().  In my 
Boost.Python code, I have several wrapper classes for dealing with 
virtual functions.  To prevent exceptions from the Python code from 
propogating too far up the call stack, I am putting try/catch blocks 
around all calls to boost::python::call<T>().  For example:

class MyClass
    virtual void f() = 0;

struct MyClass_Wrapper : MyClass
    virtual void f()
          call_method<void>(self, "f");

    PyObject* self;

Exposing this class through Boost.Python is working fine, but when an 
exception is thrown by the Python implementation of the f() method, 
PyErr_Print() causes a segmentation fault.  The partial stack trace I get 
is this:

#0  0x080af299 in PyErr_Occurred ()
#1  0x080bbd84 in PyErr_PrintEx ()
#2  0x080bbac0 in PyErr_Print ()

Frame #3 is my module's call to PyErr_Print(), and everything beyond that 
is specific to the software I am exposing to Python.  I realize this 
isn't terribly helpful in this form, and I can dig into the Python 
interpreter code to get more specific information if it would be useful.

This code is executing in a multi-threaded environment, so things are a 
little more complicated than I describe them here.  In particular, I am 
acquiring the Python GIL any time a thread other than the one that 
started the Python interpreter calls from C++ into Python.  I have tested 
calling PyErr_Print() with the GIL acquired and not acquired, but it 
doesn't make any difference.  Based on that, I am thinking that the 
multi-threading aspect isn't a factor, but there is still much I don't 
know about managing threads between Python and C++.

Is there anything specific that prevents me from calling PyErr_Print() 
from within my module?  Is there something better I can use, possibly 
from the boost::python set of functions?  Obviously I can just print 
"Exception caught" or something, but that makes debugging the exceptions 
thrown by the Python code more difficult.


Patrick L. Hartling                     | Research Assistant, VRAC
patrick at vrac.iastate.edu                | 2624 Howe Hall: 1.515.294.4916
http://www.137.org/patrick/             | http://www.vrac.iastate.edu/

More information about the Cplusplus-sig mailing list