Changes between Initial Version and Version 1 of PortingNotesFor2To3


Ignore:
Timestamp:
Dec 10, 2011, 7:41:20 AM (13 years ago)
Author:
Vinay Sajip
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • PortingNotesFor2To3

    v1 v1  
     1== N.B. Work In Progress ==
     2
     3Please Note: This is work in progress, I am working on the draft, and I will remove "I am working on the draft" when I have finished working on the draft.
     4----
     5When 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 followed, and should also apply to the work of porting Django apps to work on the same basis.
     6
     7* Do run {{{2to3}}} on the codebase, but pipe the output to a file so that you can examine what changes need to be made, But
     8  don't run {{{2to3}}} to make inplace changes to your code, as the resulting code will typically not run under Python 2.x.
     9  Go through the piped output to see where you need to make changes. These will typically fall into a number of categories, as
     10  described below.
     11
     12* In any module which uses Unicode or bytes literals ({{{u'foo'}}} or {{{b'bar'}}}), do insert
     13  {{{from django.utils.py3 import u, b}}} at the top of the module in the appropriate place, and do
     14  replace {{{u'foo'}}} with {{{u('foo')}}} and {{{b'bar'}}} with {{{b('bar')}}} throughout the source.
     15  The same applies to the constants with double quotes ({{{u"foo"}}} or {{{b"bar"}}}, which should be replaced by
     16  {{{u("foo")}}} or {{{b("bar")}}} respectively).
     17
     18* If you need to make any code conditional on 2.x vs. 3.x, you can '''do''' {{{from django.utils.py3 import PY3}}} and use {{{PY3}}
     19  as a condition (as you might expect, it's a {{{bool}}} which is {{{True}} on 3.x and {{{False}} on 2.x.
     20
     21* If you see any long constants (such as {{{5L}}}), '''do''' {{{from django.utils.py3 import long_type}}} and replace e.g. {{{5L}}}
     22  with {{{long_type(5)}}}.
     23
     24* If you see octal constants (such as {{{0777}}}), replace them with the hex value ({{{0x1ff}}} for the {{{0777}}} case), and if
     25  possible, place the octal constant in a comment so anyone can see what it was originally.
     26* If you see code of the type {{{except ExceptionTypeOrTupleOfExceptionTypes, name_to_bind_to:}}}, see if {{{name_to _bind_to}}}
     27  is used in the scope (exception handling clause, or later in the same function or method). If not used, just change the
     28  {{{except:}}} statement to {{{except ExceptionTypeOrTupleOfExceptionTypes:}}} and you're done. If used, do one more thing:
     29  put the code {{{name_to_bind_to = sys.exc_info()[1]}}} just before {{{name_to_bind_to}}} is first used, and make sure that
     30  the {{{sys}}} module is imported.
     31
     32* If {{{unicode}}} occurs in the source (i.e. not in comments), do {{{from django.utils.py3 import text_type}}} and replace
     33  {{{unicode}}} with {{{text_type}}}.
     34
     35* If {{{basestring}}} occurs in the source (i.e. not in comments), do {{{from django.utils.py3 import string_types}}} and replace
     36  {{{basestring}}} with {{{string_types}}}.
     37
     38* If {{{str}}} occurs in the source (i.e. not in comments), you may need to do {{{from django.utils.py3 import binary_type}}}
     39  and replace {{{str}}} with {{{binary_type}}}. However, don't use this blindly: {{{str()}}} is sometimes used to convert something
     40  to text for display, and these occurrences of {{{str()}}} shouldn't need changing.
     41
     42
     43
Back to Top