Ticket #751 (closed enhancement: wontfix)

Opened 19 months ago

Last modified 17 months ago

Allow lambda with C functions

Reported by: nikratio Owned by:
Priority: major Milestone: wishlist
Component: Cython Language Feature Keywords:
Cc: nikratio

Description

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

Change History

Changed 19 months ago by nikratio

  • cc nikratio added

Changed 19 months ago by nikratio

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).

Changed 17 months ago by scoder

  • status changed from new to closed
  • resolution set to wontfix
Note: See TracTickets for help on using tickets.