@@ -5,8 +5,8 @@ Automatic conversion to Py2/3 with ``futurize`` and ``pasteurize``
55
66The ``future `` source tree includes scripts called ``futurize `` and
77``pasteurize `` to aid in making Python 2 code or Python 3 code compatible with
8- both platforms (Py2&3) using the :mod: `future ` module. It is based on 2to3 and
9- uses fixers from ``lib2to3 ``, ``lib3to2 ``, and ``python-modernize ``.
8+ both platforms (Py2&3) using the :mod: `future ` module. These are based on
9+ `` lib2to3 `` and use fixers from ``2to3 ``, ``3to2 ``, and ``python-modernize ``.
1010
1111``futurize `` passes Python 2 code through all the appropriate fixers to turn it
1212into valid Python 3 code, and then adds ``__future__ `` and ``future `` package
@@ -19,276 +19,10 @@ instead. This converts Py3-only constructs (e.g. new metaclass syntax) and adds
1919In both cases, the result should be relatively clean Py3-style code that runs
2020mostly unchanged on both Python 2 and Python 3.
2121
22- .. _forwards-conversion :
2322
24- Futurize: 2 to both
25- --------------------
23+ .. include :: futurize.rst
2624
27- For example, running ``futurize `` turns this Python 2 code::
28-
29- import ConfigParser
30-
31- class Blah(object):
32- pass
33- print 'Hello',
34-
35- into this code which runs on both Py2 and Py3::
36-
37- from __future__ import print_function
38- from future import standard_library
39-
40- import configparser
41-
42- class Blah(object):
43- pass
44- print('Hello', end=' ')
45-
46-
47- To write out all the changes to your Python files that ``futurize `` suggests,
48- use the ``-w `` flag.
49-
50- For complex projects, it may be better to divide the porting into two stages.
51- Stage 1 is for "safe" changes that modernize the code but do not break Python
52- 2.6 compatibility or introduce a depdendency on the ``future `` package. Stage 2
53- is to complete the process.
54-
55-
56- .. _forwards-conversion-stage1 :
57-
58- Stage 1: "safe" fixes
59- ~~~~~~~~~~~~~~~~~~~~~
60-
61- Run with::
62-
63- futurize --stage1
64-
65- This applies fixes that modernize Python 2 code without changing the effect of
66- the code. With luck, this will not introduce any bugs into the code, or will at
67- least be trivial to fix. The changes are those that bring the Python code
68- up-to-date without breaking Py2 compatibility. The resulting code will be
69- modern Python 2.6-compatible code plus ``__future__ `` imports from the
70- following set::
71-
72- from __future__ import absolute_import
73- from __future__ import division
74- from __future__ import print_function
75-
76- Only those ``__future__ `` imports deemed necessary will be added unless
77- the ``--all-imports `` command-line option is passed to ``futurize ``, in
78- which case they are all added.
79-
80- The ``from __future__ import unicode_literals `` declaration is not added
81- unless the ``--unicode-literals `` flag is passed to ``futurize ``.
82-
83- The changes include::
84-
85- - except MyException, e:
86- + except MyException as e:
87-
88- - print >>stderr, "Blah"
89- + from __future__ import print_function
90- + print("Blah", stderr)
91-
92- Implicit relative imports fixed, e.g.::
93-
94- - import mymodule
95- + from __future__ import absolute_import
96- + from . import mymodule
97-
98- .. and all unprefixed string literals '...' gain a b prefix to be b'...'.
99-
100- .. (This last step can be prevented using --no-bytes-literals if you already have b'...' markup in your code, whose meaning would otherwise be lost.)
101-
102- Stage 1 does not add any imports from the ``future `` package. The output of
103- stage 1 will probably not (yet) run on Python 3.
104-
105- The goal for this stage is to create most of the ``diff `` for the entire
106- porting process, but without introducing any bugs. It should be uncontroversial
107- and safe to apply to every Python 2 package. The subsequent patches introducing
108- Python 3 compatibility should then be shorter and easier to review.
109-
110-
111- .. _forwards-conversion-stage2 :
112-
113- Stage 2: Py3-style code with ``future `` wrappers for Py2
114- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
115-
116- Run with::
117-
118- futurize —-stage2 myfolder/*.py
119-
120- This stage adds a dependency on the ``future `` package. The goal for stage 2 is
121- to make further mostly safe changes to the Python 2 code to use Python 3-style
122- code that then still runs on Python 2 with the help of the appropriate builtins
123- and utilities in ``future ``.
124-
125- For example::
126-
127- name = raw_input('What is your name?\n')
128-
129- for k, v in d.iteritems():
130- assert isinstance(v, basestring)
131-
132- class MyClass(object):
133- def __unicode__(self):
134- return u'My object'
135- def __str__(self):
136- return unicode(self).encode('utf-8')
137-
138- would be converted by Stage 2 to this code::
139-
140- from future.builtins import input
141- from future.builtins import str
142- from future.utils import iteritems, python_2_unicode_compatible
143-
144- name = input('What is your name?\n')
145-
146- for k, v in iteritems(d):
147- assert isinstance(v, (str, bytes))
148-
149- @python_2_unicode_compatible
150- class MyClass(object):
151- def __str__(self):
152- return u'My object'
153-
154- Stage 2 also renames standard-library imports to their Py3 names and adds these
155- two lines::
156-
157- from future import standard_library
158- standard_library.install_hooks()
159-
160- For example::
161-
162- import ConfigParser
163-
164- becomes::
165-
166- from future import standard_library
167- standard_library.install_hooks()
168- import ConfigParser
169-
170-
171- Ideally the output of this stage should not be a ``SyntaxError `` on either
172- Python 3 or Python 2.
173-
174- After this, you can run your tests on Python 3 and make further code changes
175- until they pass on Python 3.
176-
177- The next step would be manually adding some decorators from ``future `` to
178- e-enable Python 2 compatibility. See :ref: `what-else ` for more info.
179-
180-
181-
182- .. _forwards-conversion-text :
183-
184- Separating text from bytes
185- ~~~~~~~~~~~~~~~~~~~~~~~~~~
186-
187- After applying stage 2, the recommended step is to decide which of your Python
188- 2 strings represent text and which represent binary data and to prefix all
189- string literals with either ``b `` or ``u `` accordingly. Furthermore, to ensure
190- that these types behave similarly on Python 2 as on Python 3, also wrap
191- byte-strings or text in the ``bytes `` and ``str `` types from ``future ``. For
192- example::
193-
194- from future.builtins import bytes, str
195- b = bytes(b'\x00ABCD')
196- s = str(u'This is normal text')
197-
198- Any unadorned string literals will then represent native platform strings
199- (byte-strings on Py2, unicode strings on Py3).
200-
201- An alternative is to pass the ``--unicode_literals `` flag::
202-
203- $ futurize --unicode_literals mypython2script.py
204-
205- After runnign this, all string literals that were not explicitly marked up as
206- ``b'' `` will mean text (Python 3 ``str `` or Python 2 ``unicode ``).
207-
208-
209- .. _forwards-conversion-stage3 :
210-
211- Post-conversion
212- ~~~~~~~~~~~~~~~
213-
214- After running ``futurize ``, we recommend first getting the tests passing on
215- Py3, and then on Py2 again with the help of the ``future `` package.
216-
217-
218- .. _backwards-conversion :
219-
220- Pasteurize: 3 to both
221- --------------------
222-
223- Running ``pasteurize -w mypy3module.py `` turns this Python 3 code::
224-
225- import configparser
226-
227- class Blah:
228- pass
229- print('Hello', end=None)
230-
231- into this code which runs on both Py2 and Py3::
232-
233- from __future__ import print_function
234- from future import standard_library
235- standard_library.install_hooks()
236-
237- import configparser
238-
239- class Blah(object):
240- pass
241- print('Hello', end=None)
242-
243- Notice that both ``futurize `` and ``pasteurize `` create explicit new-style
244- classes that inherit from ``object `` on both Python versions, and both
245- refer to stdlib modules (as well as builtins) under their Py3 names.
246-
247- ``pasteurize `` also handles the following Python 3 features:
248-
249- - keyword-only arguments
250- - metaclasses (using :func: `~future.utils.with_metaclass `)
251- - extended tuple unpacking (PEP 3132)
252-
253- To handle function annotations (PEP 3107), see :ref: `func_annotations `.
254-
255-
256- How well do ``futurize `` and ``pasteurize `` work?
257- -------------------------------------------------
258-
259- They are still incomplete and make some mistakes, like 2to3, on which they are
260- based.
261-
262- Nevertheless, ``futurize `` and ``pasteurize `` are useful to automate much of the
263- work of porting, particularly the boring repetitive text substitutions. They also
264- help to flag which parts of the code require attention.
265-
266- Please report bugs on `GitHub
267- <https://github.com/PythonCharmers/python-future/> `_.
268-
269- Contributions to the ``lib2to3 ``-based fixers for ``futurize `` and
270- ``pasteurize `` are particularly welcome! Please see :ref: `contributing `.
271-
272-
273- .. _futurize-limitations
274-
275- Known limitations of ``futurize ``
276- ---------------------------------
277-
278- ``futurize `` doesn't currently make any of these changes automatically::
279-
280- 1. A source encoding declaration line like::
281-
282- # -*- coding:utf-8 -*-
283-
284- is not kept at the top of a file. It must be moved manually back to line 1 to take effect.
285-
286- 2. Strings containing ``\U `` produce a ``SyntaxError `` on Python 3. An example is::
287-
288- s = 'C:\Users'.
289-
290- Python 2 expands this to ``s = 'C:\\Users'``, but Python 3 requires a raw
291- prefix (``r'...'``). This also applies to multi-line strings (including
292- multi-line docstrings).
25+ .. include :: pasteurize.rst
29326
27+ .. include :: conversion_limitations.rst
29428
0 commit comments