[Python-Dev] Calling back into python from C

Andrew McNamara andrewm at object-craft.com.au
Tue Jul 24 16:15:02 CEST 2007


I realise I'm going to get slapped for asking a userish question here -
sorry in advance.  I'm looking for an explanation for why things are the
way they are, the doco and py source aren't providing the missing info,
and it looks like I'm bumping into an old Python bug (fixed in r38830
by mwh on 2005-04-18).

I'm working on an C extension that needs to call back into python.
Generally the GIL has been released when I need to do the callback,
but I can't be sure. So I need to save the GIL state, get the lock,
then restore it at the end.

As far as I can tell from the doco, the recommended way to do this is to
use PyGILState_Ensure() and PyGILState_Release(), but prior to r38830,
PyGILState_Release incorrectly used PyEval_ReleaseThread when it
should have been using PyEval_SaveThread() (I think), and the result is
SEGV. This poses a problem, as I need to support Python versions back
to 2.3.

Am I correct in using PyGILState_Ensure() and PyGILState_Release()? If
so, how do I support back to Py 2.3? Copy the current fixed
PyGILState_Release() into my code (ick)?

-- 
Andrew McNamara, Senior Developer, Object Craft
http://www.object-craft.com.au/


More information about the Python-Dev mailing list