33bytes
44-----
55
6- Handling ``bytes `` consistently and correctly has traditionally been one of the
7- most difficult tasks in writing a Py3/2 compatible codebase. This is because
8- the Python 2 `` bytes `` object is simply an alias for Python 2's `` str ``, rather
9- than a true implementation of the Python 3 `` bytes `` object, which is
10- substantially different.
11-
12- ``future `` contains a backport of the `` bytes `` object from Python 3 which
13- passes most of the Python 3 tests for `` bytes ` `. (See
6+ Handling ``bytes `` consistently and correctly has traditionally been one
7+ of the most difficult tasks in writing a Py3/2 compatible codebase. This
8+ is because the Python 2 :class: ` bytes ` object is simply an alias for
9+ Python 2's :class: ` str `, rather than a true implementation of the Python
10+ 3 :class: ` bytes ` object, which is substantially different.
11+
12+ ``future `` contains a backport of the :mod: ` bytes ` object from Python 3
13+ which passes most of the Python 3 tests for :mod: ` bytes `. (See
1414:ref: `bytes-test-results `.) You can use it as follows::
1515
1616 from future.builtins import bytes
1717
1818 b = bytes(b'ABCD')
1919
20- On Py2, this object inherits from Python 2's native `` str `` , but it enforces
21- the much stricter separation from unicode strings that Python 3's `` bytes ``
22- requires::
20+ On Py2, this object inherits from Python 2's :class: ` str `, but it
21+ enforces the much stricter separation from unicode strings that Python
22+ 3's :class: ` bytes ` requires::
2323
2424 >>> b + u'EFGH' # TypeError
2525 Traceback (most recent call last):
@@ -31,16 +31,17 @@ requires::
3131 File "<stdin>", line 1, in <module>
3232 TypeError: sequence item 0: expected bytes, found unicode string
3333
34- In most other ways, these ``bytes `` objects have identical behaviours to Python 3's ``bytes ``::
34+ In most other ways, these :class: `bytes ` objects have identical
35+ behaviours to Python 3's :class: `bytes `::
3536
3637 b = bytes(b'ABCD')
3738 assert list(b) == [65, 66, 67, 68]
3839 assert repr(b) == "b'ABCD'"
3940 assert b.split(b'b') == [b'A', b'CD']
4041
41- Currently the easiest way to ensure identical use of byte-strings compatibly between
42- Python 3 and 2 is to wrap all byte-string literals `` b'...' `` in a `` bytes() ``
43- call, as follows::
42+ Currently the easiest way to ensure identical use of byte-strings
43+ compatibly between Python 3 and 2 is to wrap all byte-string literals
44+ `` b'...' `` in a :func: ` ~bytes()` ` call, as follows::
4445
4546 from future.builtins import *
4647
@@ -50,30 +51,30 @@ call, as follows::
5051
5152 # ...
5253
53- This is not perfect, but it is superior to manually debugging and fixing code
54- incompatibilities caused by the many differences between Py3 bytes and Py2
55- strings.
54+ This is not perfect, but it is superior to manually debugging and fixing
55+ code incompatibilities caused by the many differences between Py3 bytes
56+ and Py2 strings.
5657
5758
5859.. _bytes-test-results :
5960
6061Test results
6162~~~~~~~~~~~~
6263
63- For reference, when not using the backported `` bytes `` object, running the Py3.3
64- ``bytes `` unit tests in ``test_bytes.py `` on Py2 (after fixing imports) gives
65- this::
64+ For reference, when not using the backported :class: ` bytes ` object,
65+ running the Py3.3 ``bytes `` unit tests in ``test_bytes.py `` on Py2 (after
66+ fixing imports) gives this::
6667
6768 --------------------------------------------------------------
6869 Ran 203 tests in 0.209s
6970
7071 FAILED (failures=31, errors=55, skipped=1)
7172 --------------------------------------------------------------
7273
73- The ``future `` backport of the Py3 `` bytes `` object passes most of the Python 3
74- tests for ``bytes `` on Py2, except those requiring specific wording in exception
75- messages.
74+ The ``future `` backport of the Py3 :class: ` bytes ` object passes most of
75+ the Python 3 tests for ``bytes `` on Py2, except those requiring specific
76+ wording in exception messages.
7677
77- See ``future/tests/test_bytes.py `` in the source for the unit tests that are
78- actually run.
78+ See ``future/tests/test_bytes.py `` in the source for the unit tests that
79+ are actually run.
7980
0 commit comments