Revision 6 as of 2008-12-08 12:49:09

Clear message

Porting Python Code to 3.0

Rough outline of what needs to be covered

  • Using 2to3. (-> Benjamin)
  • Bringing code up to shape to work with 2.6 and 2to3->3.0.
  • What about 2.3/2.4/2.5 compatibility?

Martin's notes from psycopg2

  • bytes vs. strings 1. Design decisions needs to be taken what exactly must be represented as bytes, and what as strings. In many cases, that was easy for psycopg, except for the question how SQL queries are represented. It appears clear that they are plain text, however, the Postgres API requires them to be transmitted in the connection encoding. I still decided to represent them internally in Unicode, but converting them to the connection encoding as early as possible probably would have worked as well.
  • the buffer object is gone; I use memoryview in 3.x.
  • various tests where in the code of the form if version_major == and version_minor > 4 (say, or 5) This will break for 3.x; you have to write if (version_major == 2 and version_minor > 4) or version_major > 2
  • Python code 1: I used the 2to3 support in distutils
  • Python code 2: setup.py needs to run in both versions. I had to replace popen2 with subprocess if available. Also, map() now returns an iterator, which I explicitly convert into list, and so on.
  • Python code 3: the test suite doesn't get installed, and hence not auto-converted with 2to3 support. I explicitly added a 2to3 conversion into the test runner, which copies the py3 version of the test into a separate directory.

Unable to edit the page? See the FrontPage for instructions.