X Tutup
Skip to content

Commit fe106f4

Browse files
committed
Merge branch 'bytes2'
2 parents 6ef1ee2 + 93780d2 commit fe106f4

20 files changed

+2513
-203
lines changed

future/builtins/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from future.builtins.iterators import (filter, map, zip)
1616
from future.builtins.misc import (ascii, oct, hex, chr, int, input, open)
17-
from future.builtins.backports import (round, range, super)
17+
from future.builtins.backports import (bytes, round, range, super)
1818
from future.builtins.str_is_unicode import str
1919
from future import utils
2020

@@ -29,7 +29,7 @@
2929
# Only shadow builtins on Py2; no new names
3030
__all__ = ['filter', 'map', 'zip',
3131
'ascii', 'oct', 'hex', 'chr', 'int', 'input', 'open',
32-
'round', 'range', 'super',
32+
'bytes', 'round', 'range', 'super',
3333
'apply', 'cmp', 'coerce', 'execfile', 'file', 'long',
3434
'raw_input', 'reduce', 'reload', 'unicode', 'xrange',
3535
'str',

future/builtins/backports/__init__.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,30 @@
33
Python 3 to Python 2.
44
55
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
89
- the magic zero-argument super() function
910
- the new round() behaviour
1011
1112
It is used as follows::
1213
1314
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
1516
1617
to bring in the new semantics for these functions from Python 3. And
1718
then, for example::
1819
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+
1930
for i in range(10**11)[:10]:
2031
pass
2132
@@ -47,10 +58,10 @@ def append(self, item):
4758
Like the new ``input()`` function from Python 3 (without eval()), except
4859
that it returns bytes. Equivalent to Python 2's ``raw_input()``.
4960
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.
5465
5566
To restore it, you can retrieve it yourself from
5667
``__builtin__._old_input``.
@@ -78,13 +89,14 @@ def append(self, item):
7889

7990
if utils.PY3:
8091
import builtins
92+
bytes = builtins.bytes
8193
range = builtins.range
8294
super = builtins.super
8395
round = builtins.round
8496
__all__ = []
8597
else:
98+
from .newbytes import bytes
8699
from .newrange import range
87100
from .newsuper import super
88101
from .newround import round
89-
90-
__all__ = ['range', 'super', 'round']
102+
__all__ = ['bytes', 'range', 'super', 'round']

0 commit comments

Comments
 (0)
X Tutup