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

Skip to content

Commit 2cb2fa9

Browse files
committed
Consistent heading spacing, and fix two typos.
1 parent ef31657 commit 2cb2fa9

1 file changed

Lines changed: 31 additions & 2 deletions

File tree

Doc/howto/pyporting.rst

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Porting Python 2 Code to Python 3
2020

2121
Choosing a Strategy
2222
===================
23+
2324
When a project makes the decision that it's time to support both Python 2 & 3,
2425
a decision needs to be made as to how to go about accomplishing that goal.
2526
The chosen strategy will depend on how large the project's existing
@@ -54,6 +55,7 @@ current best practices in a Python 2/3 compatible way.
5455

5556
Universal Bits of Advice
5657
------------------------
58+
5759
Regardless of what strategy you pick, there are a few things you should
5860
consider.
5961

@@ -128,6 +130,7 @@ and work from there.
128130

129131
Python 3 and 3to2
130132
=================
133+
131134
If you are starting a new project or your codebase is small enough, you may
132135
want to consider writing your code for Python 3 and backporting to Python 2
133136
using 3to2_. Thanks to Python 3 being more strict about things than Python 2
@@ -149,6 +152,7 @@ that 3to2 is not a high-quality project.
149152

150153
Python 2 and 2to3
151154
=================
155+
152156
Included with Python since 2.6, the 2to3_ tool (and :mod:`lib2to3` module)
153157
helps with porting Python 2 to Python 3 by performing various source
154158
translations. This is a perfect solution for projects which wish to branch
@@ -176,6 +180,7 @@ to supporting Python 2 & 3.
176180

177181
Support Python 2.7
178182
------------------
183+
179184
As a first step, make sure that your project is compatible with `Python 2.7`_.
180185
This is just good to do as Python 2.7 is the last release of Python 2 and thus
181186
will be used for a rather long time. It also allows for use of the ``-3`` flag
@@ -184,6 +189,7 @@ known to cause issues.
184189

185190
Try to Support `Python 2.6`_ and Newer Only
186191
-------------------------------------------
192+
187193
While not possible for all projects, if you can support `Python 2.6`_ and newer
188194
**only**, your life will be much easier. Various future statements, stdlib
189195
additions, etc. exist only in Python 2.6 and later which greatly assist in
@@ -199,6 +205,7 @@ at least need to watch out for situations that these solutions fix.
199205

200206
``from __future__ import print_function``
201207
'''''''''''''''''''''''''''''''''''''''''
208+
202209
This is a personal choice. 2to3 handles the translation from the print
203210
statement to the print function rather well so this is an optional step. This
204211
future statement does help, though, with getting used to typing
@@ -207,6 +214,7 @@ future statement does help, though, with getting used to typing
207214

208215
``from __future__ import unicode_literals``
209216
'''''''''''''''''''''''''''''''''''''''''''
217+
210218
Another personal choice. You can always mark what you want to be a (unicode)
211219
string with a ``u`` prefix to get the same effect. But regardless of whether
212220
you use this future statement or not, you **must** make sure you know exactly
@@ -217,6 +225,7 @@ strings with a ``u`` prefix if you do not use this future statement.
217225

218226
Bytes literals
219227
''''''''''''''
228+
220229
This is a **very** important one. The ability to prefix Python 2 strings that
221230
are meant to contain bytes with a ``b`` prefix help to very clearly delineate
222231
what is and is not a Python 3 string. When you run 2to3 on code, all Python 2
@@ -238,12 +247,14 @@ also comes about when doing comparisons between bytes and strings.
238247

239248
Supporting `Python 2.5`_ and Newer Only
240249
---------------------------------------
250+
241251
If you are supporting `Python 2.5`_ and newer there are still some features of
242252
Python that you can utilize.
243253

244254

245255
``from __future__ import absolute_imports``
246256
'''''''''''''''''''''''''''''''''''''''''''
257+
247258
Implicit relative imports (e.g., importing ``spam.bacon`` from within
248259
``spam.eggs`` with the statement ``import bacon``) does not work in Python 3.
249260
This future statement moves away from that and allows the use of explicit
@@ -261,13 +272,15 @@ than Python 2.5, use the __future__ statement.
261272

262273
Handle Common "Gotchas"
263274
-----------------------
275+
264276
There are a few things that just consistently come up as sticking points for
265277
people which 2to3 cannot handle automatically or can easily be done in Python 2
266278
to help modernize your code.
267279

268280

269281
``from __future__ import division``
270282
'''''''''''''''''''''''''''''''''''
283+
271284
While the exact same outcome can be had by using the ``-Qnew`` argument to
272285
Python, using this future statement lifts the requirement that your users use
273286
the flag to get the expected behavior of division in Python 3
@@ -305,6 +318,7 @@ possibilities:
305318

306319
Subclass ``object``
307320
'''''''''''''''''''
321+
308322
New-style classes have been around since `Python 2.2`_. You need to make sure
309323
you are subclassing from ``object`` to avoid odd edge cases involving method
310324
resolution order, etc. This continues to be totally valid in Python 3 (although
@@ -313,6 +327,7 @@ unneeded as all classes implicitly inherit from ``object``).
313327

314328
Deal With the Bytes/String Dichotomy
315329
''''''''''''''''''''''''''''''''''''
330+
316331
One of the biggest issues people have when porting code to Python 3 is handling
317332
the bytes/string dichotomy. Because Python 2 allowed the ``str`` type to hold
318333
textual data, people have over the years been rather loose in their delineation
@@ -340,6 +355,7 @@ literals you can either use six's ``u()`` function or use a ``u`` prefix.
340355

341356
Decide what APIs Will Accept
342357
****************************
358+
343359
In Python 2 it was very easy to accidentally create an API that accepted both
344360
bytes and textual data. But in Python 3, thanks to the more strict handling of
345361
disparate types, this loose usage of bytes and text together tends to fail.
@@ -417,9 +433,10 @@ bytes object instead of raising ``IndexError``:
417433

418434
``__str__()``/``__unicode__()``
419435
'''''''''''''''''''''''''''''''
436+
420437
In Python 2, objects can specify both a string and unicode representation of
421438
themselves. In Python 3, though, there is only a string representation. This
422-
becomes an issue as people can inadvertantly do things in their ``__str__()``
439+
becomes an issue as people can inadvertently do things in their ``__str__()``
423440
methods which have unpredictable results (e.g., infinite recursion if you
424441
happen to use the ``unicode(self).encode('utf8')`` idiom as the body of your
425442
``__str__()`` method).
@@ -484,6 +501,7 @@ friends.
484501

485502
Updating doctests
486503
'''''''''''''''''
504+
487505
2to3_ will attempt to generate fixes for doctests that it comes across. It's
488506
not perfect, though. If you wrote a monolithic set of doctests (e.g., a single
489507
docstring containing all of your doctests), you should at least consider
@@ -494,6 +512,7 @@ to :mod:`unittest`.
494512

495513
Eliminate ``-3`` Warnings
496514
-------------------------
515+
497516
When you run your application's test suite, run it using the ``-3`` flag passed
498517
to Python. This will cause various warnings to be raised during execution about
499518
things that 2to3 cannot handle automatically (e.g., modules that have been
@@ -503,12 +522,14 @@ to Python 3.
503522

504523
Run 2to3
505524
--------
525+
506526
Once you have made your Python 2 code future-compatible with Python 3, it's
507527
time to use 2to3_ to actually port your code.
508528

509529

510530
Manually
511531
''''''''
532+
512533
To manually convert source code using 2to3_, you use the ``2to3`` script that
513534
is installed with Python 2.6 and later.::
514535

@@ -526,6 +547,7 @@ etc.
526547

527548
During Installation
528549
'''''''''''''''''''
550+
529551
When a user installs your project for Python 3, you can have either
530552
:mod:`distutils` or Distribute_ run 2to3_ on your behalf.
531553
For distutils, use the following idiom::
@@ -552,6 +574,7 @@ you at least build your project and use the built Python 3 source for testing.
552574

553575
Verify & Test
554576
-------------
577+
555578
At this point you should (hopefully) have your project converted in such a way
556579
that it works in Python 3. Verify it by running your unit tests and making sure
557580
nothing has gone awry. If you miss something then figure out how to fix it in
@@ -567,6 +590,7 @@ to verify the fix transforms properly.
567590

568591
Python 2/3 Compatible Source
569592
============================
593+
570594
While it may seem counter-intuitive, you can write Python code which is
571595
source-compatible between Python 2 & 3. It does lead to code that is not
572596
entirely idiomatic Python (e.g., having to extract the currently raised
@@ -589,6 +613,7 @@ the same source code.
589613

590614
Follow The Steps for Using 2to3_ (sans 2to3)
591615
--------------------------------------------
616+
592617
All of the steps outlined in how to
593618
:ref:`port Python 2 code with 2to3 <use_2to3>` apply
594619
to creating a Python 2/3 codebase. This includes trying only support Python 2.6
@@ -602,6 +627,7 @@ code so that you can fix them properly before they become an issue.
602627

603628
Use six_
604629
--------
630+
605631
The six_ project contains many things to help you write portable Python code.
606632
You should make sure to read its documentation from beginning to end and use
607633
any and all features it provides. That way you will minimize any mistakes you
@@ -610,6 +636,7 @@ might make in writing cross-version code.
610636

611637
Capturing the Currently Raised Exception
612638
----------------------------------------
639+
613640
One change between Python 2 and 3 that will require changing how you code (if
614641
you support `Python 2.5`_ and earlier) is
615642
accessing the currently raised exception. In Python 2.5 and earlier the syntax
@@ -658,9 +685,11 @@ likely don't need it.
658685
(e.g. the third element of the tuple returned by :func:`sys.exc_info`)
659686
in a variable.
660687

688+
661689
Other Resources
662690
===============
663-
The authors of the following blogs posts and wiki pages deserve special thanks
691+
692+
The authors of the following blog posts and wiki pages deserve special thanks
664693
for making public their tips for porting Python 2 code to Python 3 (and thus
665694
helping provide information for this document):
666695

0 commit comments

Comments
 (0)