Linux ip-148-66-134-25.ip.secureserver.net 3.10.0-1160.119.1.el7.tuxcare.els11.x86_64 #1 SMP Sun Nov 3 09:06:59 UTC 2024 x86_64
Apache
: 148.66.134.25 | : 3.133.117.113
66 Domain
8.0.30
amvm
www.github.com/MadExploits
Terminal
AUTO ROOT
Adminer
Backdoor Destroyer
Linux Exploit
Lock Shell
Lock File
Create User
CREATE RDP
PHP Mailer
BACKCONNECT
UNLOCK SHELL
HASH IDENTIFIER
CPANEL RESET
BLACK DEFEND!
README
+ Create Folder
+ Create File
/
usr /
share /
doc /
python-docs-2.7.5 /
html /
howto /
[ HOME SHELL ]
Name
Size
Permission
Action
argparse.html
72.39
KB
-rw-r--r--
cporting.html
39.22
KB
-rw-r--r--
curses.html
42.8
KB
-rw-r--r--
descriptor.html
50.15
KB
-rw-r--r--
doanddont.html
38.69
KB
-rw-r--r--
functional.html
109
KB
-rw-r--r--
index.html
7.51
KB
-rw-r--r--
logging-cookbook.html
103.51
KB
-rw-r--r--
logging.html
93.47
KB
-rw-r--r--
pyporting.html
60.8
KB
-rw-r--r--
regex.html
133.84
KB
-rw-r--r--
sockets.html
39.93
KB
-rw-r--r--
sorting.html
44.25
KB
-rw-r--r--
unicode.html
64.29
KB
-rw-r--r--
urllib2.html
58.48
KB
-rw-r--r--
webservers.html
55.66
KB
-rw-r--r--
Delete
Unzip
Zip
${this.title}
Close
Code Editor : pyporting.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Porting Python 2 Code to Python 3 — Python 2.7.5 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '2.7.5', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.5 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="Python 2.7.5 documentation" href="../index.html" /> <link rel="up" title="Python HOWTOs" href="index.html" /> <link rel="next" title="Porting Extension Modules to Python 3" href="cporting.html" /> <link rel="prev" title="Python HOWTOs" href="index.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="cporting.html" title="Porting Extension Modules to Python 3" accesskey="N">next</a> |</li> <li class="right" > <a href="index.html" title="Python HOWTOs" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" accesskey="U">Python HOWTOs</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="porting-python-2-code-to-python-3"> <span id="pyporting-howto"></span><h1>Porting Python 2 Code to Python 3<a class="headerlink" href="#porting-python-2-code-to-python-3" title="Permalink to this headline">¶</a></h1> <table class="docutils field-list" frame="void" rules="none"> <col class="field-name" /> <col class="field-body" /> <tbody valign="top"> <tr class="field-odd field"><th class="field-name">author:</th><td class="field-body">Brett Cannon</td> </tr> </tbody> </table> <div class="topic"> <p class="topic-title first">Abstract</p> <p>With Python 3 being the future of Python while Python 2 is still in active use, it is good to have your project available for both major releases of Python. This guide is meant to help you choose which strategy works best for your project to support both Python 2 & 3 along with how to execute that strategy.</p> <p>If you are looking to port an extension module instead of pure Python code, please see <a class="reference internal" href="cporting.html#cporting-howto"><em>Porting Extension Modules to Python 3</em></a>.</p> </div> <div class="section" id="choosing-a-strategy"> <h2>Choosing a Strategy<a class="headerlink" href="#choosing-a-strategy" title="Permalink to this headline">¶</a></h2> <p>When a project makes the decision that it’s time to support both Python 2 & 3, a decision needs to be made as to how to go about accomplishing that goal. The chosen strategy will depend on how large the project’s existing codebase is and how much divergence you want from your Python 2 codebase from your Python 3 one (e.g., starting a new version with Python 3).</p> <p>If your project is brand-new or does not have a large codebase, then you may want to consider writing/porting <a class="reference internal" href="#use-3to2"><em>all of your code for Python 3 and use 3to2</em></a> to port your code for Python 2.</p> <p>If you would prefer to maintain a codebase which is semantically <strong>and</strong> syntactically compatible with Python 2 & 3 simultaneously, you can write <a class="reference internal" href="#use-same-source"><em>Python 2/3 Compatible Source</em></a>. While this tends to lead to somewhat non-idiomatic code, it does mean you keep a rapid development process for you, the developer.</p> <p>Finally, you do have the option of <a class="reference internal" href="#use-2to3"><em>using 2to3</em></a> to translate Python 2 code into Python 3 code (with some manual help). This can take the form of branching your code and using 2to3 to start a Python 3 branch. You can also have users perform the translation at installation time automatically so that you only have to maintain a Python 2 codebase.</p> <p>Regardless of which approach you choose, porting is not as hard or time-consuming as you might initially think. You can also tackle the problem piece-meal as a good portion of porting is simply updating your code to follow current best practices in a Python 2/3 compatible way.</p> <div class="section" id="universal-bits-of-advice"> <h3>Universal Bits of Advice<a class="headerlink" href="#universal-bits-of-advice" title="Permalink to this headline">¶</a></h3> <p>Regardless of what strategy you pick, there are a few things you should consider.</p> <p>One is make sure you have a robust test suite. You need to make sure everything continues to work, just like when you support a new minor version of Python. This means making sure your test suite is thorough and is ported properly between Python 2 & 3. You will also most likely want to use something like <a class="reference external" href="http://codespeak.net/tox/">tox</a> to automate testing between both a Python 2 and Python 3 VM.</p> <p>Two, once your project has Python 3 support, make sure to add the proper classifier on the <a class="reference external" href="http://pypi.python.org/">Cheeseshop</a> (<a class="reference external" href="http://pypi.python.org/">PyPI</a>). To have your project listed as Python 3 compatible it must have the <a class="reference external" href="http://pypi.python.org/pypi?:action=browse&c=533">Python 3 classifier</a> (from <a class="reference external" href="http://techspot.zzzeek.org/2011/01/24/zzzeek-s-guide-to-python-3-porting/">http://techspot.zzzeek.org/2011/01/24/zzzeek-s-guide-to-python-3-porting/</a>):</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">setup</span><span class="p">(</span> <span class="n">name</span><span class="o">=</span><span class="s">'Your Library'</span><span class="p">,</span> <span class="n">version</span><span class="o">=</span><span class="s">'1.0'</span><span class="p">,</span> <span class="n">classifiers</span><span class="o">=</span><span class="p">[</span> <span class="c"># make sure to use :: Python *and* :: Python :: 3 so</span> <span class="c"># that pypi can list the package on the python 3 page</span> <span class="s">'Programming Language :: Python'</span><span class="p">,</span> <span class="s">'Programming Language :: Python :: 3'</span> <span class="p">],</span> <span class="n">packages</span><span class="o">=</span><span class="p">[</span><span class="s">'yourlibrary'</span><span class="p">],</span> <span class="c"># make sure to add custom_fixers to the MANIFEST.in</span> <span class="n">include_package_data</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="c"># ...</span> <span class="p">)</span> </pre></div> </div> <p>Doing so will cause your project to show up in the <a class="reference external" href="http://pypi.python.org/pypi?:action=browse&c=533&show=all">Python 3 packages list</a>. You will know you set the classifier properly as visiting your project page on the Cheeseshop will show a Python 3 logo in the upper-left corner of the page.</p> <p>Three, the <a class="reference external" href="http://packages.python.org/six">six</a> project provides a library which helps iron out differences between Python 2 & 3. If you find there is a sticky point that is a continual point of contention in your translation or maintenance of code, consider using a source-compatible solution relying on six. If you have to create your own Python 2/3 compatible solution, you can use <tt class="docutils literal"><span class="pre">sys.version_info[0]</span> <span class="pre">>=</span> <span class="pre">3</span></tt> as a guard.</p> <p>Four, read all the approaches. Just because some bit of advice applies to one approach more than another doesn’t mean that some advice doesn’t apply to other strategies.</p> <p>Five, drop support for older Python versions if possible. <a class="reference external" href="http://www.python.org/2.5.x">Python 2.5</a> introduced a lot of useful syntax and libraries which have become idiomatic in Python 3. <a class="reference external" href="http://www.python.org/2.6.x">Python 2.6</a> introduced future statements which makes compatibility much easier if you are going from Python 2 to 3. <a class="reference external" href="http://www.python.org/2.7.x">Python 2.7</a> continues the trend in the stdlib. So choose the newest version of Python which you believe can be your minimum support version and work from there.</p> </div> </div> <div class="section" id="python-3-and-3to2"> <span id="use-3to2"></span><h2>Python 3 and 3to2<a class="headerlink" href="#python-3-and-3to2" title="Permalink to this headline">¶</a></h2> <p>If you are starting a new project or your codebase is small enough, you may want to consider writing your code for Python 3 and backporting to Python 2 using <a class="reference external" href="https://bitbucket.org/amentajo/lib3to2/overview">3to2</a>. Thanks to Python 3 being more strict about things than Python 2 (e.g., bytes vs. strings), the source translation can be easier and more straightforward than from Python 2 to 3. Plus it gives you more direct experience developing in Python 3 which, since it is the future of Python, is a good thing long-term.</p> <p>A drawback of this approach is that 3to2 is a third-party project. This means that the Python core developers (and thus this guide) can make no promises about how well 3to2 works at any time. There is nothing to suggest, though, that 3to2 is not a high-quality project.</p> </div> <div class="section" id="python-2-and-2to3"> <span id="use-2to3"></span><h2>Python 2 and 2to3<a class="headerlink" href="#python-2-and-2to3" title="Permalink to this headline">¶</a></h2> <p>Included with Python since 2.6, the <a class="reference external" href="http://docs.python.org/py3k/library/2to3.html">2to3</a> tool (and <a class="reference internal" href="../library/2to3.html#module-lib2to3" title="lib2to3: the 2to3 library"><tt class="xref py py-mod docutils literal"><span class="pre">lib2to3</span></tt></a> module) helps with porting Python 2 to Python 3 by performing various source translations. This is a perfect solution for projects which wish to branch their Python 3 code from their Python 2 codebase and maintain them as independent codebases. You can even begin preparing to use this approach today by writing future-compatible Python code which works cleanly in Python 2 in conjunction with 2to3; all steps outlined below will work with Python 2 code up to the point when the actual use of 2to3 occurs.</p> <p>Use of 2to3 as an on-demand translation step at install time is also possible, preventing the need to maintain a separate Python 3 codebase, but this approach does come with some drawbacks. While users will only have to pay the translation cost once at installation, you as a developer will need to pay the cost regularly during development. If your codebase is sufficiently large enough then the translation step ends up acting like a compilation step, robbing you of the rapid development process you are used to with Python. Obviously the time required to translate a project will vary, so do an experimental translation just to see how long it takes to evaluate whether you prefer this approach compared to using <a class="reference internal" href="#use-same-source"><em>Python 2/3 Compatible Source</em></a> or simply keeping a separate Python 3 codebase.</p> <p>Below are the typical steps taken by a project which uses a 2to3-based approach to supporting Python 2 & 3.</p> <div class="section" id="support-python-2-7"> <h3>Support Python 2.7<a class="headerlink" href="#support-python-2-7" title="Permalink to this headline">¶</a></h3> <p>As a first step, make sure that your project is compatible with <a class="reference external" href="http://www.python.org/2.7.x">Python 2.7</a>. This is just good to do as Python 2.7 is the last release of Python 2 and thus will be used for a rather long time. It also allows for use of the <tt class="docutils literal"><span class="pre">-3</span></tt> flag to Python to help discover places in your code which 2to3 cannot handle but are known to cause issues.</p> </div> <div class="section" id="try-to-support-python-2-6-and-newer-only"> <h3>Try to Support <a class="reference external" href="http://www.python.org/2.6.x">Python 2.6</a> and Newer Only<a class="headerlink" href="#try-to-support-python-2-6-and-newer-only" title="Permalink to this headline">¶</a></h3> <p>While not possible for all projects, if you can support <a class="reference external" href="http://www.python.org/2.6.x">Python 2.6</a> and newer <strong>only</strong>, your life will be much easier. Various future statements, stdlib additions, etc. exist only in Python 2.6 and later which greatly assist in porting to Python 3. But if you project must keep support for <a class="reference external" href="http://www.python.org/2.5.x">Python 2.5</a> (or even <a class="reference external" href="http://www.python.org/2.4.x">Python 2.4</a>) then it is still possible to port to Python 3.</p> <p>Below are the benefits you gain if you only have to support Python 2.6 and newer. Some of these options are personal choice while others are <strong>strongly</strong> recommended (the ones that are more for personal choice are labeled as such). If you continue to support older versions of Python then you at least need to watch out for situations that these solutions fix.</p> <div class="section" id="from-future-import-print-function"> <h4><tt class="docutils literal"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">print_function</span></tt><a class="headerlink" href="#from-future-import-print-function" title="Permalink to this headline">¶</a></h4> <p>This is a personal choice. 2to3 handles the translation from the print statement to the print function rather well so this is an optional step. This future statement does help, though, with getting used to typing <tt class="docutils literal"><span class="pre">print('Hello,</span> <span class="pre">World')</span></tt> instead of <tt class="docutils literal"><span class="pre">print</span> <span class="pre">'Hello,</span> <span class="pre">World'</span></tt>.</p> </div> <div class="section" id="from-future-import-unicode-literals"> <h4><tt class="docutils literal"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">unicode_literals</span></tt><a class="headerlink" href="#from-future-import-unicode-literals" title="Permalink to this headline">¶</a></h4> <p>Another personal choice. You can always mark what you want to be a (unicode) string with a <tt class="docutils literal"><span class="pre">u</span></tt> prefix to get the same effect. But regardless of whether you use this future statement or not, you <strong>must</strong> make sure you know exactly which Python 2 strings you want to be bytes, and which are to be strings. This means you should, <strong>at minimum</strong> mark all strings that are meant to be text strings with a <tt class="docutils literal"><span class="pre">u</span></tt> prefix if you do not use this future statement.</p> </div> <div class="section" id="bytes-literals"> <h4>Bytes literals<a class="headerlink" href="#bytes-literals" title="Permalink to this headline">¶</a></h4> <p>This is a <strong>very</strong> important one. The ability to prefix Python 2 strings that are meant to contain bytes with a <tt class="docutils literal"><span class="pre">b</span></tt> prefix help to very clearly delineate what is and is not a Python 3 string. When you run 2to3 on code, all Python 2 strings become Python 3 strings <strong>unless</strong> they are prefixed with <tt class="docutils literal"><span class="pre">b</span></tt>.</p> <p>There are some differences between byte literals in Python 2 and those in Python 3 thanks to the bytes type just being an alias to <tt class="docutils literal"><span class="pre">str</span></tt> in Python 2. Probably the biggest “gotcha” is that indexing results in different values. In Python 2, the value of <tt class="docutils literal"><span class="pre">b'py'[1]</span></tt> is <tt class="docutils literal"><span class="pre">'y'</span></tt>, while in Python 3 it’s <tt class="docutils literal"><span class="pre">121</span></tt>. You can avoid this disparity by always slicing at the size of a single element: <tt class="docutils literal"><span class="pre">b'py'[1:2]</span></tt> is <tt class="docutils literal"><span class="pre">'y'</span></tt> in Python 2 and <tt class="docutils literal"><span class="pre">b'y'</span></tt> in Python 3 (i.e., close enough).</p> <p>You cannot concatenate bytes and strings in Python 3. But since Python 2 has bytes aliased to <tt class="docutils literal"><span class="pre">str</span></tt>, it will succeed: <tt class="docutils literal"><span class="pre">b'a'</span> <span class="pre">+</span> <span class="pre">u'b'</span></tt> works in Python 2, but <tt class="docutils literal"><span class="pre">b'a'</span> <span class="pre">+</span> <span class="pre">'b'</span></tt> in Python 3 is a <a class="reference internal" href="../library/exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a>. A similar issue also comes about when doing comparisons between bytes and strings.</p> </div> </div> <div class="section" id="supporting-python-2-5-and-newer-only"> <h3>Supporting <a class="reference external" href="http://www.python.org/2.5.x">Python 2.5</a> and Newer Only<a class="headerlink" href="#supporting-python-2-5-and-newer-only" title="Permalink to this headline">¶</a></h3> <p>If you are supporting <a class="reference external" href="http://www.python.org/2.5.x">Python 2.5</a> and newer there are still some features of Python that you can utilize.</p> <div class="section" id="from-future-import-absolute-import"> <h4><tt class="docutils literal"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">absolute_import</span></tt><a class="headerlink" href="#from-future-import-absolute-import" title="Permalink to this headline">¶</a></h4> <p>Implicit relative imports (e.g., importing <tt class="docutils literal"><span class="pre">spam.bacon</span></tt> from within <tt class="docutils literal"><span class="pre">spam.eggs</span></tt> with the statement <tt class="docutils literal"><span class="pre">import</span> <span class="pre">bacon</span></tt>) does not work in Python 3. This future statement moves away from that and allows the use of explicit relative imports (e.g., <tt class="docutils literal"><span class="pre">from</span> <span class="pre">.</span> <span class="pre">import</span> <span class="pre">bacon</span></tt>).</p> <p>In <a class="reference external" href="http://www.python.org/2.5.x">Python 2.5</a> you must use the __future__ statement to get to use explicit relative imports and prevent implicit ones. In <a class="reference external" href="http://www.python.org/2.6.x">Python 2.6</a> explicit relative imports are available without the statement, but you still want the __future__ statement to prevent implicit relative imports. In <a class="reference external" href="http://www.python.org/2.7.x">Python 2.7</a> the __future__ statement is not needed. In other words, unless you are only supporting Python 2.7 or a version earlier than Python 2.5, use the __future__ statement.</p> </div> </div> <div class="section" id="handle-common-gotchas"> <h3>Handle Common “Gotchas”<a class="headerlink" href="#handle-common-gotchas" title="Permalink to this headline">¶</a></h3> <p>There are a few things that just consistently come up as sticking points for people which 2to3 cannot handle automatically or can easily be done in Python 2 to help modernize your code.</p> <div class="section" id="from-future-import-division"> <h4><tt class="docutils literal"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">division</span></tt><a class="headerlink" href="#from-future-import-division" title="Permalink to this headline">¶</a></h4> <p>While the exact same outcome can be had by using the <tt class="docutils literal"><span class="pre">-Qnew</span></tt> argument to Python, using this future statement lifts the requirement that your users use the flag to get the expected behavior of division in Python 3 (e.g., <tt class="docutils literal"><span class="pre">1/2</span> <span class="pre">==</span> <span class="pre">0.5;</span> <span class="pre">1//2</span> <span class="pre">==</span> <span class="pre">0</span></tt>).</p> </div> <div class="section" id="specify-when-opening-a-file-as-binary"> <h4>Specify when opening a file as binary<a class="headerlink" href="#specify-when-opening-a-file-as-binary" title="Permalink to this headline">¶</a></h4> <p>Unless you have been working on Windows, there is a chance you have not always bothered to add the <tt class="docutils literal"><span class="pre">b</span></tt> mode when opening a binary file (e.g., <tt class="docutils literal"><span class="pre">rb</span></tt> for binary reading). Under Python 3, binary files and text files are clearly distinct and mutually incompatible; see the <a class="reference internal" href="../library/io.html#module-io" title="io: Core tools for working with streams."><tt class="xref py py-mod docutils literal"><span class="pre">io</span></tt></a> module for details. Therefore, you <strong>must</strong> make a decision of whether a file will be used for binary access (allowing to read and/or write bytes data) or text access (allowing to read and/or write unicode data).</p> </div> <div class="section" id="text-files"> <h4>Text files<a class="headerlink" href="#text-files" title="Permalink to this headline">¶</a></h4> <p>Text files created using <tt class="docutils literal"><span class="pre">open()</span></tt> under Python 2 return byte strings, while under Python 3 they return unicode strings. Depending on your porting strategy, this can be an issue.</p> <p>If you want text files to return unicode strings in Python 2, you have two possibilities:</p> <ul class="simple"> <li>Under Python 2.6 and higher, use <a class="reference internal" href="../library/io.html#io.open" title="io.open"><tt class="xref py py-func docutils literal"><span class="pre">io.open()</span></tt></a>. Since <a class="reference internal" href="../library/io.html#io.open" title="io.open"><tt class="xref py py-func docutils literal"><span class="pre">io.open()</span></tt></a> is essentially the same function in both Python 2 and Python 3, it will help iron out any issues that might arise.</li> <li>If pre-2.6 compatibility is needed, then you should use <a class="reference internal" href="../library/codecs.html#codecs.open" title="codecs.open"><tt class="xref py py-func docutils literal"><span class="pre">codecs.open()</span></tt></a> instead. This will make sure that you get back unicode strings in Python 2.</li> </ul> </div> <div class="section" id="subclass-object"> <h4>Subclass <tt class="docutils literal"><span class="pre">object</span></tt><a class="headerlink" href="#subclass-object" title="Permalink to this headline">¶</a></h4> <p>New-style classes have been around since <a class="reference external" href="http://www.python.org/2.2.x">Python 2.2</a>. You need to make sure you are subclassing from <tt class="docutils literal"><span class="pre">object</span></tt> to avoid odd edge cases involving method resolution order, etc. This continues to be totally valid in Python 3 (although unneeded as all classes implicitly inherit from <tt class="docutils literal"><span class="pre">object</span></tt>).</p> </div> <div class="section" id="deal-with-the-bytes-string-dichotomy"> <h4>Deal With the Bytes/String Dichotomy<a class="headerlink" href="#deal-with-the-bytes-string-dichotomy" title="Permalink to this headline">¶</a></h4> <p>One of the biggest issues people have when porting code to Python 3 is handling the bytes/string dichotomy. Because Python 2 allowed the <tt class="docutils literal"><span class="pre">str</span></tt> type to hold textual data, people have over the years been rather loose in their delineation of what <tt class="docutils literal"><span class="pre">str</span></tt> instances held text compared to bytes. In Python 3 you cannot be so care-free anymore and need to properly handle the difference. The key handling this issue is to make sure that <strong>every</strong> string literal in your Python 2 code is either syntactically of functionally marked as either bytes or text data. After this is done you then need to make sure your APIs are designed to either handle a specific type or made to be properly polymorphic.</p> <div class="section" id="mark-up-python-2-string-literals"> <h5>Mark Up Python 2 String Literals<a class="headerlink" href="#mark-up-python-2-string-literals" title="Permalink to this headline">¶</a></h5> <p>First thing you must do is designate every single string literal in Python 2 as either textual or bytes data. If you are only supporting Python 2.6 or newer, this can be accomplished by marking bytes literals with a <tt class="docutils literal"><span class="pre">b</span></tt> prefix and then designating textual data with a <tt class="docutils literal"><span class="pre">u</span></tt> prefix or using the <tt class="docutils literal"><span class="pre">unicode_literals</span></tt> future statement.</p> <p>If your project supports versions of Python predating 2.6, then you should use the <a class="reference external" href="http://packages.python.org/six">six</a> project and its <tt class="docutils literal"><span class="pre">b()</span></tt> function to denote bytes literals. For text literals you can either use six’s <tt class="docutils literal"><span class="pre">u()</span></tt> function or use a <tt class="docutils literal"><span class="pre">u</span></tt> prefix.</p> </div> <div class="section" id="decide-what-apis-will-accept"> <h5>Decide what APIs Will Accept<a class="headerlink" href="#decide-what-apis-will-accept" title="Permalink to this headline">¶</a></h5> <p>In Python 2 it was very easy to accidentally create an API that accepted both bytes and textual data. But in Python 3, thanks to the more strict handling of disparate types, this loose usage of bytes and text together tends to fail.</p> <p>Take the dict <tt class="docutils literal"><span class="pre">{b'a':</span> <span class="pre">'bytes',</span> <span class="pre">u'a':</span> <span class="pre">'text'}</span></tt> in Python 2.6. It creates the dict <tt class="docutils literal"><span class="pre">{u'a':</span> <span class="pre">'text'}</span></tt> since <tt class="docutils literal"><span class="pre">b'a'</span> <span class="pre">==</span> <span class="pre">u'a'</span></tt>. But in Python 3 the equivalent dict creates <tt class="docutils literal"><span class="pre">{b'a':</span> <span class="pre">'bytes',</span> <span class="pre">'a':</span> <span class="pre">'text'}</span></tt>, i.e., no lost data. Similar issues can crop up when transitioning Python 2 code to Python 3.</p> <p>This means you need to choose what an API is going to accept and create and consistently stick to that API in both Python 2 and 3.</p> </div> <div class="section" id="bytes-unicode-comparison"> <h5>Bytes / Unicode Comparison<a class="headerlink" href="#bytes-unicode-comparison" title="Permalink to this headline">¶</a></h5> <p>In Python 3, mixing bytes and unicode is forbidden in most situations; it will raise a <tt class="xref py py-class docutils literal"><span class="pre">TypeError</span></tt> where Python 2 would have attempted an implicit coercion between types. However, there is one case where it doesn’t and it can be very misleading:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">b</span><span class="s">""</span> <span class="o">==</span> <span class="s">""</span> <span class="go">False</span> </pre></div> </div> <p>This is because an equality comparison is required by the language to always succeed (and return <tt class="docutils literal"><span class="pre">False</span></tt> for incompatible types). However, this also means that code incorrectly ported to Python 3 can display buggy behaviour if such comparisons are silently executed. To detect such situations, Python 3 has a <tt class="docutils literal"><span class="pre">-b</span></tt> flag that will display a warning:</p> <div class="highlight-python"><pre>$ python3 -b >>> b"" == "" __main__:1: BytesWarning: Comparison between bytes and string False</pre> </div> <p>To turn the warning into an exception, use the <tt class="docutils literal"><span class="pre">-bb</span></tt> flag instead:</p> <div class="highlight-python"><pre>$ python3 -bb >>> b"" == "" Traceback (most recent call last): File "<stdin>", line 1, in <module> BytesWarning: Comparison between bytes and string</pre> </div> </div> </div> <div class="section" id="indexing-bytes-objects"> <h4>Indexing bytes objects<a class="headerlink" href="#indexing-bytes-objects" title="Permalink to this headline">¶</a></h4> <p>Another potentially surprising change is the indexing behaviour of bytes objects in Python 3:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">b</span><span class="s">"xyz"</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="go">120</span> </pre></div> </div> <p>Indeed, Python 3 bytes objects (as well as <a class="reference internal" href="../library/functions.html#bytearray" title="bytearray"><tt class="xref py py-class docutils literal"><span class="pre">bytearray</span></tt></a> objects) are sequences of integers. But code converted from Python 2 will often assume that indexing a bytestring produces another bytestring, not an integer. To reconcile both behaviours, use slicing:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">b</span><span class="s">"xyz"</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">1</span><span class="p">]</span> <span class="go">b'x'</span> <span class="gp">>>> </span><span class="n">n</span> <span class="o">=</span> <span class="mi">1</span> <span class="gp">>>> </span><span class="n">b</span><span class="s">"xyz"</span><span class="p">[</span><span class="n">n</span><span class="p">:</span><span class="n">n</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="go">b'y'</span> </pre></div> </div> <p>The only remaining gotcha is that an out-of-bounds slice returns an empty bytes object instead of raising <tt class="docutils literal"><span class="pre">IndexError</span></tt>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">b</span><span class="s">"xyz"</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="gt">Traceback (most recent call last):</span> File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n"><module></span> <span class="gr">IndexError</span>: <span class="n">index out of range</span> <span class="gp">>>> </span><span class="n">b</span><span class="s">"xyz"</span><span class="p">[</span><span class="mi">3</span><span class="p">:</span><span class="mi">4</span><span class="p">]</span> <span class="go">b''</span> </pre></div> </div> </div> <div class="section" id="str-unicode"> <h4><tt class="docutils literal"><span class="pre">__str__()</span></tt>/<tt class="docutils literal"><span class="pre">__unicode__()</span></tt><a class="headerlink" href="#str-unicode" title="Permalink to this headline">¶</a></h4> <p>In Python 2, objects can specify both a string and unicode representation of themselves. In Python 3, though, there is only a string representation. This becomes an issue as people can inadvertently do things in their <tt class="docutils literal"><span class="pre">__str__()</span></tt> methods which have unpredictable results (e.g., infinite recursion if you happen to use the <tt class="docutils literal"><span class="pre">unicode(self).encode('utf8')</span></tt> idiom as the body of your <tt class="docutils literal"><span class="pre">__str__()</span></tt> method).</p> <p>There are two ways to solve this issue. One is to use a custom 2to3 fixer. The blog post at <a class="reference external" href="http://lucumr.pocoo.org/2011/1/22/forwards-compatible-python/">http://lucumr.pocoo.org/2011/1/22/forwards-compatible-python/</a> specifies how to do this. That will allow 2to3 to change all instances of <tt class="docutils literal"><span class="pre">def</span> <span class="pre">__unicode(self):</span> <span class="pre">...</span></tt> to <tt class="docutils literal"><span class="pre">def</span> <span class="pre">__str__(self):</span> <span class="pre">...</span></tt>. This does require that you define your <tt class="docutils literal"><span class="pre">__str__()</span></tt> method in Python 2 before your <tt class="docutils literal"><span class="pre">__unicode__()</span></tt> method.</p> <p>The other option is to use a mixin class. This allows you to only define a <tt class="docutils literal"><span class="pre">__unicode__()</span></tt> method for your class and let the mixin derive <tt class="docutils literal"><span class="pre">__str__()</span></tt> for you (code from <a class="reference external" href="http://lucumr.pocoo.org/2011/1/22/forwards-compatible-python/">http://lucumr.pocoo.org/2011/1/22/forwards-compatible-python/</a>):</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">sys</span> <span class="k">class</span> <span class="nc">UnicodeMixin</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <span class="sd">"""Mixin class to handle defining the proper __str__/__unicode__</span> <span class="sd"> methods in Python 2 or 3."""</span> <span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">version_info</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">>=</span> <span class="mi">3</span><span class="p">:</span> <span class="c"># Python 3</span> <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__unicode__</span><span class="p">()</span> <span class="k">else</span><span class="p">:</span> <span class="c"># Python 2</span> <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__unicode__</span><span class="p">()</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">'utf8'</span><span class="p">)</span> <span class="k">class</span> <span class="nc">Spam</span><span class="p">(</span><span class="n">UnicodeMixin</span><span class="p">):</span> <span class="k">def</span> <span class="nf">__unicode__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">return</span> <span class="s">u'spam-spam-bacon-spam'</span> <span class="c"># 2to3 will remove the 'u' prefix</span> </pre></div> </div> </div> <div class="section" id="don-t-index-on-exceptions"> <h4>Don’t Index on Exceptions<a class="headerlink" href="#don-t-index-on-exceptions" title="Permalink to this headline">¶</a></h4> <p>In Python 2, the following worked:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">exc</span> <span class="o">=</span> <span class="ne">Exception</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">exc</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="go">2</span> <span class="gp">>>> </span><span class="n">exc</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="c"># Python 2 only!</span> <span class="go">2</span> </pre></div> </div> <p>But in Python 3, indexing directly on an exception is an error. You need to make sure to only index on the <tt class="xref py py-attr docutils literal"><span class="pre">BaseException.args</span></tt> attribute which is a sequence containing all arguments passed to the <a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><tt class="xref py py-meth docutils literal"><span class="pre">__init__()</span></tt></a> method.</p> <p>Even better is to use the documented attributes the exception provides.</p> </div> <div class="section" id="don-t-use-getslice-friends"> <h4>Don’t use <tt class="docutils literal"><span class="pre">__getslice__</span></tt> & Friends<a class="headerlink" href="#don-t-use-getslice-friends" title="Permalink to this headline">¶</a></h4> <p>Been deprecated for a while, but Python 3 finally drops support for <tt class="docutils literal"><span class="pre">__getslice__()</span></tt>, etc. Move completely over to <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><tt class="xref py py-meth docutils literal"><span class="pre">__getitem__()</span></tt></a> and friends.</p> </div> <div class="section" id="updating-doctests"> <h4>Updating doctests<a class="headerlink" href="#updating-doctests" title="Permalink to this headline">¶</a></h4> <p><a class="reference external" href="http://docs.python.org/py3k/library/2to3.html">2to3</a> will attempt to generate fixes for doctests that it comes across. It’s not perfect, though. If you wrote a monolithic set of doctests (e.g., a single docstring containing all of your doctests), you should at least consider breaking the doctests up into smaller pieces to make it more manageable to fix. Otherwise it might very well be worth your time and effort to port your tests to <a class="reference internal" href="../library/unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><tt class="xref py py-mod docutils literal"><span class="pre">unittest</span></tt></a>.</p> </div> </div> <div class="section" id="eliminate-3-warnings"> <h3>Eliminate <tt class="docutils literal"><span class="pre">-3</span></tt> Warnings<a class="headerlink" href="#eliminate-3-warnings" title="Permalink to this headline">¶</a></h3> <p>When you run your application’s test suite, run it using the <tt class="docutils literal"><span class="pre">-3</span></tt> flag passed to Python. This will cause various warnings to be raised during execution about things that 2to3 cannot handle automatically (e.g., modules that have been removed). Try to eliminate those warnings to make your code even more portable to Python 3.</p> </div> <div class="section" id="run-2to3"> <h3>Run 2to3<a class="headerlink" href="#run-2to3" title="Permalink to this headline">¶</a></h3> <p>Once you have made your Python 2 code future-compatible with Python 3, it’s time to use <a class="reference external" href="http://docs.python.org/py3k/library/2to3.html">2to3</a> to actually port your code.</p> <div class="section" id="manually"> <h4>Manually<a class="headerlink" href="#manually" title="Permalink to this headline">¶</a></h4> <p>To manually convert source code using <a class="reference external" href="http://docs.python.org/py3k/library/2to3.html">2to3</a>, you use the <tt class="docutils literal"><span class="pre">2to3</span></tt> script that is installed with Python 2.6 and later.:</p> <div class="highlight-python"><pre>2to3 <directory or file to convert></pre> </div> <p>This will cause 2to3 to write out a diff with all of the fixers applied for the converted source code. If you would like 2to3 to go ahead and apply the changes you can pass it the <tt class="docutils literal"><span class="pre">-w</span></tt> flag:</p> <div class="highlight-python"><pre>2to3 -w <stuff to convert></pre> </div> <p>There are other flags available to control exactly which fixers are applied, etc.</p> </div> <div class="section" id="during-installation"> <h4>During Installation<a class="headerlink" href="#during-installation" title="Permalink to this headline">¶</a></h4> <p>When a user installs your project for Python 3, you can have either <a class="reference internal" href="../library/distutils.html#module-distutils" title="distutils: Support for building and installing Python modules into an existing Python installation."><tt class="xref py py-mod docutils literal"><span class="pre">distutils</span></tt></a> or <a class="reference external" href="http://packages.python.org/distribute/">Distribute</a> run <a class="reference external" href="http://docs.python.org/py3k/library/2to3.html">2to3</a> on your behalf. For distutils, use the following idiom:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">try</span><span class="p">:</span> <span class="c"># Python 3</span> <span class="kn">from</span> <span class="nn">distutils.command.build_py</span> <span class="kn">import</span> <span class="n">build_py_2to3</span> <span class="k">as</span> <span class="n">build_py</span> <span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span> <span class="c"># Python 2</span> <span class="kn">from</span> <span class="nn">distutils.command.build_py</span> <span class="kn">import</span> <span class="n">build_py</span> <span class="n">setup</span><span class="p">(</span><span class="n">cmdclass</span> <span class="o">=</span> <span class="p">{</span><span class="s">'build_py'</span><span class="p">:</span> <span class="n">build_py</span><span class="p">},</span> <span class="c"># ...</span> <span class="p">)</span> </pre></div> </div> <p>For Distribute:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">setup</span><span class="p">(</span><span class="n">use_2to3</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="c"># ...</span> <span class="p">)</span> </pre></div> </div> <p>This will allow you to not have to distribute a separate Python 3 version of your project. It does require, though, that when you perform development that you at least build your project and use the built Python 3 source for testing.</p> </div> </div> <div class="section" id="verify-test"> <h3>Verify & Test<a class="headerlink" href="#verify-test" title="Permalink to this headline">¶</a></h3> <p>At this point you should (hopefully) have your project converted in such a way that it works in Python 3. Verify it by running your unit tests and making sure nothing has gone awry. If you miss something then figure out how to fix it in Python 3, backport to your Python 2 code, and run your code through 2to3 again to verify the fix transforms properly.</p> </div> </div> <div class="section" id="python-2-3-compatible-source"> <span id="use-same-source"></span><h2>Python 2/3 Compatible Source<a class="headerlink" href="#python-2-3-compatible-source" title="Permalink to this headline">¶</a></h2> <p>While it may seem counter-intuitive, you can write Python code which is source-compatible between Python 2 & 3. It does lead to code that is not entirely idiomatic Python (e.g., having to extract the currently raised exception from <tt class="docutils literal"><span class="pre">sys.exc_info()[1]</span></tt>), but it can be run under Python 2 <strong>and</strong> Python 3 without using <a class="reference external" href="http://docs.python.org/py3k/library/2to3.html">2to3</a> as a translation step (although the tool should be used to help find potential portability problems). This allows you to continue to have a rapid development process regardless of whether you are developing under Python 2 or Python 3. Whether this approach or using <a class="reference internal" href="#use-2to3"><em>Python 2 and 2to3</em></a> works best for you will be a per-project decision.</p> <p>To get a complete idea of what issues you will need to deal with, see the <a class="reference external" href="http://docs.python.org/release/3.0/whatsnew/3.0.html">What’s New in Python 3.0</a>. Others have reorganized the data in other formats such as <a class="reference external" href="http://docs.pythonsprints.com/python3_porting/py-porting.html">http://docs.pythonsprints.com/python3_porting/py-porting.html</a> .</p> <p>The following are some steps to take to try to support both Python 2 & 3 from the same source code.</p> <div class="section" id="follow-the-steps-for-using-2to3"> <h3>Follow The Steps for Using <a class="reference external" href="http://docs.python.org/py3k/library/2to3.html">2to3</a><a class="headerlink" href="#follow-the-steps-for-using-2to3" title="Permalink to this headline">¶</a></h3> <p>All of the steps outlined in how to <a class="reference internal" href="#use-2to3"><em>port Python 2 code with 2to3</em></a> apply to creating a Python 2/3 codebase. This includes trying only support Python 2.6 or newer (the <a class="reference internal" href="../library/__future__.html#module-__future__" title="__future__: Future statement definitions"><tt class="xref py py-mod docutils literal"><span class="pre">__future__</span></tt></a> statements work in Python 3 without issue), eliminating warnings that are triggered by <tt class="docutils literal"><span class="pre">-3</span></tt>, etc.</p> <p>You should even consider running <a class="reference external" href="http://docs.python.org/py3k/library/2to3.html">2to3</a> over your code (without committing the changes). This will let you know where potential pain points are within your code so that you can fix them properly before they become an issue.</p> </div> <div class="section" id="use-six"> <h3>Use <a class="reference external" href="http://packages.python.org/six">six</a><a class="headerlink" href="#use-six" title="Permalink to this headline">¶</a></h3> <p>The <a class="reference external" href="http://packages.python.org/six">six</a> project contains many things to help you write portable Python code. You should make sure to read its documentation from beginning to end and use any and all features it provides. That way you will minimize any mistakes you might make in writing cross-version code.</p> </div> <div class="section" id="capturing-the-currently-raised-exception"> <h3>Capturing the Currently Raised Exception<a class="headerlink" href="#capturing-the-currently-raised-exception" title="Permalink to this headline">¶</a></h3> <p>One change between Python 2 and 3 that will require changing how you code (if you support <a class="reference external" href="http://www.python.org/2.5.x">Python 2.5</a> and earlier) is accessing the currently raised exception. In Python 2.5 and earlier the syntax to access the current exception is:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">try</span><span class="p">:</span> <span class="k">raise</span> <span class="ne">Exception</span><span class="p">()</span> <span class="k">except</span> <span class="ne">Exception</span><span class="p">,</span> <span class="n">exc</span><span class="p">:</span> <span class="c"># Current exception is 'exc'</span> <span class="k">pass</span> </pre></div> </div> <p>This syntax changed in Python 3 (and backported to <a class="reference external" href="http://www.python.org/2.6.x">Python 2.6</a> and later) to:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">try</span><span class="p">:</span> <span class="k">raise</span> <span class="ne">Exception</span><span class="p">()</span> <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span> <span class="c"># Current exception is 'exc'</span> <span class="c"># In Python 3, 'exc' is restricted to the block; Python 2.6 will "leak"</span> <span class="k">pass</span> </pre></div> </div> <p>Because of this syntax change you must change to capturing the current exception to:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">try</span><span class="p">:</span> <span class="k">raise</span> <span class="ne">Exception</span><span class="p">()</span> <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span> <span class="kn">import</span> <span class="nn">sys</span> <span class="n">exc</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">exc_info</span><span class="p">()[</span><span class="mi">1</span><span class="p">]</span> <span class="c"># Current exception is 'exc'</span> <span class="k">pass</span> </pre></div> </div> <p>You can get more information about the raised exception from <a class="reference internal" href="../library/sys.html#sys.exc_info" title="sys.exc_info"><tt class="xref py py-func docutils literal"><span class="pre">sys.exc_info()</span></tt></a> than simply the current exception instance, but you most likely don’t need it.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p>In Python 3, the traceback is attached to the exception instance through the <tt class="docutils literal"><span class="pre">__traceback__</span></tt> attribute. If the instance is saved in a local variable that persists outside of the <tt class="docutils literal"><span class="pre">except</span></tt> block, the traceback will create a reference cycle with the current frame and its dictionary of local variables. This will delay reclaiming dead resources until the next cyclic <a class="reference internal" href="../glossary.html#term-garbage-collection"><em class="xref std std-term">garbage collection</em></a> pass.</p> <p class="last">In Python 2, this problem only occurs if you save the traceback itself (e.g. the third element of the tuple returned by <a class="reference internal" href="../library/sys.html#sys.exc_info" title="sys.exc_info"><tt class="xref py py-func docutils literal"><span class="pre">sys.exc_info()</span></tt></a>) in a variable.</p> </div> </div> </div> <div class="section" id="other-resources"> <h2>Other Resources<a class="headerlink" href="#other-resources" title="Permalink to this headline">¶</a></h2> <p>The authors of the following blog posts, wiki pages, and books deserve special thanks for making public their tips for porting Python 2 code to Python 3 (and thus helping provide information for this document):</p> <ul class="simple"> <li><a class="reference external" href="http://python3porting.com/">http://python3porting.com/</a></li> <li><a class="reference external" href="http://docs.pythonsprints.com/python3_porting/py-porting.html">http://docs.pythonsprints.com/python3_porting/py-porting.html</a></li> <li><a class="reference external" href="http://techspot.zzzeek.org/2011/01/24/zzzeek-s-guide-to-python-3-porting/">http://techspot.zzzeek.org/2011/01/24/zzzeek-s-guide-to-python-3-porting/</a></li> <li><a class="reference external" href="http://dabeaz.blogspot.com/2011/01/porting-py65-and-my-superboard-to.html">http://dabeaz.blogspot.com/2011/01/porting-py65-and-my-superboard-to.html</a></li> <li><a class="reference external" href="http://lucumr.pocoo.org/2011/1/22/forwards-compatible-python/">http://lucumr.pocoo.org/2011/1/22/forwards-compatible-python/</a></li> <li><a class="reference external" href="http://lucumr.pocoo.org/2010/2/11/porting-to-python-3-a-guide/">http://lucumr.pocoo.org/2010/2/11/porting-to-python-3-a-guide/</a></li> <li><a class="reference external" href="http://wiki.python.org/moin/PortingPythonToPy3k">http://wiki.python.org/moin/PortingPythonToPy3k</a></li> </ul> <p>If you feel there is something missing from this document that should be added, please email the <a class="reference external" href="http://mail.python.org/mailman/listinfo/python-porting">python-porting</a> mailing list.</p> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">Porting Python 2 Code to Python 3</a><ul> <li><a class="reference internal" href="#choosing-a-strategy">Choosing a Strategy</a><ul> <li><a class="reference internal" href="#universal-bits-of-advice">Universal Bits of Advice</a></li> </ul> </li> <li><a class="reference internal" href="#python-3-and-3to2">Python 3 and 3to2</a></li> <li><a class="reference internal" href="#python-2-and-2to3">Python 2 and 2to3</a><ul> <li><a class="reference internal" href="#support-python-2-7">Support Python 2.7</a></li> <li><a class="reference internal" href="#try-to-support-python-2-6-and-newer-only">Try to Support Python 2.6 and Newer Only</a><ul> <li><a class="reference internal" href="#from-future-import-print-function"><tt class="docutils literal"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">print_function</span></tt></a></li> <li><a class="reference internal" href="#from-future-import-unicode-literals"><tt class="docutils literal"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">unicode_literals</span></tt></a></li> <li><a class="reference internal" href="#bytes-literals">Bytes literals</a></li> </ul> </li> <li><a class="reference internal" href="#supporting-python-2-5-and-newer-only">Supporting Python 2.5 and Newer Only</a><ul> <li><a class="reference internal" href="#from-future-import-absolute-import"><tt class="docutils literal"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">absolute_import</span></tt></a></li> </ul> </li> <li><a class="reference internal" href="#handle-common-gotchas">Handle Common “Gotchas”</a><ul> <li><a class="reference internal" href="#from-future-import-division"><tt class="docutils literal"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">division</span></tt></a></li> <li><a class="reference internal" href="#specify-when-opening-a-file-as-binary">Specify when opening a file as binary</a></li> <li><a class="reference internal" href="#text-files">Text files</a></li> <li><a class="reference internal" href="#subclass-object">Subclass <tt class="docutils literal"><span class="pre">object</span></tt></a></li> <li><a class="reference internal" href="#deal-with-the-bytes-string-dichotomy">Deal With the Bytes/String Dichotomy</a><ul> <li><a class="reference internal" href="#mark-up-python-2-string-literals">Mark Up Python 2 String Literals</a></li> <li><a class="reference internal" href="#decide-what-apis-will-accept">Decide what APIs Will Accept</a></li> <li><a class="reference internal" href="#bytes-unicode-comparison">Bytes / Unicode Comparison</a></li> </ul> </li> <li><a class="reference internal" href="#indexing-bytes-objects">Indexing bytes objects</a></li> <li><a class="reference internal" href="#str-unicode"><tt class="docutils literal"><span class="pre">__str__()</span></tt>/<tt class="docutils literal"><span class="pre">__unicode__()</span></tt></a></li> <li><a class="reference internal" href="#don-t-index-on-exceptions">Don’t Index on Exceptions</a></li> <li><a class="reference internal" href="#don-t-use-getslice-friends">Don’t use <tt class="docutils literal"><span class="pre">__getslice__</span></tt> & Friends</a></li> <li><a class="reference internal" href="#updating-doctests">Updating doctests</a></li> </ul> </li> <li><a class="reference internal" href="#eliminate-3-warnings">Eliminate <tt class="docutils literal"><span class="pre">-3</span></tt> Warnings</a></li> <li><a class="reference internal" href="#run-2to3">Run 2to3</a><ul> <li><a class="reference internal" href="#manually">Manually</a></li> <li><a class="reference internal" href="#during-installation">During Installation</a></li> </ul> </li> <li><a class="reference internal" href="#verify-test">Verify & Test</a></li> </ul> </li> <li><a class="reference internal" href="#python-2-3-compatible-source">Python 2/3 Compatible Source</a><ul> <li><a class="reference internal" href="#follow-the-steps-for-using-2to3">Follow The Steps for Using 2to3</a></li> <li><a class="reference internal" href="#use-six">Use six</a></li> <li><a class="reference internal" href="#capturing-the-currently-raised-exception">Capturing the Currently Raised Exception</a></li> </ul> </li> <li><a class="reference internal" href="#other-resources">Other Resources</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="index.html" title="previous chapter">Python HOWTOs</a></p> <h4>Next topic</h4> <p class="topless"><a href="cporting.html" title="next chapter">Porting Extension Modules to Python 3</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li><a href="../_sources/howto/pyporting.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="cporting.html" title="Porting Extension Modules to Python 3" >next</a> |</li> <li class="right" > <a href="index.html" title="Python HOWTOs" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >Python HOWTOs</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2019, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="http://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Jul 03, 2019. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>
Close