PyGILState_Release + Python2.3 = Segmentation Fault

K

Kirill Simonov

Hi,

Could someone tell me why my extension module works under Python 2.4, but
fails with Segmentation Fault under Python 2.3? Here is the stripped version:

================================================
#include <Python.h>

static PyObject *
test_gil(PyObject *self)
{
PyGILState_STATE gs;

Py_BEGIN_ALLOW_THREADS

gs = PyGILState_Ensure();

PyGILState_Release(gs);

Py_END_ALLOW_THREADS

Py_INCREF(Py_None);
return Py_None;
}

static PyMethodDef test_gil_methods[] = {
{"test_gil", (PyCFunction)test_gil, METH_NOARGS},
{NULL}
};

PyMODINIT_FUNC
init_test_gil(void) {
(void)Py_InitModule("_test_gil", test_gil_methods);
}
===============================================

I've tested it with Debian Sid and Ubuntu Breezy.

Thanks,
Kirill
 
C

cadavis2k2

Kirill said:
Hi,

Could someone tell me why my extension module works under Python 2.4, but
fails with Segmentation Fault under Python 2.3? Here is the stripped version:

Maybe Python threads aren't initialized? Adding a call to
PyEval_InitThreads() stops the seg fault for me in Python 2.3 on
Debian:

#include <Python.h>

static PyObject *
test_gil(PyObject *self)
{
PyGILState_STATE gs;


Py_BEGIN_ALLOW_THREADS


gs = PyGILState_Ensure();


PyGILState_Release(gs);


Py_END_ALLOW_THREADS


Py_INCREF(Py_None);
return Py_None;
}


static PyMethodDef test_gil_methods[] = {
{"test_gil", (PyCFunction)test_gil, METH_NOARGS},
{NULL}


};


PyMODINIT_FUNC
init_test_gil(void) {

/* This added */
PyEval_InitThreads();

(void)Py_InitModule("_test_gil", test_gil_methods);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top