Skip to content

Commit 4002f59

Browse files
committed
Docs (mostly): Update references to future.builtins -> builtins
1 parent fdf1ea3 commit 4002f59

Some content is hidden

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

44 files changed

+429
-779
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ develop-eggs
2222
lib
2323
lib64
2424

25+
# Backup files
26+
*.bak
27+
*.backup
28+
2529
# Installer logs
2630
pip-log.txt
2731

README.rst

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,13 @@ Features
2525
.. image:: https://travis-ci.org/PythonCharmers/python-future.svg?branch=master
2626
:target: https://travis-ci.org/PythonCharmers/python-future
2727

28-
- ``future.builtins`` package provides backports and remappings for 20
29-
builtins with different semantics on Py3 versus Py2
28+
- ``future.builtins`` package (also available as ``builtins`` on Py2) provides
29+
backports and remappings for 20 builtins with different semantics on Py3
30+
versus Py2
3031

3132
- ``future.standard_library``, in conjunction with ``future.moves``, provides
3233
support for importing standard library modules under their Python 3 names
3334

34-
- ``future.backports`` package provides backports from the Py3.3
35-
standard library
36-
3735
- ``past.builtins`` package provides forward-ports of 19 Python 2 types and
3836
builtin functions. These can aid with per-module code migrations.
3937

@@ -65,8 +63,8 @@ these imports as it does on Python 3.3+:
6563
.. code-block:: python
6664
6765
from __future__ import absolute_import, division, print_function
68-
from future.builtins import (bytes, str, open, super, range,
69-
zip, round, input, int, pow, object)
66+
from builtins import (bytes, str, open, super, range,
67+
zip, round, input, int, pow, object)
7068
7169
# Backported Py3 bytes object
7270
b = bytes(b'ABCD')
@@ -120,7 +118,7 @@ these imports as it does on Python 3.3+:
120118
assert isinstance('blah', str) # only if unicode_literals is in effect
121119
122120
# Py3-style iterators written as new-style classes (subclasses of
123-
# future.builtins.object) are automatically backward compatible with Py2:
121+
# future.types.newobject) are automatically backward compatible with Py2:
124122
class Upper(object):
125123
def __init__(self, iterable):
126124
self._iter = iter(iterable)
@@ -131,19 +129,25 @@ these imports as it does on Python 3.3+:
131129
assert list(Upper('hello')) == list('HELLO')
132130
133131
134-
There is also support for renamed standard library modules in the form of import
135-
hooks. The context-manager form works like this:
132+
There is also support for renamed standard library modules. The recommended
133+
interface works like this:
136134

137135
.. code-block:: python
138136
137+
# Many Py3 module names are supported directly on both Py2.x and 3.x:
138+
from http.client import HttpConnection
139+
import html.parser
140+
import queue
141+
import xmlrpc.client
142+
143+
# Refactored modules with clashing names on Py2 and Py3 are supported
144+
# as follows:
139145
from future import standard_library
146+
standard_library.install_aliases()
140147
141-
with standard_library.hooks():
142-
from http.client import HttpConnection
143-
from itertools import filterfalse
144-
import html.parser
145-
import queue
146-
from urllib.request import urlopen
148+
# Then, as usual:
149+
from itertools import filterfalse
150+
from urllib.request import urlopen
147151
148152
149153
Automatic conversion to Py2/3-compatible code
@@ -190,9 +194,9 @@ into this code which runs on both Py2 and Py3:
190194
.. code-block:: python
191195
192196
from __future__ import print_function
193-
from future.builtins import input
194197
from future import standard_library
195-
standard_library.install_hooks()
198+
standard_library.install_aliases()
199+
from builtins import input
196200
import queue
197201
from urllib.request import urlopen
198202

docs/bytes_object.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ Python 2's :class:`str`, rather than a true implementation of the Python
1111

1212
:mod:`future` contains a backport of the :mod:`bytes` object from Python 3
1313
which passes most of the Python 3 tests for :mod:`bytes`. (See
14-
``future/tests/test_bytes.py`` in the source tree.) You can use it as
14+
``tests/test_future/test_bytes.py`` in the source tree.) You can use it as
1515
follows::
1616

17-
>>> from future.builtins import bytes
17+
>>> from builtins import bytes
1818
>>> b = bytes(b'ABCD')
1919

2020
On Py3, this is simply the builtin :class:`bytes` object. On Py2, this
@@ -53,7 +53,7 @@ Currently the easiest way to ensure identical behaviour of byte-strings
5353
in a Py2/3 codebase is to wrap all byte-string literals ``b'...'`` in a
5454
:func:`~bytes` call as follows::
5555
56-
from future.builtins import bytes
56+
from builtins import bytes
5757
5858
# ...
5959

docs/custom_iterators.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ to retrieve the next item across Py3 and Py2. On Python 3 it is ``__next__``,
88
whereas on Python 2 it is ``next``.
99

1010
The most elegant solution to this is to derive your custom iterator class from
11-
``future.builtins.object`` and define a ``__next__`` method as you normally
11+
``builtins.object`` and define a ``__next__`` method as you normally
1212
would on Python 3. On Python 2, ``object`` then refers to the
1313
``future.types.newobject`` base class, which provides a fallback ``next``
1414
method that calls your ``__next__``. Use it as follows::
1515

16-
from future.builtins import object
16+
from builtins import object
1717
1818
class Upper(object):
1919
def __init__(self, iterable):
@@ -31,10 +31,10 @@ method that calls your ``__next__``. Use it as follows::
3131

3232
You can use this approach unless you are defining a custom iterator as a
3333
subclass of a base class defined elsewhere that does not derive from
34-
``future.builtins.object``. In that case, you can provide compatibility across
34+
``newobject``. In that case, you can provide compatibility across
3535
Python 2 and Python 3 using the ``next`` function from ``future.builtins``::
3636

37-
from future.builtins import next
37+
from builtins import next
3838

3939
from some_module import some_base_class
4040

docs/dev_notes.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Notes
2+
-----
3+
This module only supports Python 2.6, Python 2.7, and Python 3.1+.
4+
5+
The following renames are already supported on Python 2.7 without any
6+
additional work from us::
7+
8+
reload() -> imp.reload()
9+
reduce() -> functools.reduce()
10+
StringIO.StringIO -> io.StringIO
11+
Bytes.BytesIO -> io.BytesIO
12+
13+
Old things that can one day be fixed automatically by futurize.py::
14+
15+
string.uppercase -> string.ascii_uppercase # works on either Py2.7 or Py3+
16+
sys.maxint -> sys.maxsize # but this isn't identical
17+
18+
TODO: Check out these:
19+
Not available on Py2.6:
20+
unittest2 -> unittest?
21+
buffer -> memoryview?
22+
23+

docs/development.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
.. developer-docs
2+
3+
Developer docs
4+
==============
5+
6+
The easiest way to start developing ``python-future`` is as follows:
7+
8+
1. Install Anaconda Python distribution
9+
10+
2. Run::
11+
12+
conda install -n future2 python=2.7 pip
13+
conda install -n future3 python=3.3 pip
14+
15+
git clone https://github.com/PythonCharmers/python-future
16+
17+
3. If you are using Anaconda Python distribution, this comes without a ``test``
18+
module on Python 2.x. Copy ``Python-2.7.6/Lib/test`` from the Python source tree
19+
to ``~/anaconda/envs/yourenvname/lib/python2.7/site-packages/`.
20+
21+
22+

docs/dict_object.rst

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ stick with standard Python 3 code in your Py2/3 compatible codebase::
2525
In this case there will be memory overhead of list creation for each call to
2626
``items``, ``values`` or ``keys``.
2727

28-
For improved efficiency, ``future.builtins`` provides a Python 2 ``dict``
29-
subclass whose :func:`keys`, :func:`values`, and :func:`items` methods return
30-
iterators on all versions of Python >= 2.6. On Python 2.7, these iterators also
31-
have the same set-like view behaviour as dictionaries in Python 3. This can
32-
streamline code that iterates over large dictionaries. For example::
28+
For improved efficiency, ``future.builtins`` (aliased to ``builtins``) provides
29+
a Python 2 ``dict`` subclass whose :func:`keys`, :func:`values`, and
30+
:func:`items` methods return iterators on all versions of Python >= 2.6. On
31+
Python 2.7, these iterators also have the same set-like view behaviour as
32+
dictionaries in Python 3. This can streamline code that iterates over large
33+
dictionaries. For example::
3334

3435
from __future__ import print_function
35-
from future.builtins import dict, range
36+
from builtins import dict, range
3637
3738
# Memory-efficient construction:
3839
d = dict((i, i**2) for i in range(10**7))
@@ -46,16 +47,16 @@ streamline code that iterates over large dictionaries. For example::
4647
On Python 2.6, these methods currently return iterators but do not support the
4748
new Py3 set-like behaviour.
4849

49-
As usual, on Python 3 ``future.builtins.dict`` is just the built-in ``dict``
50-
class.
50+
As usual, on Python 3 ``dict`` imported from either ``builtins`` or
51+
``future.builtins`` is just the built-in ``dict`` class.
5152

5253

5354
Memory-efficiency and alternatives
5455
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5556

5657
If you already have large native dictionaries, the downside to wrapping them in
57-
a ``dict`` call is that memory is copied (on both Py3 and with
58-
``future.builtins.dict``). For example::
58+
a ``dict`` call is that memory is copied (on both Py3 and on Py2). For
59+
example::
5960

6061
# This allocates and then frees a large amount of temporary memory:
6162
d = dict({i: i**2 for i in range(10**7)})
@@ -72,7 +73,7 @@ The memory-efficient (and CPU-efficient) alternatives are:
7273
d = dict((i, i**2) for i in range(10**7)
7374

7475
- to construct an empty dictionary with a ``dict()`` call using
75-
``future.builtins.dict`` (rather than ``{}``) and then update it;
76+
``builtins.dict`` (rather than ``{}``) and then update it;
7677

7778
- to use the ``viewitems`` etc. functions from :mod:`future.utils`, passing in
7879
regular dictionaries::

docs/future-builtins.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
``future.builtins``
44
===================
55

6+
The ``future.builtins`` module is also accessible as ``builtins`` on Py2.
7+
68
- ``pow()`` supports fractional exponents of negative numbers like in Py3::
79

8-
>>> from future.builtins import pow
10+
>>> from builtins import pow
911
>>> pow(-1, 0.5)
1012
(6.123233995736766e-17+1j)
1113

1214
- ``round()`` uses Banker's Rounding as in Py3 to the nearest even last digit::
1315

14-
>>> from future.builtins import round
16+
>>> from builtins import round
1517
>>> assert round(0.1250, 2) == 0.12
1618

docs/futurize.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ For example::
192192

193193
would be converted by Stage 2 to this code::
194194

195-
from future.builtins import input
196-
from future.builtins import str
195+
from builtins import input
196+
from builtins import str
197197
from future.utils import iteritems, python_2_unicode_compatible
198198

199199
name = input('What is your name?\n')
@@ -290,7 +290,7 @@ that these types behave similarly on Python 2 as on Python 3, also wrap
290290
byte-strings or text in the ``bytes`` and ``str`` types from ``future``. For
291291
example::
292292

293-
from future.builtins import bytes, str
293+
from builtins import bytes, str
294294
b = bytes(b'\x00ABCD')
295295
s = str(u'This is normal text')
296296

docs/futurize_cheatsheet.rst

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,15 @@ changes to all Python source files recursively with::
8888
To apply the changes, add the ``-w`` argument.
8989

9090
This stage makes further conversions needed to support both Python 2 and 3.
91-
These will likely require imports from ``future``, such as::
91+
These will likely require imports from ``future`` on Py2 (and sometimes on Py3),
92+
such as::
9293

9394
from future import standard_library
94-
standard_library.install_hooks()
95-
from future.builtins import bytes
96-
from future.builtins import open
95+
standard_library.install_aliases()
96+
# ...
97+
from builtins import bytes
98+
from builtins import open
99+
from future.utils import with_metaclass
97100

98101
Optionally, you can use the ``--unicode-literals`` flag to add this import to
99102
the top of each module::

0 commit comments

Comments
 (0)