[TIP] backport 0.1.1 release

03sjbrown 03sjbrown+subscriptions at gmail.com
Tue Nov 29 22:20:27 PST 2011


A few weeks ago, I posted asking about existing projects that
integrate 3to2 into their test harnesses. I also brought this up with
a few people at last year's TIP BOF but no one knew of projects doing
this. I have a method I've been using to do this myself and finally
decided to clean it up and release it into the wild for other people
to play with.

So here's the backport 0.1.1 release announcement:

backport <http://code.google.com/p/backport/> is a Python module that
provides helper functions to auto-run 3to2 and register import hooks
for testing. It is intended for use as part of a project's testing
suite (e.g. in a runtests.py file). Write new projects in Python 3 and
use integrated code translation to test under 2.x quickly and
incrementally as your project grows. You won't be tasked with a
porting project at the end of every release cycle.

It's been barely a year since the 3to2 code-conversion utility reached
version 1.0 status. Thanks to Joe Amenta's great work, developers can
begin new Python 3 projects and provide 2.x support by backporting
using 3to2 (as mentioned in Brett Cannon's "Porting" write-up).

Currently, backport is distributed as a Python 3 project template that
demonstrates how to use backport.py as part of a test harness.

Before you can use backport, you'll also need 3to2 -- available on
PyPI (for Py2.7, for Py3k). 3to2 began as a Google Summer of Code
project -- you can read more about it and its development here.

  * Project code and unit tests are written in Python 3.
  * A small test runner is written in code that is both Python 2 and
Python 3 compatible.
  * When testing under Python 2:
     - The code (including tests) is replicated in a py2_folder.
     - All .py files (including tests) are converted using 3to2.
     - These new Python 2 files are added to the module search path
with an import hook.
     - Tests are loaded from the py2_folder.
     - On subsequent runs, only new & changed files are copied and
converted (unchanged files are cached).
  * When testing under Python 3:
     - File copying and conversion is skipped.
     - Tests are loaded and modules are imported from the main code
base (Python 3) and run as normal.

Using backport as part of your project's test-harness would look
something like the following:

  >>> from tests import backport
  >>> ...
  >>> if python_version == 2:
  >>>     backport.py2port(PY3_DIR, PY2_DIR, run3to2)  # Replicate
file-tree and run 3to2.
  >>>     backport.import_from(PY2_DIR)                # Add import
hook for Py2 modules.
  >>>     TESTS_DIR = PY2_DIR + os.sep + TESTS_DIR     # Change tests directory.
  >>> ...


More information about the testing-in-python mailing list