Skip to content

Commit fd44b5e

Browse files
committed
Docs: improve organization
Consolidate several smaller sections into larger ones
1 parent 3d17779 commit fd44b5e

11 files changed

+195
-206
lines changed

docs/automatic_conversion.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ mostly unchanged on both Python 2 and Python 3.
2222

2323
.. include:: futurize.rst
2424

25+
.. include:: futurize_cheatsheet.rst
26+
2527
.. include:: pasteurize.rst
2628

2729
.. include:: conversion_limitations.rst

docs/contents.rst.inc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ Contents:
99
quickstart
1010
compatible_idioms
1111
imports
12-
standard_library_imports
1312
what_else
1413
automatic_conversion
15-
futurize_cheatsheet
16-
translation
17-
stdlib_incompatibilities
1814
faq
15+
stdlib_incompatibilities
16+
older_interfaces
1917
changelog
2018
credits
2119
reference

docs/conversion_limitations.rst

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
1-
How well do ``futurize`` and ``pasteurize`` work?
2-
-------------------------------------------------
1+
.. _futurize-limitations:
32

4-
They are still incomplete and make some mistakes, like 2to3, on which they are
5-
based.
3+
Known limitations
4+
-----------------
65

7-
Nevertheless, ``futurize`` and ``pasteurize`` are useful to automate much of the
6+
``futurize`` and ``pasteurize`` are useful to automate much of the
87
work of porting, particularly the boring repetitive text substitutions. They also
98
help to flag which parts of the code require attention.
109

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
21-
-----------------
10+
Nevertheless, ``futurize`` and ``pasteurize`` are still incomplete and make
11+
some mistakes, like 2to3, on which they are based. Please report bugs on
12+
`GitHub <https://github.com/PythonCharmers/python-future/>`_. Contributions to
13+
the ``lib2to3``-based fixers for ``futurize`` and ``pasteurize`` are
14+
particularly welcome! Please see :ref:`contributing`.
2215

23-
``futurize`` doesn't currently make this change automatically:
16+
``futurize`` doesn't currently make the following change automatically:
2417

2518
1. Strings containing ``\U`` produce a ``SyntaxError`` on Python 3. An example is::
2619

docs/futurize_cheatsheet.rst

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
.. _futurize_cheatsheet:
22

3-
``futurize`` quick-start: automatic conversion from Py2 to Py2/3
4-
================================================================
3+
``futurize`` quick-start guide
4+
------------------------------
55

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``:
6+
How to convert Py2 code to Py2/3 code using ``futurize``:
87

98
.. _porting-setup:
109

1110
Step 0: setup
12-
-------------
11+
~~~~~~~~~~~~~
1312

1413
Step 0 goal: set up and see the tests passing on Python 2 and failing on Python 3.
1514

@@ -23,7 +22,7 @@ d. Install Python 3.3 with e.g. ``sudo apt-get install python3``. On other platf
2322
.. _porting-step1:
2423

2524
Step 1: modern Py2 code
26-
-----------------------
25+
~~~~~~~~~~~~~~~~~~~~~~~
2726

2827
The goal for this step is to modernize the Python 2 code without introducing any dependencies (on ``future`` or e.g. ``six``) at this stage.
2928

@@ -45,7 +44,7 @@ See :ref:`forwards-conversion-stage1` for more info.
4544

4645

4746
Example error
48-
~~~~~~~~~~~~~
47+
*************
4948

5049
One relatively common error after conversion is::
5150

@@ -71,7 +70,7 @@ imports.)
7170
.. _porting-step2:
7271

7372
Step 2: working Py3 code that still supports Py2
74-
------------------------------------------------
73+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7574

7675
The goal for this step is to get the tests passing first on Py3 and then on Py2
7776
again with the help of the ``future`` package.

docs/imports.rst

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,12 @@ certain automated code-analysis tools.
6060

6161
The complete set of imports of builtins from ``future`` is::
6262
63-
from future.builtins import (ascii, bytes, chr, dict, filter, hex, input,
64-
int, map, next, oct, open, pow, range, round,
65-
str, super, zip)
66-
67-
The contents of the ``future.builtins`` module are also accessible under the
68-
``builtins`` namespace as follows::
69-
7063
from builtins import (ascii, bytes, chr, dict, filter, hex, input,
7164
int, map, next, oct, open, pow, range, round,
7265
str, super, zip)
7366

67+
These are also available under the ``future.builtins`` namespace for backward compatibility.
68+
7469
Importing only some of the builtins is cleaner but increases the risk of
7570
introducing Py2/3 portability bugs as your code evolves over time. For example,
7671
be aware of forgetting to import ``input``, which could expose a security
@@ -84,12 +79,12 @@ The internal API is currently as follows::
8479

8580
from future.types import bytes, dict, int, range, str
8681
from future.builtins.misc import (ascii, chr, hex, input, next,
87-
oct, open, round, super)
82+
oct, open, pow, round, super)
8883
from future.builtins.iterators import filter, map, zip
8984

90-
To understand the details of the backported builtins on Python 2, see the
91-
docs for these modules. Please note that this internal API is evolving and may
92-
not be stable between different versions of ``future``.
85+
Please note that this internal API is evolving and may not be stable between
86+
different versions of ``future``. To understand the details of the backported
87+
builtins on Python 2, see the docs for these modules.
9388

9489
For more information on what the backported types provide, see :ref:`what-else`.
9590

@@ -99,7 +94,7 @@ For more information on what the backported types provide, see :ref:`what-else`.
9994
.. _obsolete-builtins:
10095

10196
Obsolete Python 2 builtins
102-
~~~~~~~~~~~~~~~~~~~~~~~~~~
97+
__________________________
10398

10499
Twelve Python 2 builtins have been removed from Python 3. To aid with
105100
porting code to Python 3 module by module, you can use the following
@@ -120,5 +115,12 @@ equivalent Python 3 forms and then adds ``future`` imports to resurrect
120115
Python 2 support, as described in :ref:`forwards-conversion-stage2`.
121116

122117

118+
.. include:: standard_library_imports.rst
119+
120+
.. include:: translation.rst
121+
123122
.. include:: unicode_literals.rst
124123

124+
Next steps
125+
----------
126+
See :ref:`what-else`.

docs/older_interfaces.rst

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
.. _older-standard-library-interfaces:
2+
3+
Older interfaces
4+
~~~~~~~~~~~~~~~~
5+
6+
In addition to the direct and ``install_aliases()`` interfaces (described in
7+
:ref:`standard-library-imports`), ``future`` supports four other interfaces to
8+
the reorganized standard library. This is largely for historical reasons (for
9+
versions prior to 0.14).
10+
11+
12+
Context-manager for import hooks
13+
________________________________
14+
15+
The context-manager interface is via a context-manager called ``hooks``::
16+
17+
from future.standard_library import hooks
18+
with hooks():
19+
import socketserver
20+
import queue
21+
import configparser
22+
import test.support
23+
import html.parser
24+
from collections import UserList
25+
from itertools import filterfalse, zip_longest
26+
from http.client import HttpConnection
27+
import urllib.request
28+
# and other moved modules and definitions
29+
30+
This interface is straightforward and effective, using PEP 302 import
31+
hooks.
32+
33+
34+
``future.moves`` interface
35+
__________________________
36+
37+
The ``future.moves`` interface avoids import hooks. It may therefore be more
38+
robust, at the cost of less idiomatic code. Use it as follows::
39+
40+
from future.moves import queue
41+
from future.moves import socketserver
42+
from future.moves.http.client import HTTPConnection
43+
# etc.
44+
45+
If you wish to achieve the effect of a two-level import such as this::
46+
47+
import http.client
48+
49+
portably on both Python 2 and Python 3, note that Python currently does not
50+
support syntax like this::
51+
52+
from future.moves import http.client
53+
54+
One workaround is to replace the dot with an underscore::
55+
56+
import future.moves.http.client as http_client
57+
58+
59+
Comparing future.moves and six.moves
60+
++++++++++++++++++++++++++++++++++++
61+
62+
``future.moves`` and ``six.moves`` provide a similar Python 3-style
63+
interface to the native standard library module definitions.
64+
65+
The major difference is that the ``future.moves`` package is a real Python package
66+
(``future/moves/__init__.py``) with real modules provided as ``.py`` files, whereas
67+
``six.moves`` constructs fake ``_LazyModule`` module objects within the Python
68+
code and injects them into the ``sys.modules`` cache.
69+
70+
The advantage of ``six.moves`` is that the code fits in a single module that can be
71+
copied into a project that seeks to eliminate external dependencies.
72+
73+
The advantage of ``future.moves`` is that it is likely to be more robust in the
74+
face of magic like Django's auto-reloader and tools like ``py2exe`` and
75+
``cx_freeze``. See issues #51, #53, #56, and #63 in the ``six`` project for
76+
more detail of bugs related to the ``six.moves`` approach.
77+
78+
79+
``import_`` and ``from_import`` functions
80+
_________________________________________
81+
82+
The functional interface is to use the ``import_`` and ``from_import``
83+
functions from ``future.standard_library`` as follows::
84+
85+
from future.standard_library import import_, from_import
86+
87+
http = import_('http.client')
88+
urllib = import_('urllib.request')
89+
90+
urlopen, urlsplit = from_import('urllib.request', 'urlopen', 'urlsplit')
91+
92+
This interface also works with two-level imports.
93+
94+
95+
install_hooks() call
96+
____________________
97+
98+
The last interface to the reorganized standard library is via a call to
99+
``install_hooks()``::
100+
101+
from future import standard_library
102+
standard_library.install_hooks()
103+
104+
import urllib
105+
f = urllib.request.urlopen('http://www.python.org/')
106+
107+
standard_library.remove_hooks()
108+
109+
If you use this interface, it is recommended to disable the import hooks again
110+
after use by calling ``remove_hooks()``, in order to prevent the futurized
111+
modules from being invoked inadvertently by other modules. (Python does not
112+
automatically disable import hooks at the end of a module, but keeps them
113+
active for the life of a process unless removed.)
114+
115+
.. The call to ``scrub_future_sys_modules()`` removes any modules from the
116+
.. ``sys.modules`` cache (on Py2 only) that have Py3-style names, like ``http.client``.
117+
.. This can prevent libraries that have their own Py2/3 compatibility code from
118+
.. importing the ``future.moves`` or ``future.backports`` modules unintentionally.
119+
.. Code such as this will then fall through to using the Py2 standard library
120+
.. modules on Py2::
121+
..
122+
.. try:
123+
.. from http.client import HTTPConnection
124+
.. except ImportError:
125+
.. from httplib import HTTPConnection
126+
..
127+
.. **Requests**: The above snippet is from the `requests
128+
.. <http://docs.python-requests.org>`_ library. As of v0.12, the
129+
.. ``future.standard_library`` import hooks are compatible with Requests.
130+
131+
132+
.. If you wish to avoid changing every reference of ``http.client`` to
133+
.. ``http_client`` in your code, an alternative is this::
134+
..
135+
.. from future.standard_library import http
136+
.. from future.standard_library.http import client as _client
137+
.. http.client = client
138+
139+
.. but it has the advantage that it can be used by automatic translation scripts such as ``futurize`` and ``pasteurize``.
140+
141+

docs/quickstart.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ to be accessed under their Python 3 names and locations in Python 2::
123123
import xmlrpc.client
124124
import xmlrpc.server
125125

126-
and others. For a complete list, see :ref:`list-standard-library-renamed`.
126+
and others. For a complete list, see :ref:`direct-imports`.
127127

128128
.. _py2-dependencies:
129129

0 commit comments

Comments
 (0)