Re: [cython-users] Use the function abs in a cpdef function with nogil

866 views
Skip to first unread message

Lars Buitinck

unread,
Sep 20, 2012, 4:37:08 PM9/20/12
to cython...@googlegroups.com
2012/9/20 Joaquin Cuenca Abela <e98c...@gmail.com>:
> I'm a newbie with cython. I have a cpdef function with nogil, and I want to
> calculate the absolute value of an integer. What's the most straightforward
> way to do it?
>
> I tried using abs, but I get the error "Calling gil-requiring function not
> allowed without gil".

I had the same error message yesterday. The solution was

from libc.stdlib cimport c_abs

then replace abs by c_abs.

--
Lars Buitinck
Scientific programmer, ILPS
University of Amsterdam

Chris Barker

unread,
Sep 20, 2012, 7:51:33 PM9/20/12
to cython...@googlegroups.com
On Sep 20, 2012, at 1:34 PM, Joaquin Cuenca Abela <e98c...@gmail.com>
>
> A related question, how can you define a cpdef function that does not return any value? I tried putting void as return value, but then I get the error "Cannot convert 'void' to Python object".

Just like you do in python : either no "return" at all, or:

return None

Note that a python function always return something-- it just may be None

-Chris

Joaquin Cuenca Abela

unread,
Sep 21, 2012, 5:48:21 AM9/21/12
to cython...@googlegroups.com
Hi Chris,

I don't set a return type, it gives the error:

cpdef undo_filter_sub(int filter_unit, unsigned char[:] scanline,
^
------------------------------------------------------------
cpngfilters.pyx:10:6: Function with Python return type cannot be declared nogil
--
Joaquin Cuenca Abela -- presspeople.com: Fuentes de prensa y comunicados

Joaquin Cuenca Abela

unread,
Sep 21, 2012, 5:48:58 AM9/21/12
to cython...@googlegroups.com
Hi Lars,

thanks! that worked with a minor modification:

from libc.stdlib cimport abs as c_abs

Lars Buitinck

unread,
Sep 21, 2012, 6:52:26 AM9/21/12
to cython...@googlegroups.com
2012/9/21 Joaquin Cuenca Abela <joa...@cuencaabela.com>:
> from libc.stdlib cimport abs as c_abs

Ah, yes, I was typing in a hurry :)

Chris Barker

unread,
Sep 21, 2012, 12:12:34 PM9/21/12
to cython...@googlegroups.com
On Fri, Sep 21, 2012 at 2:48 AM, Joaquin Cuenca Abela
<joa...@cuencaabela.com> wrote:
> Hi Chris,
>
> I don't set a return type, it gives the error:
>
> cpdef undo_filter_sub(int filter_unit, unsigned char[:] scanline,
> ^
> ------------------------------------------------------------
> cpngfilters.pyx:10:6: Function with Python return type cannot be declared nogil

oops, sorry -- I didn't look carefully at both nogil and cpdef...

cpdef means build both a pyton function and a C function -- C
functions need a return type. Cython defaulats to a generic Python
object as a return type, which could cause issue with nogil, so it
won't let you do that -- so try declareing a void return type:

cpdef void undo_filter_sub(int filter_unit, unsigned char[:] scanline,


I suspect that will give you None for the Python version, but I'm just
guessing here.

If that doesn't work, use int or something, and ignore the return value.

-HTH,

-Chris

> On Fri, Sep 21, 2012 at 1:51 AM, Chris Barker <chris....@noaa.gov> wrote:
>> On Sep 20, 2012, at 1:34 PM, Joaquin Cuenca Abela <e98c...@gmail.com>
>>>
>>> A related question, how can you define a cpdef function that does not return any value? I tried putting void as return value, but then I get the error "Cannot convert 'void' to Python object".
>>
>> Just like you do in python : either no "return" at all, or:
>>
>> return None
>>
>> Note that a python function always return something-- it just may be None
>>
>> -Chris
>
>
>
> --
> Joaquin Cuenca Abela -- presspeople.com: Fuentes de prensa y comunicados



--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris....@noaa.gov

Joaquin Cuenca Abela

unread,
Sep 21, 2012, 1:32:15 PM9/21/12
to cython...@googlegroups.com
Hi Chris,

nope, with void I was getting "Cannot convert 'void' to Python
object". What I did is put int and return 0, maybe this should be
added to the docs.

Thanks for your help!

Stefan Behnel

unread,
Sep 22, 2012, 9:03:11 AM9/22/12
to cython...@googlegroups.com, Cython-devel
[CC-ing cython-devel here as this is a design issue]

Joaquin Cuenca Abela, 21.09.2012 19:32:
> On Fri, Sep 21, 2012 at 6:12 PM, Chris Barker wrote:
>> On Fri, Sep 21, 2012 at 2:48 AM, Joaquin Cuenca Abela wrote:
>>> I don't set a return type, it gives the error:
>>>
>>> cpdef undo_filter_sub(int filter_unit, unsigned char[:] scanline,
>>> ^
>>> ------------------------------------------------------------
>>> cpngfilters.pyx:10:6: Function with Python return type cannot be declared nogil
>>
>> cpdef means build both a pyton function and a C function -- C
>> functions need a return type. Cython defaulats to a generic Python
>> object as a return type, which could cause issue with nogil, so it
>> won't let you do that -- so try declareing a void return type:
>>
>> cpdef void undo_filter_sub(int filter_unit, unsigned char[:] scanline,
>>
>> I suspect that will give you None for the Python version, but I'm just
>> guessing here.
>>
>> If that doesn't work, use int or something, and ignore the return value.
>
> nope, with void I was getting "Cannot convert 'void' to Python
> object". What I did is put int and return 0, maybe this should be
> added to the docs.

I think we should support "cpdef void func()" and just let the Python
wrapper return None automatically. That's the expected Python equivalent of
returning nothing. Shouldn't be hard to implement.

cpdef functions that are also declared nogil are somewhat unusual, but I
don't see them being outright wrong. Basically, it would allow you to call
them from Cython code with or without holding the GIL as well as directly
from Python code (that always holds the GIL). Might be handy for avoid code
redundancy in some cases.

Patches welcome, as always.

Note, however, that declaring the return type as void will make exception
handling more difficult. That's ok for a nogil function, which cannot raise
exceptions, but it's worth remembering for normal functions.

Stefan

mark florisson

unread,
Sep 22, 2012, 5:43:02 PM9/22/12
to cython...@googlegroups.com, Cython-devel
On 22 September 2012 14:03, Stefan Behnel <stef...@behnel.de> wrote:
> [CC-ing cython-devel here as this is a design issue]
>
> Joaquin Cuenca Abela, 21.09.2012 19:32:
>> On Fri, Sep 21, 2012 at 6:12 PM, Chris Barker wrote:
>>> On Fri, Sep 21, 2012 at 2:48 AM, Joaquin Cuenca Abela wrote:
>>>> I don't set a return type, it gives the error:
>>>>
>>>> cpdef undo_filter_sub(int filter_unit, unsigned char[:] scanline,
>>>> ^
>>>> ------------------------------------------------------------
>>>> cpngfilters.pyx:10:6: Function with Python return type cannot be declared nogil
>>>
>>> cpdef means build both a pyton function and a C function -- C
>>> functions need a return type. Cython defaulats to a generic Python
>>> object as a return type, which could cause issue with nogil, so it
>>> won't let you do that -- so try declareing a void return type:
>>>
>>> cpdef void undo_filter_sub(int filter_unit, unsigned char[:] scanline,
>>>
>>> I suspect that will give you None for the Python version, but I'm just
>>> guessing here.
>>>
>>> If that doesn't work, use int or something, and ignore the return value.
>>
>> nope, with void I was getting "Cannot convert 'void' to Python
>> object". What I did is put int and return 0, maybe this should be
>> added to the docs.
>
> I think we should support "cpdef void func()" and just let the Python
> wrapper return None automatically. That's the expected Python equivalent of
> returning nothing. Shouldn't be hard to implement.

Sounds like a good idea, that will make it easier for people.

> cpdef functions that are also declared nogil are somewhat unusual, but I
> don't see them being outright wrong. Basically, it would allow you to call
> them from Cython code with or without holding the GIL as well as directly
> from Python code (that always holds the GIL). Might be handy for avoid code
> redundancy in some cases.
>
> Patches welcome, as always.
>
> Note, however, that declaring the return type as void will make exception
> handling more difficult. That's ok for a nogil function, which cannot raise
> exceptions, but it's worth remembering for normal functions.

Nogil functions can raise exceptions without problem. It would be nice
to implement the Cython abi at some point where we get rid of
unraisable exceptions at the Cython and Python level.

> Stefan
>

Robert Bradshaw

unread,
Sep 23, 2012, 2:06:04 AM9/23/12
to cython...@googlegroups.com
> Sounds like a good idea, that will make it easier for people.'

+1
Reply all
Reply to author
Forward
0 new messages