[Numpy-discussion] minor threads-related issue in numpy-release 1.6.2 and numpy-dev

Nathaniel Smith njs at pobox.com
Tue Aug 14 10:23:59 EDT 2012

On Tue, Aug 14, 2012 at 3:13 PM, Charles R Harris
<charlesr.harris at gmail.com> wrote:
> On Tue, Aug 14, 2012 at 7:47 AM, Aron Ahmadia <aron at ahmadia.net> wrote:
>> Hi all,
>> Installing numpy 1.6.2 against a Python interpreter built with the
>> --without threads currently fails due to missing references to
>> PyGILState_Ensure and PyGILState_Release.  The references appear to be
>> coming from the following code in nditer.c.src:
>> NpyIter_DebugPrint(NpyIter *iter)
>> {
>> // <snip>
>>     PyGILState_STATE gilstate = PyGILState_Ensure();
>> // <snip>
>>     PyGILState_Release(gilstate);
>> }
>> Since this is debugging code, I'm guessing it doesn't get called very
>> frequently, and I could probably just #ifdef it out or use the NPY macros
>> for grabbing the GIL for a non-threaded build: (NPY_ALLOW_C_API and
>> NPY_DISABLE_C_API), but I don't understand why it's grabbing the GIL in the
>> first place.  Where is it calling into the interpreter?  Does it need the
>> GIL for something else?  I'm hesitant to touch this code and issue a pull
>> request until I understand what it's trying to do.
>> Heading on over to the master branch at numpy/numpy, I'm starting to
>> notice more unprotected PyGILState references creeping into the development
>> code.  Even the Python developers seem to think that nobody is using
>> --without-threaded, so I'm not going to make a strong case for being more
>> careful, but I do want to point it out in case you want to keep the numpy
>> sources correct for this case.
> Some parts of the numpy code release the GIL, so it needs to be reacquired
> in order to call the Python C_API. A quick look shows a call to
> PyObject_Print in the function and there may be other such calls. The right
> fix is probably to use a couple of ifdefs if there is an easy way to
> determine the Python interpreter configuration <looks>... WITH_THREAD seems
> to be the right flag.
> #ifdef WITH_THREAD
> PyAPI_FUNC(void) PyThreadState_DeleteCurrent(void);
> #endif
> Could you give that a shot? TIA,

The NPY_*_THREADS macros appear to already check for WITH_THREAD
automatically. I think we'd be happy to apply a patch that cleans up
numpy's direct PyGIL_* calls to use the NPY_*_THREADS macros instead.


More information about the NumPy-Discussion mailing list