Search code examples
cythonkeywordword-wrap

Is there a way to wrap a structure that is named like a keyword (eg. print)?


I have some C source code and want to wrap it in Cython. Now, the problem is, that there is a structure called print, and externing it throws a syntax error.

cdef extern from "foo.h":
    struct print:
        # ...

Same problem would appear when an attribute or a function or alike is called like a keyword.

cdef extern from "foo.h":
    struct foo:
        bint print
    print(char*, int)

Is there a way to work around this, without modifieng the source? Maybe some technique that replaces a proxy-name with the real-name in the source-file ?


Solution

  • I think the solution you are looking for is something along the lines of:

    cdef extern from "foo.h":
        struct print "MY_print":
            double var "MY_var"
    

    print.var will then be defined by:

    MY_print.MY_var
    

    This way you can rename structs, functions, unions and enums from the header file. The names are converted when Cython compiles your code into C code.

    The relevant part of Cython documentation can be found here.