[Cython] inspection of compile-time macro/etc. namespace

Chuck Blake cb at pdos.cail.mit.edu
Fri Jul 18 15:48:30 CEST 2008


Hey, guys.  Thanks for the hard work on Cython.
It's been far superior to Pyrex for my needs.

Anyway, I have .pxi files that I like to
use for actual gcc-level function inlining.
I need conditional compilation directives
that make multiple inclusion idempotent.

The usual guards in the C/C++ world would be:

    #ifndef FOO_H
    #define FOO_H
    ...
    #endif

In Cython, I have been doing

    DEF FOO_1 = 1
    DEF FOO_2 = 1
    IF FOO_1 == FOO_2:
        DEF FOO_2 = 2
        ...

That seems kind of ugly, though.  Beyond aesthetics,
it is probably nice to test for a name before using
it/logically fork/etc. based on that.  Just as one
e.g., if there is any evolution of __builtin__ names
populating the environment.

So, I propose a tiny addition to add a new compile-
time builtin called DEFINED() that just evaluates
to a boolean based on whether its string argument
is in the compile-time environment.

This allows the above construct to be written both
more simply and more clearly:

    IF not DEFINED("FOO"):
        DEF FOO = 1
        ...

Name choice follows the principle of least surprise
since the lowercase cpp name has similar functionality,
just like the other conditional compilation features.

cb


More information about the Cython-dev mailing list