Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5632aff

Browse files
committed
Merge with 3.4 for porting HOWTO tweaks
2 parents 65e4cb1 + 90783eb commit 5632aff

1 file changed

Lines changed: 23 additions & 8 deletions

File tree

Doc/howto/pyporting.rst

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,18 @@ are:
3030

3131
#. Update your code to drop support for Python 2.5 or older (supporting only
3232
Python 2.7 is ideal)
33-
#. Make sure you have good test coverage (coverage.py_ can help)
33+
#. Make sure you have good test coverage (coverage.py_ can help;
34+
``pip install coverage``)
3435
#. Learn the differences between Python 2 & 3
35-
#. Use Modernize_ or Futurize_ to update your code
36+
#. Use Modernize_ or Futurize_ to update your code (``pip install modernize`` or
37+
``pip install future``, respectively)
3638
#. Use Pylint_ to help make sure you don't regress on your Python 3 support
37-
(if only supporting Python 2.7/3.4 or newer)
39+
(if only supporting Python 2.7/3.4 or newer; ``pip install pylint``)
3840
#. Use caniusepython3_ to find out which of your dependencies are blocking your
39-
use of Python 3
41+
use of Python 3 (``pip install caniusepython3``)
4042
#. Once your dependencies are no longer blocking you, use continuous integration
4143
to make sure you stay compatible with Python 2 & 3 (tox_ can help test
42-
against multiple versions of Python)
44+
against multiple versions of Python; ``pip install tox``)
4345

4446
If you are dropping support for Python 2 entirely, then after you learn the
4547
differences between Python 2 & 3 you can run 2to3_ over your code and skip the
@@ -71,8 +73,9 @@ Drop support for Python 2.5 and older (at least)
7173
While you can make Python 2.5 work with Python 3, it is **much** easier if you
7274
only have to work with Python 2.6 or newer (and easier still if you only have
7375
to work with Python 2.7). If dropping Python 2.5 is not an option then the six_
74-
project can help you support Python 2.5 & 3 simultaneously. Do realize, though,
75-
that nearly all the projects listed in this HOWTO will not be available to you.
76+
project can help you support Python 2.5 & 3 simultaneously
77+
(``pip install six``). Do realize, though, that nearly all the projects listed
78+
in this HOWTO will not be available to you.
7679

7780
If you are able to only support Python 2.6 or newer, then the required changes
7881
to your code should continue to look and feel like idiomatic Python code. At
@@ -115,7 +118,8 @@ Python 3! But to fully understand how your code is going to change and what
115118
you want to look out for while you code, you will want to learn what changes
116119
Python 3 makes in terms of Python 2. Typically the two best ways of doing that
117120
is reading the `"What's New"`_ doc for each release of Python 3 and the
118-
`Porting to Python 3`_ book (which is free online).
121+
`Porting to Python 3`_ book (which is free online). There is also a handy
122+
`cheat sheet`_ from the Python-Future project.
119123

120124

121125
Update your code
@@ -245,6 +249,16 @@ for opening files instead of the built-in :func:`open` function as the :mod:`io`
245249
module is consistent from Python 2 to 3 while the built-in :func:`open` function
246250
is not (in Python 3 it's actually :func:`io.open`).
247251

252+
The constructors of both ``str`` and ``bytes`` have different semantics for the
253+
same arguments between Python 2 & 3. Passing an integer to ``bytes`` in Python 2
254+
will give you the string representation of the integer: ``bytes(3) == '3'``.
255+
But in Python 3, an integer argument to ``bytes`` will give you a bytes object
256+
as long as the integer specified, filled with null bytes:
257+
``bytes(3) == b'\x00\x00\x00'``. A similar worry is necessary when passing a
258+
bytes object to ``str``. In Python 2 you just get the bytes object back:
259+
``str(b'3') == b'3'``. But in Python 3 you get the string representation of the
260+
bytes object: ``str(b'3') == "b'3'"``.
261+
248262
Finally, the indexing of binary data requires careful handling (slicing does
249263
**not** require any special handling). In Python 2,
250264
``b'123'[1] == b'2'`` while in Python 3 ``b'123'[1] == 50``. Because binary data
@@ -362,6 +376,7 @@ supported by Python 2. You should also update the classifiers in your
362376

363377
.. _2to3: https://docs.python.org/3/library/2to3.html
364378
.. _caniusepython3: https://pypi.python.org/pypi/caniusepython3
379+
.. _cheat sheet: http://python-future.org/compatible_idioms.html
365380
.. _coverage.py: https://pypi.python.org/pypi/coverage
366381
.. _Futurize: http://python-future.org/automatic_conversion.html
367382
.. _Modernize:

0 commit comments

Comments
 (0)