Python preprosessor

  • Thread starter Tuomas Vesterinen
  • Start date
T

Tuomas Vesterinen

I am developing a Python application as a Python2.x and Python3.0
version. A common code base would make the work easier. So I thought to
try a preprosessor. GNU cpp handles this kind of code correct:

<test_cpp.py>
#ifdef python2
print u'foo', u'bar'
#endif
#ifdef python3
print('foo', 'bar')
#endif
<end code>

results:
> cpp -E -Dpython2 test_cpp.py
....
print u'foo', u'bar'

Any other suggestions?

Tuomas Vesterinen
 
T

Tuomas Vesterinen

Peter said:

I am intensively using 2to3.py. So I have 2 codebase: one in py2 and the
other in py3. When changing the code I have to do things to 2 separate
codebase in the repository. There are no patch2to3 or patch3to2, So I
thought that for ensuring the common functionality of both version it
would be nice to bring both versions into a common codebase. So I can
update only one code and automate builds and tests for both versions.

Tuomas Vesterinen
 
R

Roger Binns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Tuomas said:
I am intensively using 2to3.py. So I have 2 codebase: one in py2 and the
other in py3.

The expectation would be that you only maintain the py2 code and
automatically generate the py3 code on demand using 2to3.

It is possible to have the same code run under both versions, but not
recommended. As an example I do this for the APSW test suite, mostly
because the test code deliberately explores various corner cases that
2to3 could not convert (nor should it try!). Here is how I do the whole
Unicode thing:

if py3: # defined earlier
UPREFIX=""
else:
UPREFIX="u"

def u(x): # use with raw strings
return eval(UPREFIX+"'''"+x+"'''")

# Example of use
u(r"\N${BLACK STAR}\u234")

You can pull similar stunts for bytes (I use buffers in py2), long ints
(needing L suffix in some py2 versions), the next() builtin from py3,
exec syntax differences etc. You can see more details in the first 120
lines of http://code.google.com/p/apsw/source/browse/apsw/trunk/tests.py

Roger
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkosTxEACgkQmOOfHg372QS4rQCgl1ymNME2kdHTBUoc7/f2e+W6
cbMAmwf7mArr7hVA8k/US53JE59ChnIt
=pQ92
-----END PGP SIGNATURE-----
 
R

R. David Murray

Tuomas Vesterinen said:
I am developing a Python application as a Python2.x and Python3.0
version. A common code base would make the work easier. So I thought to
try a preprosessor. GNU cpp handles this kind of code correct:

<test_cpp.py>
#ifdef python2
print u'foo', u'bar'
#endif
#ifdef python3
print('foo', 'bar')
#endif
<end code>

results:
...
print u'foo', u'bar'

Any other suggestions?

There's a Google Summer of Code project to create a 3to2 processor.
That would let you maintain the code in 3.x, and have it automatically
translated on demand so that it will run under 2.x (where x goes back
to at least 5, I think, but I'm not sure).

Of course, it isn't finished yet, so it won't do you any good right at
the moment :(
 
T

Tuomas Vesterinen

R. David Murray said:
There's a Google Summer of Code project to create a 3to2 processor.
That would let you maintain the code in 3.x, and have it automatically
translated on demand so that it will run under 2.x (where x goes back
to at least 5, I think, but I'm not sure).

Of course, it isn't finished yet, so it won't do you any good right at
the moment :(

I found also a ready made py3to2-tool (test/develop/integrate python 3.0
code natively under robust, software-rich python 2.5 environment) at:
http://code.activestate.com/recipes/574436/
It seems so complicated that I don't dare to try before some more
experienced people has commented it.

TV
 
T

Tuomas Vesterinen

Roger said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1



The expectation would be that you only maintain the py2 code and
automatically generate the py3 code on demand using 2to3.

It is possible to have the same code run under both versions, but not
recommended. As an example I do this for the APSW test suite, mostly
because the test code deliberately explores various corner cases that
2to3 could not convert (nor should it try!). Here is how I do the whole
Unicode thing:

if py3: # defined earlier
UPREFIX=""
else:
UPREFIX="u"

def u(x): # use with raw strings
return eval(UPREFIX+"'''"+x+"'''")

# Example of use
u(r"\N${BLACK STAR}\u234")

You can pull similar stunts for bytes (I use buffers in py2), long ints
(needing L suffix in some py2 versions), the next() builtin from py3,
exec syntax differences etc. You can see more details in the first 120
lines of http://code.google.com/p/apsw/source/browse/apsw/trunk/tests.py

Roger
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkosTxEACgkQmOOfHg372QS4rQCgl1ymNME2kdHTBUoc7/f2e+W6
cbMAmwf7mArr7hVA8k/US53JE59ChnIt
=pQ92
-----END PGP SIGNATURE-----

You have interesting solutions.

TV
 
G

Gabriel Genellina

En Sun, 07 Jun 2009 13:31:07 -0300, Tuomas Vesterinen
I am developing a Python application as a Python2.x and Python3.0
version. A common code base would make the work easier. So I thought to
try a preprosessor. GNU cpp handles this kind of code correct:

<test_cpp.py>
#ifdef python2
print u'foo', u'bar'
#endif
#ifdef python3
print('foo', 'bar')
#endif
<end code>

See ifdef.py in the Tools/scripts directory for a pure Python preprocessor.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,289
Messages
2,571,450
Members
48,127
Latest member
svastipharmancrr

Latest Threads

Top