| Version 10 (modified by , 14 years ago) ( diff ) |
|---|
Porting 2.x code so that it runs on 2.x and 3.x from a single codebase
When porting Django so that it works from a single codebase in Python 2.x and 3.x, the following is a rough-and-ready guide which I (Vinay Sajip) followed, and these guidelines should also apply to the work of porting Django apps to work on the same basis.
- Do run
2to3on the codebase, but pipe the output to a file so that you can examine what changes need to be made, But don't run2to3to make inplace changes to your code, as the resulting code will typically not run under Python 2.x. Go through the piped output to see where you need to make changes. These will typically fall into a number of categories, as described below.
- In any module which uses Unicode or bytes literals (
u'foo'orb'bar'), do insertfrom django.utils.py3 import u, bat the top of the module in the appropriate place, and do replaceu'foo'withu('foo')andb'bar'withb('bar')throughout the source. The same applies to the constants with double quotes (u"foo"orb"bar", which should be replaced byu("foo")orb("bar")respectively). Note: this is needed for Python 2.5 compatibility only. For compatibility only with Python 2.6 and later, addfrom __future__ import unicode_literalsin all modules where you are using Unicode literals, and remove theuprefix from the Unicode literals, as you don't need it. If you need native strings, usefrom django.utils.py3 import nand replace those literals with n(...). This will return str on both 2.x and 3.x. If you need byte strings, use b'foo'.
- If you need to make any code conditional on 2.x vs. 3.x, you can do
from django.utils.py3 import PY3and usePY3as a condition (as you might expect, it's aboolwhich isTrueon 3.x andFalseon 2.x.
- If you see any long constants (such as
5L), dofrom django.utils.py3 import long_typeand replace e.g.5Lwithlong_type(5).
- If you see
(int, long)or(long, int)in the source, dofrom django.utils.py3 import integer_typesand replace the tuple withinteger_types. Iflongandintappear in a tuple along with other values, remove them from the tuple and replace the tuple with tuple+ integer_types.
- If you see octal constants (such as
0777), replace them with the hex value (0x1fffor the0777case), and if possible, place the octal constant in a comment so anyone can see what it was originally. Note: this is only needed for 2.5 compatibility. For 2.6+ compatibility, you can use the 3.x octal form0o777.
- If you see code of the type
except ExceptionTypeOrTupleOfExceptionTypes, name_to_bind_to:, see ifname_to _bind_tois used in the scope (exception handling clause, or later in the same function or method). If not used, just change theexcept:statement toexcept ExceptionTypeOrTupleOfExceptionTypes:and you're done. If used, do one more thing: put the codename_to_bind_to = sys.exc_info()[1]just beforename_to_bind_tois first used, and make sure that thesysmodule is imported. Note: this is only needed for 2.5 compatibility. For 2.6+ compatibility, you can use the 3.x formexcept ExceptionTypeOrTupleOfExceptionTypes as name_to_bind_to. Note that in 3.x, names bound like this are not in the local scope, and so not visible outside the exception clause.
- If
unicodeoccurs in the source (i.e. not in comments), dofrom django.utils.py3 import text_typeand replaceunicodewithtext_type.
- If
basestringoccurs in the source (i.e. not in comments), dofrom django.utils.py3 import string_typesand replacebasestringwithstring_types.
- If
stroccurs in the source (i.e. not in comments), you may need to dofrom django.utils.py3 import binary_typeand replacestrwithbinary_type. However, don't use this blindly:str()is sometimes used to convert something to text for display, and these occurrences ofstr()shouldn't need changing.
- If you have classes with metaclasses, change them to use the form
MyClassWithMetaClass(with_metaclass(MetaClass, BaseClass):where you can omitBaseClassif it isobject. Remove the__metaclass__ = XXXstatement from the class body (replacing it withpassif that's all there was in the class body).
- If you need to reraise an exception, do
from django.utils.py3 import reraiseand invoke it using the formreraise(type, instance, traceback).
- You will see that
2to3putslist()around calls to methods namedkeys,valuesoritems. This is sometimes but not always necessary, so examine each on a case-by-case basis. In particular, ifvaluesis called on a queryset, you don't want to wrap withlist().
- There are functions
iterkeys,itervaluesanditeritemsdefined indjango.utils.py3. Import them if there is any reference to those methods, and replace using the patternx.iterkeys()->iterkeys(x)(wherexmight be an expression rather than just a name).
- Replace
mapcalls with the list comprehensions suggested by2to3. If you seemap(None, ...)you will need to import and useizip_longestfromdjango.utils.py3.
- If you see
xrangein the source, just dofrom django.utils.py3 import xrange.
- If you use anything from
urllib,urllib2,urlparse, you will need to import the actual functions fromdjango.utils.py3. You can grep the instances of those calls in the ported Django code to see how it's done.
- If you're using
StringIO,BytesIO, orpickle, import them fromdjango.utils.py3.
- If a class has a
next()method which is used for iteration, add a line__next__ = nextjust after it in the class. If it has a__nonzero__method, add__bool__ = __nonzero__in the same way. Where you seex.next()used to iterate, dofrom django.utils.py3 import nextand replace using the patternx.next()->next(x)(wherexmight be an expression rather than just a name).
- If you define rich comparisons, review how they work in 2.x and 3.x and adapt your code accordingly.
- If you use
execorexecfile, use the versionsexec_andexecfile_fromdjango.utils.py3.
- Use
django.utils.py3.maxsizein place ofsys.maxint.
- Review
__repr__,__str__and__unicode__methods to see what needs changing. On Python 3.x,__str__()should not returnbytes, and neither should__repr__().
- Take a look at
django.utils.py3to see what else you might need to change in your code: everything in there is needed somewhere in the porting process.
Note:
See TracWiki
for help on using the wiki.