Python preprosessor

Roger Binns rogerb at rogerbinns.com
Sun Jun 7 19:36:54 EDT 2009


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

Tuomas Vesterinen wrote:
> 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-----




More information about the Python-list mailing list