cimport vs extern --> nogil compat

23 views
Skip to first unread message

Brock Mendel

unread,
Sep 26, 2017, 11:53:04 AM9/26/17
to cython-users
cimporting e.g. PyUnicode_Check using

from cpython cimport PyUnicode_Check 

does not allow PyUnicode_Check to be used in nogil functions, while

cdef extern from "Python.h":
    bint PyUnicode_Check(PyObject obj) nogil

does.  AFAICT this is because the declaration in https://github.com/cython/cython/blob/master/Cython/Includes/cpython/unicode.pxd for PyUnicode_Check does not include the "nogil".  So two questions:

1) Am I doing something unsafe by declaring this function nogil?

2) Is there a way to use the cimport syntax to coerce it to be nogil-ready?

Stefan Behnel

unread,
Sep 26, 2017, 1:32:39 PM9/26/17
to cython...@googlegroups.com
Brock Mendel schrieb am 26.09.2017 um 17:53:
> cimporting e.g. PyUnicode_Check using
>
> from cpython cimport PyUnicode_Check
>
> does not allow PyUnicode_Check to be used in nogil functions, while
>
> cdef extern from "Python.h":
> bint PyUnicode_Check(PyObject obj) nogil
>
> does. AFAICT this is because the declaration
> in https://github.com/cython/cython/blob/master/Cython/Includes/cpython/unicode.pxd
> for PyUnicode_Check does not include the "nogil".

Yes, that's exactly the reason, and it's all that Cython validates here.


> So two questions:
>
> 1) Am I doing something unsafe by declaring this function nogil?

If a C-API function (or macro) is not documented as being callable without
the GIL, the risk is on your side. If nothing else, debug builds of CPython
might inject safety checks into even simple macros that require the GIL. At
the very least, read the sources of all Python implementations and versions
that you want to support, and keep an eye on them in future releases.

OTOH, is there really something you can do with that object after the type
check without holding the GIL?


> 2) Is there a way to use the cimport syntax to coerce it to be nogil-ready?

No, that would be a useless feature. If a function is allowed to be called
without the GIL, it should be declared nogil, and that's it.

Stefan

Brock Mendel

unread,
Sep 26, 2017, 2:51:31 PM9/26/17
to cython-users
OTOH, is there really something you can do with that object after the type 
check without holding the GIL? 
No, but at least in this case I'm looking at PyString_Check, PyUnicode_Check, ..., where there doesn't really need to be anything that can be done with it afterwards. 

> > 2) Is there a way to use the cimport syntax to coerce it to be nogil-ready? 

> No, that would be a useless feature. If a function is allowed to be called 
> without the GIL, it should be declared nogil, and that's it. 

It would be useful for the purposes of pure-python mode compatibility.  (or since the alternative is still "cimport" I guess less-incompatibility)

Reply all
Reply to author
Forward
0 new messages