Hi,

since William brought up this topic lately, I think there are a couple of very
valid cases where you want to define a macro in C and use it as a function in
Cython. This currently means:

1) write a macros.h file to hold the macro
2) add a 'cdef extern from "macros.h"' to your favourite .pxd file to define
your macros in Cython

So this requires you to keep track of two files for things that may just be a
couple of lines in C.

I think this is so common that it would help if you could write inline C code
in a .pxd file and have it written into your generated C file automatically. I
could imagine a syntax like this:

    # macros.pxd
    MACROS = """
        #define _isString(obj)   (PyString_CheckExact(obj)  || \
                                  PyUnicode_CheckExact(obj) || \
                                  PyObject_TypeCheck(obj, &PyBaseString_Type))

        #define _fqtypename(o)   (((PyTypeObject*)o)->ob_type->tp_name)
    """

    cdef inline from MACROS:
        cdef int _isString(object obj)
        cdef char* _fqtypename(object t)

Instead of an '#include "macros.h"', this would write the string given by
MACROS verbatimly into the C file, directly behind the #include section. It
could also keep track of the strings in a set to make sure they are only
written out once, even when cimported redundantly at various places.

Any comments on this?

Stefan


_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to