Code

Changes between Version 1 and Version 2 of PortingNotesFor2To3


Ignore:
Timestamp:
12/10/11 05:50:53 (2 years ago)
Author:
vsajip
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • PortingNotesFor2To3

    v1 v2  
    1 == N.B. Work In Progress == 
     1= Porting 2.x code so that it runs on 2.x and 3.x from a single codebase (WIP) = 
    22 
    33Please 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. 
    44---- 
    5 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 followed, and should also apply to the work of porting Django apps to work on the same basis. 
     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 (Vinay Sajip) followed, and should also apply to the work of porting Django apps to work on the same basis. 
    66 
    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. 
     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. 
    99  Go through the piped output to see where you need to make changes. These will typically fall into a number of categories, as 
    1010  described below. 
    1111 
    1212* 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). 
     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). 
    1717 
    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. 
     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. 
    2020 
    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)}}}. 
     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 `(int, long)` or `(long, int)` in the source, do `from django.utils.py3 import integer_types` and replace the 
     25  tuple with `integer_types`. If `long` and `int` appear in tuple along with other values, remove them from the tuple and replace 
     26  the tuple with tuple `+  integer_types`. 
    2327 
    2428* If you see octal constants (such as {{{0777}}}), replace them with the hex value ({{{0x1ff}}} for the {{{0777}}} case), and if 
     
    4044  to text for display, and these occurrences of {{{str()}}} shouldn't need changing. 
    4145 
    42  
    43  
     46* If you have classes with metaclasses, change them to use the form {{{MyClassWithMetaClass(with_metaclass(MetaClass, BaseClass):}}} 
     47  where you can omit {{{BaseClass}}} if it is {{{object}}}.