[Pyrex] conditional compilation

Stefan Behnel behnel_ml at gkec.informatik.tu-darmstadt.de
Wed Mar 29 09:43:02 CEST 2006


Sam Rushing wrote:
> I noticed something about conditional compilation in the recent patch
> from Stefan Behnel... maybe it's time to pipe up about this change I've
> been sitting on.
> 
> The attached patch extends the Pyrex syntax to support three forms of
> 'directive', which allows some compile-time changes to the output.  Now,
> Greg Ewing isn't a big fan of this - and I agree with him that
> conditionals are bad in many ways.  I don't blame him in the least for
> not wanting this wart in his language. 8^)
> 
> However, we desperately need this functionality for our work at
> IronPort... and the patch isn't all that large.  We're currently using
> it for debugging, instrumentation, assertions, platform differentiation,
> etc... I hope others find it useful.
> 
> The new syntax is:
> 
> %define <name> [<expr>]
> %if <expr>:
>   <suite-to-be-merged>
> [%else:
>   <suite-to-be-merged>]
> %import <module-name>
> 
> There's a single global 'compile-time' namespace.  For <expr> you can
> put just about anything you like, it's very flexible.  [One sample I
> tried grepped through an include file - how's that for ugly!]
> 
> The patch is against 0.9.3.1.  A demo file with a setup.py is also
> included.  Comments/feedback appreciated.

This actually solves a different purpose than my patch. Mine is a compiler
internal optimisation feature, while yours is a language feature.

If we're talking language syntax, what about allowing the "const" qualifier
instead of a "define" ? Then you could hand that to the C-compiler and it
would see by its own what can be resolved at compile time. That should solve a
lot of cases where your conditionals are used now without actually needing
explicit conditionals.

Proposed syntax (intentionally similar to cdef):

const <simple-C-type> <name> = <expr>

or in pxd files or for external definitions (like -DDEBUG):

const <simple-C-type> name

The first would then be implemented as a #define, the expression could be
thrown into an eval() at compile time and the C-compiler would do the rest.

BTW: what is your "%import" doing?

Stefan




More information about the Pyrex mailing list