You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Instructions and notes on porting code from Python 2 to both Python 3 and 2 using ``future``:
6
+
Instructions and notes on converting code from supporting only Python 2 to
7
+
supporting both Python 3 and 2 with a single codebase using ``futurize``:
7
8
8
9
.. _porting-setup:
9
10
@@ -14,7 +15,7 @@ Step 0 goal: set up and see the tests passing on Python 2 and failing on Python
14
15
15
16
a. Clone the package from github/bitbucket. Optionally rename your repo to ``package-future``. Examples: ``reportlab-future``, ``paramiko-future``, ``mezzanine-future``.
16
17
b. Create and activate a Python 2 conda environment or virtualenv. Install the package with ``python setup.py install`` and run its test suite on Py2.7 or Py2.6 (e.g. ``python setup.py test`` or ``py.test`` or ``nosetests``)
17
-
c. Optionally: if there’s a ``.travis.yml`` file, add Python version 3.3 and remove any versions < 2.6.
18
+
c. Optionally: if there is a ``.travis.yml`` file, add Python version 3.3 and remove any versions < 2.6.
18
19
d. Install Python 3.3 with e.g. ``sudo apt-get install python3``. On other platforms, an easy way is to use `Miniconda <http://repo.continuum.io/miniconda/index.html>`_. Then e.g.::
19
20
20
21
conda create -n py33 python=3.3 pip
@@ -31,7 +32,8 @@ The goal for this step is to modernize the Python 2 code without introducing any
31
32
pip install future
32
33
33
34
**1b**. Run ``futurize --stage1 -w *.py subdir1/*.py subdir2/*.py``. Note that with
34
-
``zsh``, you can apply stage1 to all Python source files recursively with::
35
+
recursive globbing in ``bash`` or ``zsh``, you can apply stage 1 to all Python
36
+
source files recursively with::
35
37
36
38
futurize --stage1 -w **/*.py
37
39
@@ -76,10 +78,10 @@ again with the help of the ``future`` package.
76
78
77
79
**2a**. Run::
78
80
79
-
futurize —-stage2 myfolder1/*.py myfolder2/*.py
81
+
futurize --stage2 myfolder1/*.py myfolder2/*.py
80
82
81
-
Alternatively, with ``zsh``, you can view the stage 2 changes to all Python source files
82
-
recursively with::
83
+
Or, using recursive globbing with ``bash`` or ``zsh``, you can view the stage 2
84
+
changes to all Python source files recursively with::
83
85
84
86
futurize --stage2 **/*.py
85
87
@@ -93,13 +95,13 @@ These will likely require imports from ``future``, such as::
93
95
from future.builtins import bytes
94
96
from future.builtins import open
95
97
96
-
Optionally, you can use the ``--unicode-literals`` flag to adds this further
97
-
import to the top of each module::
98
+
Optionally, you can use the ``--unicode-literals`` flag to add this import to
99
+
the top of each module::
98
100
99
101
from __future__ import unicode_literals
100
102
101
-
All strings would then be unicode (on Py2 as on Py3) unless explicitly marked
102
-
with a ``b''`` prefix.
103
+
All strings in the module would then be unicode on Py2 (as on Py3) unless
104
+
explicitly marked with a ``b''`` prefix.
103
105
104
106
If you would like ``futurize`` to import all the changed builtins to have their
105
107
Python 3 semantics on Python 2, invoke it like this::
@@ -111,19 +113,11 @@ Python 3 semantics on Python 2, invoke it like this::
111
113
112
114
**2c**. Commit your changes! :)
113
115
114
-
**2d**. Now run your tests on Python 2 and notice the errors. Add wrappers from ``future`` to re-enable Python 2 compatibility:
115
-
116
-
- :func:`utils.reraise()` function for raising exceptions compatibly
117
-
- ``bytes(b'blah')`` instead of ``b'blah'``
118
-
- ``str('my string')`` instead of ``'my string'`` if you need to enforce Py3’s strict type-checking on Py2
119
-
- ``int(1234)`` instead of ``1234`` if you want to enforce a Py3-like long integer
120
-
- :func:`@utils.implements_iterator` decorator for any custom iterator class with a ``.__next__()`` method (which used to be ``.next()``)
121
-
- :func:`@utils.python_2_unicode_compatible` decorator for any class with a ``__str__`` method (which used to be ``__unicode__``).
122
-
- :func:`utils.with_metaclass` to define any metaclasses.
123
-
124
-
See :ref:`what-else` for more info.
116
+
**2d**. Now run your tests on Python 2 and notice the errors. Add wrappers from
117
+
``future`` to re-enable Python 2 compatibility. See the
118
+
:ref:`compatible-idioms` cheat sheet or :ref:`what-else` for more info.
125
119
126
120
After each change, re-run the tests on Py3 and Py2 to ensure they pass on both.
127
121
128
-
**2e.** You’re done! Celebrate! Push your code and announce to the world! Hashtags
122
+
**2e**. You're done! Celebrate! Push your code and announce to the world! Hashtags
0 commit comments