Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > gpp (conditional compilation)

Reply
Thread Tools

gpp (conditional compilation)

 
 
maxwell@ldc.upenn.edu
Guest
Posts: n/a
 
      05-02-2007
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

I'm using the following args to gpp:
+s \" \" \" +s \' \' \' +c \\\# \\n -n
The result is that the #ifdef and #endif lines get treated as
comments, rather than instructions to gpp to keep or omit the lines in
between.

I tried just omitting the +c arg, but then I get msgs at the end of
each file saying
Input ended while scanning a comment/string
apparently because I use apostrophes inside comments, and gpp thinks
those are unterminated strings.

I can think of some work-arounds, like "don't use apostrophes inside
comments", or "don't use single-quoted strings (or define them for
gpp)" or "use a different char for the first char of a gpp macro".
But I'd rather not...

Does anyone have a set of gpp args that plays well with Python? (Or
makefiles, where I presume the same problem comes up.)

Mike Maxwell
CASL/ U MD

 
Reply With Quote
 
 
 
 
dustin@v.igoro.us
Guest
Posts: n/a
 
      05-02-2007
On Wed, May 02, 2007 at 10:37:40AM -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) 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.


The hacks you'll need to wrap gpp in to get it to work will make your
head spin.

I'm not sure what brought you to gpp, but if you're looking for a basic
macro-processing language suitable for the kinds of tasks the C
preprocessor performs, I would recommend M4. GNU has an implementation
of the language, and it's actually quite widely used as the basis for
the GNU autotools suite. It is incredibly flexible (enough so that its
learning curve is a bit steep, but not too bad), but that flexibility
enables it to work with just about any underlying language.

Dustin
 
Reply With Quote
 
 
 
 
Duncan Booth
Guest
Posts: n/a
 
      05-02-2007
"(E-Mail Removed)" <(E-Mail Removed)> 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 ...
 
Reply With Quote
 
John Nagle
Guest
Posts: n/a
 
      05-02-2007
(E-Mail Removed) 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.


Just use an "if" statement. The Python interpreter is so slow
it won't matter.

John Nagle
 
Reply With Quote
 
maxwell@ldc.upenn.edu
Guest
Posts: n/a
 
      05-02-2007
(replying to myself because I got four good replies)

Wow! That was fast! OK, I'll try out these ideas, and many thanks!

Mike Maxwell

 
Reply With Quote
 
Cameron Laird
Guest
Posts: n/a
 
      05-02-2007
In article <Xns9924BED31AD3Aduncanbooth@127.0.0.1>,
Duncan Booth <(E-Mail Removed)> wrote:
>"(E-Mail Removed)" <(E-Mail Removed)> 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.
 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      05-03-2007
En Wed, 02 May 2007 14:37:40 -0300, (E-Mail Removed)
<(E-Mail Removed)> escribió:

> 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


In addition to all previous comments, I just want to menction the
existence of Tools/Scripts/ifdef.py, included in every Python install; it
can process #if #ifdef #else etc.

--
Gabriel Genellina

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help with boost signals and gpp timor.super@gmail.com C++ 2 04-30-2007 03:51 PM
Compilator GPP karolgreat@gmail.com C++ 3 06-22-2006 04:27 PM
Any small program in c++ compilable with gnu gpp compiler Robert Bralic C++ 4 07-19-2004 10:13 PM
how to let GPP/GCC read command line switches from a file ? Rolf Hemmerling C++ 0 11-19-2003 05:53 AM
how to let GPP/GCC read command line switches from a file ? Rolf Hemmerling C++ 2 11-18-2003 02:47 PM



Advertisments