|
3 | 3 | Python 3 to Python 2. |
4 | 4 |
|
5 | 5 | For example: |
6 | | -- a Python 2 backport of the range iterator from Py3 with slicing |
7 | | - support. |
| 6 | +- an implementation of Python 3's bytes object (pure Python subclass of |
| 7 | + Python 2's builtin 8-bit str type) |
| 8 | +- a backport of the range iterator from Py3 with slicing support |
8 | 9 | - the magic zero-argument super() function |
9 | 10 | - the new round() behaviour |
10 | 11 |
|
11 | 12 | It is used as follows:: |
12 | 13 |
|
13 | 14 | from __future__ import division, absolute_import, print_function |
14 | | - from future.builtins.backports import range, super, round |
| 15 | + from future.builtins.backports import bytes, range, super, round |
15 | 16 |
|
16 | 17 | to bring in the new semantics for these functions from Python 3. And |
17 | 18 | then, for example:: |
18 | 19 | |
| 20 | + b = bytes(b'ABCD') |
| 21 | + assert list(b) == [65, 66, 67, 68] |
| 22 | + assert repr(b) == "b'ABCD'" |
| 23 | + assert [65, 66] in b |
| 24 | +
|
| 25 | + # These raise TypeErrors: |
| 26 | + # b + u'EFGH' |
| 27 | + # b.split(u'B') |
| 28 | + # bytes(b',').join([u'Fred', u'Bill']) |
| 29 | +
|
19 | 30 | for i in range(10**11)[:10]: |
20 | 31 | pass |
21 | 32 |
|
@@ -47,10 +58,10 @@ def append(self, item): |
47 | 58 | Like the new ``input()`` function from Python 3 (without eval()), except |
48 | 59 | that it returns bytes. Equivalent to Python 2's ``raw_input()``. |
49 | 60 |
|
50 | | -By default, the old Python 2 input() is **removed** from ``__builtin__`` |
51 | | -for safety (because it may otherwise lead to shell injection on Python 2 |
52 | | -if used accidentally after forgetting to import the replacement for some |
53 | | -reason. |
| 61 | +Warning: By default, importing this module *removes* the old Python 2 |
| 62 | +input() function entirely from ``__builtin__`` for safety. This is |
| 63 | +because forgetting to import the new ``input`` from ``future`` might |
| 64 | +otherwise lead to a security vulnerability (shell injection) on Python 2. |
54 | 65 |
|
55 | 66 | To restore it, you can retrieve it yourself from |
56 | 67 | ``__builtin__._old_input``. |
@@ -78,13 +89,14 @@ def append(self, item): |
78 | 89 |
|
79 | 90 | if utils.PY3: |
80 | 91 | import builtins |
| 92 | + bytes = builtins.bytes |
81 | 93 | range = builtins.range |
82 | 94 | super = builtins.super |
83 | 95 | round = builtins.round |
84 | 96 | __all__ = [] |
85 | 97 | else: |
| 98 | + from .newbytes import bytes |
86 | 99 | from .newrange import range |
87 | 100 | from .newsuper import super |
88 | 101 | from .newround import round |
89 | | - |
90 | | - __all__ = ['range', 'super', 'round'] |
| 102 | + __all__ = ['bytes', 'range', 'super', 'round'] |
0 commit comments