Revision 8 as of 2008-10-28 06:51:03

Clear message

I started porting Django to Python 3 at PyCon2008. The current patch allows me to get through the tutorial, but there are certainly large parts of the code base that I haven't touched, so more issues are likely to show up.

The current version of the patch is available from http://www.dcl.hpi.uni-potsdam.de/home/loewis/django.diff

Which version (release or SVN revision) of Django is your patch based on? -- see the diff file itself; it's currently for r7346

Porting Strategy

This port attempts to maintain compatibility with older Python versions from a single code base; the goal is to support all versions that Django supports, plus 3.0. The current patch fails to do so in certain details, due to bugs in the 2to3 tool.

To simplify porting, and to allow running Django from a single code base, I changed distutils to allow invocation of the 2to3 tool as part of the build_py step, if setup.py is invoked from python3.0. To add that support, I first made 2to3 a standard library (named lib2to3).

This approach mostly works, and has the following flaws:

Changes

The changes can be roughly grouped into the following categories

Naming Changes

A number of modules that django uses have been removed, or renamed:

In addition, a few functions and attributes got renamed:

Bytes vs. Strings

Python 3 separates byte strings and character strings clearly, and defines that string literals are character strings, unless prefixed as bytes. Character strings are always represented as sequence of Unicode characters.

2to3 will change all occurrences of the unicode identifier to str, but leave all occurrences of str alone. It will also change all Unicode string literals to regular string literals, but leave the existing string literals alone.

As a consequence, it produces code like this one:

  class EscapeString(str, EscapeData): pass
  class EscapeUnicode(str, EscapeDate): pass

These types were meant to be separate, but became identical after 2to3.

To fix these problems, I made the following changes:

New-style classes

Other changes

Open Issues

Tags

Python 3k. Python 3.0. Python 3000. Py3K. Django.

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