[C++-sig] Deriving Python function from C++ base class with pure virtual function
Jonathan Brandmeyer
jbrandmeyer at earthlink.net
Thu Dec 2 21:34:15 CET 2004
On Thu, 2004-12-02 at 08:34 -0800, Paul F. Kunz wrote:
> >>>>> On Sun, 14 Nov 2004 17:35:14 -0500, Jonathan Brandmeyer <jbrandmeyer at earthlink.net> said:
>
> > See the PEP 311 interface PyGILState_Ensure()/PyGILState_Release(),
> > which you will use to wrap around the callback function in your C++
> > code. This interface solves the thread bootstrap problem.
> > Furthermore, the lock is recursive, so locking the GIL is safe when
> > you already have the lock.
>
> I tried this with Boost-1.32.0 and Python 2.4 as follows...
>
> double
> FunctionWrap::
> operator () ( double x ) const
> {
> PyGILState_STATE state = PyGILState_Ensure ();
> double value = call_method < double, double > ( m_self, "valueAt", x );
> PyGILState_Release ( state );
>
> return value;
> }
This isn't safe since call_method() can throw, which would prevent
releasing the GIL. I made a small lock class that calls
PyGILState_Ensure() in its constructor, and PyGILState_Release() in the
destructor for this purpose.
> I still get SIGSEGV with different traceback as follows...
>
[snipped]
> Any suggestions?
Execute PyExec_InitThreads() in your module initialization function, and
see if that helps (it does for me). Python does not do this
automatically.
HTH,
-Jonathan
More information about the Cplusplus-sig
mailing list