Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pure-python mode: nogil + ccall #3169

Open
paugier opened this issue Oct 7, 2019 · 2 comments

Comments

@paugier
Copy link

commented Oct 7, 2019

This code works fine:

# cython: language_level=3
import cython

cpdef inline cython.int add(cython.int a, cython.int b) nogil:
    return a + b

def use_add(cython.int n):
    cdef Py_ssize_t i
    cdef cython.int result = 1
    with cython.nogil:
        for i in range(n):
            result = add(result, result)
    return result

However, the equivalent code in pure-Python mode does not compile:

# cython: language_level=3
import cython

@cython.ccall
@cython.inline
@cython.returns(cython.int)
@cython.locals(a=cython.int, b=cython.int)
@cython.nogil
def add(a, b):
    return a + b

@cython.locals(n=cython.int, i=Py_ssize_t, result=cython.int)
def use_add(n):
    result = 1
    with cython.nogil:
        for i in range(n):
            result = add(result, result)
    return result

And it works if I replace @cython.ccall by @cython.cfunc. But it is no longer equivalent to the first code.

The error log:

Compiling pure_wo_pxd_ccall.py because it changed.
[1/1] Cythonizing pure_wo_pxd_ccall.py

Error compiling Cython file:
------------------------------------------------------------
...
@cython.ccall
@cython.inline
@cython.returns(cython.int)
@cython.locals(a=cython.int, b=cython.int)
@cython.nogil
def add(a, b):
^
------------------------------------------------------------

pure_wo_pxd_ccall.py:10:0: Constructing Python function not allowed without gil

Error compiling Cython file:
------------------------------------------------------------
...
@cython.ccall
@cython.inline
@cython.returns(cython.int)
@cython.locals(a=cython.int, b=cython.int)
@cython.nogil
def add(a, b):
^
------------------------------------------------------------

pure_wo_pxd_ccall.py:10:0: Constructing Python tuple not allowed without gil

Error compiling Cython file:
------------------------------------------------------------
...
@cython.ccall
@cython.inline
@cython.returns(cython.int)
@cython.locals(a=cython.int, b=cython.int)
@cython.nogil
def add(a, b):
       ^
------------------------------------------------------------

pure_wo_pxd_ccall.py:10:8: Operation not allowed without gil

Error compiling Cython file:
------------------------------------------------------------
...
@cython.ccall
@cython.inline
@cython.returns(cython.int)
@cython.locals(a=cython.int, b=cython.int)
@cython.nogil
def add(a, b):
          ^
------------------------------------------------------------

pure_wo_pxd_ccall.py:10:11: Operation not allowed without gil

...

  File "/home/users/me/.pyenv/versions/3.7.3/lib/python3.7/site-packages/Cython/Build/Dependencies.py", line 1096, in cythonize
    cythonize_one(*args)
  File "/home/users/me/.pyenv/versions/3.7.3/lib/python3.7/site-packages/Cython/Build/Dependencies.py", line 1219, in cythonize_one
    raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: pure_wo_pxd_ccall.py

Could you please confirm that it is indeed a bug? It would be great to tell where to start to try to fix it.

@robertwb

This comment has been minimized.

Copy link
Contributor

commented Oct 8, 2019

Yes, this is a bug. I'm not sure where the bug is, but it almost looks like it's trying to apply the nogil to the creation of the wrapper and not just the body of the function itself.

@paugier

This comment has been minimized.

Copy link
Author

commented Oct 12, 2019

The same bug also happens with pure Python mode with a pxd file:

The Python file:

# cython: language_level=3
import cython

def add(a, b):
    return a + b

def use_add(n):
    result = 1
    with cython.nogil:
        for i in range(n):
            result = add(result, result)
    return result

The .pxd file:

cimport cython
cdef inline int add(int a, int b) nogil

@cython.locals(n=int, i=int, result=int)
cpdef int use_add(int n)

This works but if I replace cdef by cpdef, it does not compile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.