[Cython] Keyword arguments for cdef functions and call for a little help

Stefan Behnel stefan_ml at behnel.de
Fri Feb 8 00:28:21 CET 2013


Hi,

in the current master branch, I implemented support for passing keyword
arguments into cdef functions. The names are mapped statically at compile
time to the names declared in the signature. This means that you can now do
this:

    cdef func(int x, bint flag):
        pass

    func(1, flag=True)

and it will be compiled down into (essentially) this calling code:

    func(1, 1);

Note that optional arguments at the end worked already, so this:

    cdef func(int x, bint flag1=False, bint flag2=True):
        pass

    func(1, flag1=True)

is equivalent to this call:

    func(1, 1, 1);

but you can't (currently) do this:

    func(1, flag2=True)     # error: flag1 is missing!


Obviously, you also can't use keyword arguments for functions that were
declared without argument names, e.g. in a case like this:

    cdef extern from "some_header.h":
        int somefunc(int, char*)


This feature also works for libc declarations, e.g.

    from libc.string cimport strstr

    print(strstr(needle="abc", haystack="xabcy"))

where strstr() is declared as

    cdef extern from "string.h":
        char *strstr (const char *haystack, const char *needle)

(I keep getting the argument order wrong here, so this really makes it
easier to read for me.)

We keep these declarations here in the source tree:

https://github.com/cython/cython/tree/master/Cython/Includes


However, I only now converted the parameter names in these standard
declarations to lower case, they were previously written as upper case
names (i.e. "HAYSTACK" and "NEEDLE"), which is a bit ugly when used as
keyword arguments (but allowed parameter names like "FROM" instead of the
reserved word "from").

Would someone be so kind to go over the standard declarations that we ship
and check that the argument names they are declared with are a) available
and b) proper lower case parameter names, as one would expect them?
Preferably someone who has the glibc headers within reach to look them up
if they are missing? I'm pretty sure that the current names were copied
from there anyway, so most of the declarations should be ok already - but
some may not be, and I'd like to get this straight before people start to
rely on them.

I also noticed that many of the C++ function/method declarations and posix
declarations lack names. It would be nice if someone could add them, too.

I admit that this is a boring and somewhat tedious task, but it would
really help us.

And, obviously, this new feature needs a bit of general testing. :)

Thanks for any help,

Stefan


More information about the cython-devel mailing list