robertwb / issues-import-test

0 stars 0 forks source link

Allow lambda with C functions #114

Closed robertwb closed 7 years ago

robertwb commented 7 years ago

Reported by nikratio on 24 Oct 2011 00:33 UTC Currently, it is not possible to use lambda with a C function:

$ cython -Wextra mylib2.pyx

Error compiling Cython file:
------------------------------------------------------------
...

def test():
    cdef void *ptr
    ptr = malloc(sizeof(int))

    return lambda : init(ptr)
                       ^
------------------------------------------------------------

mylib2.pyx:10:24: Cannot convert 'void' to Python object

$ cat mylib2.pyx 
from libc.stdlib cimport malloc

cdef extern from "*":
    void init(void* ptr)

def test():
    cdef void *ptr
    ptr = malloc(sizeof(int))

    return lambda : init(ptr)

This is of course a legitimate error, it's not clear what the lambda expression is supposed to return.

However, that means that creation of a Python wrapper for a C function always requires explicit definition of the function. It would be very nice to have a way around that.

My suggestion is therefore to treat any call to C function of type void as returning None, so that the following expression becomes valid:

(lambda : <void> c_function())() is None

Migrated-From: http://trac.cython.org/ticket/751

robertwb commented 7 years ago

Modified by nikratio on 24 Oct 2011 00:33 UTC

robertwb commented 7 years ago

Comment by nikratio on 26 Oct 2011 16:24 UTC Consensus on -users was that special casing lambda to convert void to None is not a good idea, and adding a new keyword "clambda" is not reasonable just to save two lines of code for defining a named function. So I guess we can forgot about this idea (I don't seem to have permission to change the ticket status).

robertwb commented 7 years ago

Modified by scoder on 9 Jan 2012 15:14 UTC