Skip to content

Commit 7bd9a4a

Browse files
committed
Merge branch 'develop'
2 parents b46a5bf + 063baa1 commit 7bd9a4a

File tree

211 files changed

+9983
-5881
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

211 files changed

+9983
-5881
lines changed

README.rst

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,19 @@ Features
2525
- ``future.builtins`` package provides backports and remappings for 19
2626
builtins with different semantics on Py3 versus Py2
2727

28-
- ``future.standard_library`` package provides backports and remappings from
29-
the Py3 standard library
28+
- ``future.standard_library`` package provides backports from the Py3.3
29+
standard library
30+
31+
- ``future.moves`` package provides support for reorganized standard library
32+
modules (renames from native packages)
3033

3134
- ``past.builtins`` package provides forward-ports of Python 2 types and
3235
resurrects some Python 2 builtins (to aid with per-module code migrations)
3336

3437
- ``past.translation`` package supports transparent translation of Python 2
3538
modules to Python 3 upon import. [This feature is currently in alpha.]
3639

37-
- 640+ unit tests, including many from the Py3.3 source tree.
40+
- 800+ unit tests, including many from the Py3.3 source tree.
3841

3942
- ``futurize`` and ``pasteurize`` scripts based on ``2to3`` and parts of
4043
``3to2`` and ``python-modernize``, for automatic conversion from either Py2
@@ -135,7 +138,7 @@ hooks. The context-manager form works like this::
135138
import queue
136139

137140

138-
Automatic conversion to Py3/2-compatible code
141+
Automatic conversion to Py2/3-compatible code
139142
=============================================
140143

141144
``future`` comes with two scripts called ``futurize`` and

TESTING.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
Currently three tests have errors on Py2.7 with module import errors (http.client
2-
and test.support) when the test suite is run with:
1+
Currently the tests are passing on OS X on Python 2.6, 2.7 and 3.3.
2+
3+
On Linux they are currently failing with ImportErrors for test.support when the
4+
test suite is run with:
35

46
$ python setup.py test
57

docs/automatic_conversion.rst

Lines changed: 5 additions & 271 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ Automatic conversion to Py2/3 with ``futurize`` and ``pasteurize``
55

66
The ``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
1212
into 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
1919
In both cases, the result should be relatively clean Py3-style code that runs
2020
mostly 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

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
# Note the "1" or "True" value above as the third argument to indicate
116116
# an arbitrary url.
117117
'navbar_links': [
118-
("Overview", "overview.html"),
118+
("Overview", "overview"),
119119
("FAQ", "faq.html", True),
120120
# ("Link", "http://example.com", True),
121121
],

docs/contents.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Contents:
77
overview
88
quickstart
99
imports
10+
standard_library_imports
1011
what_else
1112
automatic_conversion
1213
porting

docs/conversion_limitations.rst

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
How well do ``futurize`` and ``pasteurize`` work?
2+
-------------------------------------------------
3+
4+
They are still incomplete and make some mistakes, like 2to3, on which they are
5+
based.
6+
7+
Nevertheless, ``futurize`` and ``pasteurize`` are useful to automate much of the
8+
work of porting, particularly the boring repetitive text substitutions. They also
9+
help to flag which parts of the code require attention.
10+
11+
Please report bugs on `GitHub
12+
<https://github.com/PythonCharmers/python-future/>`_.
13+
14+
Contributions to the ``lib2to3``-based fixers for ``futurize`` and
15+
``pasteurize`` are particularly welcome! Please see :ref:`contributing`.
16+
17+
18+
.. _futurize-limitations
19+
20+
Known limitations of ``futurize``
21+
---------------------------------
22+
23+
``futurize`` doesn't currently make any of these changes automatically::
24+
25+
1. A source encoding declaration line like::
26+
27+
# -*- coding:utf-8 -*-
28+
29+
is not kept at the top of a file. It must be moved manually back to line 1 to take effect.
30+
31+
2. Strings containing ``\U`` produce a ``SyntaxError`` on Python 3. An example is::
32+
33+
s = 'C:\Users'.
34+
35+
Python 2 expands this to ``s = 'C:\\Users'``, but Python 3 requires a raw
36+
prefix (``r'...'``). This also applies to multi-line strings (including
37+
multi-line docstrings).
38+
39+

docs/dict_object.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ stick with standard Python 3 code in your Py2/3 compatible codebase::
1313
1414
# Assuming d is a native dict ...
1515

16-
for item in d:
16+
for key in d:
1717
# code here
1818

1919
for item in d.items():

0 commit comments

Comments
 (0)