473,587 Members | 2,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

gpp (conditional compilation)

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("v ariable 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

May 2 '07 #1
6 2839
"ma*****@ldc.up enn.edu" <ma*****@umiacs .umd.eduwrote:
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("v ariable is...") #details of msg omitted
#endif
Why do you want conditional compilation. Is there anything wrong with:

if __debug__:
stderr.write("v ariable 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 ...
May 2 '07 #2
ma*****@ldc.upe nn.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.
Just use an "if" statement. The Python interpreter is so slow
it won't matter.

John Nagle
May 2 '07 #3
On Wed, May 02, 2007 at 10:37:40AM -0700, ma*****@ldc.upe nn.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.
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
May 2 '07 #4
(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

May 2 '07 #5
In article <Xn************ *************@1 27.0.0.1>,
Duncan Booth <du**********@s uttoncourtenay. org.ukwrote:
>"ma*****@ldc.u penn.edu" <ma*****@umiacs .umd.eduwrote:
>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("v ariable is...") #details of msg omitted
#endif

Why do you want conditional compilation. Is there anything wrong with:

if __debug__:
stderr.write("v ariable 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.
May 2 '07 #6
En Wed, 02 May 2007 14:37:40 -0300, ma*****@ldc.upe nn.edu
<ma*****@umiacs .umd.eduescribi ó:
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("v ariable 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

May 3 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2452
by: Steven T. Hatton | last post by:
I've made no secret of the fact that I really dislike the C preprocessor in C++. No aspect of the language has caused me more trouble. No aspect of the language has cause more code I've read to be difficult to understand. I've described it as GOTO's on steroids, and that's what it is!. One argument against abolishing it it that it is useful...
2
4309
by: Steve Jorgensen | last post by:
To begin with an example... Let's say you were wanting to write code usign early binding to the MSXML library, but then be able to switch between early and late binding at will. Conditional compilation is one possibility, but it's not practical. 1. You have to put a precompiler condition block around every Dim and every function...
1
16543
by: chris han | last post by:
Hi, all, I'm trying to use Conditional Compilation Statements in my code. using System; #define DEBUG public class MyClass { public static void Main() {
12
2474
by: wanghz | last post by:
Hi, Could I ask some questions about the conditional compilaion? Suppose I have three simple files: a.c, b.c and c.h /* --------a.c--------- */ #include <stdio.h> #include "c.h" int main()
2
2581
by: FireStarter | last post by:
Guys, in the code that follows, why does the method F() still compile, even if DBG is undefined? Inside method G(), the code inside <#if DBG> does not compile (notice that I can write whatever I want in there, I will not receive a compilation error). I do get such an error in F() - because of the garbage I intentionally put there - but F()...
1
3317
by: A.M-SG | last post by:
Hi, We have a solution with several c# projects within it. How can I define solution wide conditional compilation symbols?
4
2794
by: Bob | last post by:
Hi, In VS2003 conditional compilation constants and their state could be defined at project level. I was using this to control what features where offered by various builds. i.e. Feature1=true,Feature2=false, ... VS2005 seems to either allow the existance of the constant or not. ie. You must omit a constant not declare it to be false ...
10
3084
by: Dave | last post by:
I'm a C++ programmer of many years, trying to get my feet wet in C#. I have a question about conditional compilation. In C++, I would sometimes define a constant in an include file, and then have blocks of code in different source files that were conditionally compiled based on that constant. Now that C# has done away with include files,...
1
2288
by: Marek | last post by:
I use VS2005 with framework 2.0 and I just found a behavior I consider odd. Here is the code that illustrates th eproblem: public static void MethodA() { MethodB() } #if DEBUG
0
7843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7967
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.