gpp (conditional compilation)

Cameron Laird claird at lairds.us
Wed May 2 18:13:37 EDT 2007


In article <Xns9924BED31AD3Aduncanbooth at 127.0.0.1>,
Duncan Booth  <duncan.booth at suttoncourtenay.org.uk> wrote:
>"maxwell at ldc.upenn.edu" <maxwell at umiacs.umd.edu> wrote:
>
>> I'm trying to use the gpp utility (Gnu points to
>> http://en.nothingisreal.com/wiki/GPP) to do conditional compilation in
>> Python, and I'm running into a problem: the same '#' character
>> introduces Python comments and is used by default to introduce #ifdef
>> etc. lines. 
>> 
>> Here's an example of what I'm trying to do:
>> 
>> #ifdef DEBUG
>>        stderr.write("variable is...") #details of msg omitted
>> #endif
>> 
>
>Why do you want conditional compilation. Is there anything wrong with:
>
>    if __debug__:
>        stderr.write("variable is...") #details of msg omitted
>
>If you run Python with the -O command line option the code protected by the 
>if statement will be optimised out.
>
>For most other purposes where you might use conditional compilation just 
>adding 'if' statements to execute at runtime (or try/except) will do the 
>same purpose:
>
>if sys.platform=='win32':
>   def foo():
>       ... something ...
>else:
>   def foo():
>       .... something different ...

I want to reinforce this.  Yes, it is possible to pre-process Python
source, it's even been done before, and I'm sympathetic to the 
suggestion that m4's more appropriate than gpp.

However, Duncan and others who've followed up are right:  you can 
live without this stuff.  In fact, those who think they need condi-
tional compilation with Python are, with very few exceptions, simply
mistaken.  The urge to transform Python source this way almost always
is a symptom of unfamiliarity with Python potential and good style.

It's not just that Python can do without conditional compilation; 
Python offers *better* means to achieve the same goals.



More information about the Python-list mailing list