[Cython] inspection of compile-time macro/etc. namespace
Chuck Blake
cb at mit.edu
Thu Jul 17 23:15:47 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
-------------- next part --------------
diff -ruw Cython-0.9.8.orig/Cython/Compiler/Scanning.py Cython-0.9.8/Cython/Compiler/Scanning.py
--- Cython-0.9.8.orig/Cython/Compiler/Scanning.py 2008-06-11 14:25:34.000000000 -0400
+++ Cython-0.9.8/Cython/Compiler/Scanning.py 2008-07-17 16:39:27.691882798 -0400
@@ -186,6 +186,9 @@
else:
raise
+ def defined(self, name):
+ return name in self.entries
+
def initial_compile_time_env():
benv = CompileTimeScope()
names = ('UNAME_SYSNAME', 'UNAME_NODENAME', 'UNAME_RELEASE',
@@ -201,6 +204,7 @@
for name in names:
benv.declare(name, getattr(__builtin__, name))
denv = CompileTimeScope(benv)
+ denv.declare('DEFINED', denv.defined)
return denv
#------------------------------------------------------------------
More information about the Cython-dev
mailing list