Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
c9514cf2
Commit
c9514cf2
authored
Feb 03, 2011
by
Brett Cannon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use 3-space indents.
parent
3c93e979
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
69 additions
and
69 deletions
+69
-69
Doc/howto/pyporting.rst
Doc/howto/pyporting.rst
+69
-69
No files found.
Doc/howto/pyporting.rst
View file @
c9514cf2
...
...
@@ -8,14 +8,14 @@ Porting Python 2 Code to Python 3
.. topic:: Abstract
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.
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.
If you are looking to port an extension module instead of pure Python code,
please see http://docs.python.org/py3k/howto/cporting.html .
If you are looking to port an extension module instead of pure Python code,
please see http://docs.python.org/py3k/howto/cporting.html .
Choosing a Strategy
...
...
@@ -70,20 +70,20 @@ compatible it must have the
(from
http://techspot.zzzeek.org/2011/01/24/zzzeek-s-guide-to-python-3-porting/)::
setup(
name='Your Library',
version='1.0',
classifiers=[
# make sure to use :: Python *and* :: Python :: 3 so
# that pypi can list the package on the python 3 page
'Programming Language :: Python',
'Programming Language :: Python :: 3'
],
packages=['yourlibrary'],
# make sure to add custom_fixers to the MANIFEST.in
include_package_data=True,
# ...
)
setup(
name='Your Library',
version='1.0',
classifiers=[
# make sure to use :: Python *and* :: Python :: 3 so
# that pypi can list the package on the python 3 page
'Programming Language :: Python',
'Programming Language :: Python :: 3'
],
packages=['yourlibrary'],
# make sure to add custom_fixers to the MANIFEST.in
include_package_data=True,
# ...
)
Doing so will cause your project to show up in the
...
...
@@ -340,25 +340,25 @@ The other option is to use a mixin class. This allows you to only define a
``__str__()`` for you (code from
http://lucumr.pocoo.org/2011/1/22/forwards-compatible-python/)::
import sys
import sys
class UnicodeMixin(object):
class UnicodeMixin(object):
"""Mixin class to handle defining the proper __str__/__unicode__
methods in Python 2 or 3."""
"""Mixin class to handle defining the proper __str__/__unicode__
methods in Python 2 or 3."""
if sys.version_info[0] >= 3: # Python 3
def __str__(self):
return self.__unicode__()
else: # Python 2
def __str__(self):
return self.__unicode__().encode('utf8')
if sys.version_info[0] >= 3: # Python 3
def __str__(self):
return self.__unicode__()
else: # Python 2
def __str__(self):
return self.__unicode__().encode('utf8')
class Spam(UnicodeMixin):
class Spam(UnicodeMixin):
def __unicode__(self):
return u'spam-spam-bacon-spam' # 2to3 will remove the 'u' prefix
def __unicode__(self):
return u'spam-spam-bacon-spam' # 2to3 will remove the 'u' prefix
Specify when opening a file as binary
...
...
@@ -380,11 +380,11 @@ Don't Index on Exceptions
'''''''''''''''''''''''''
In Python 2, the following worked::
>>> exc = Exception(1, 2, 3)
>>> exc.args[1]
2
>>> exc[1] # Python 2 only!
2
>>> exc = Exception(1, 2, 3)
>>> exc.args[1]
2
>>> exc[1] # Python 2 only!
2
But in Python 3, indexing directly off of an exception is an error. You need to
make sure to only index on :attr:`BaseException.args` attribute which is a
...
...
@@ -426,13 +426,13 @@ Manually
To manually convert source code using 2to3_, you use the ``2to3`` script that
is installed with Python 2.6 and later.::
2to3 <directory or file to convert>
2to3 <directory or file to convert>
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 ``-w`` flag::
2to3 -w <stuff to convert>
2to3 -w <stuff to convert>
There are other flags available to control exactly which fixers are applied,
etc.
...
...
@@ -444,20 +444,20 @@ When a user installs your project for Python 3, you can have either
:mod:`distutils` or Distribute_ run 2to3_ on your behalf.
For distutils, use the following idiom::
try: # Python 3
from distutils.command.build_py import build_py_2to3 as build_py
except ImportError: # Python 2
from distutils.command.build_py import build_py
try: # Python 3
from distutils.command.build_py import build_py_2to3 as build_py
except ImportError: # Python 2
from distutils.command.build_py import build_py
setup(cmdclass = {'build_py':build_py},
# ...
)
setup(cmdclass = {'build_py':build_py},
# ...
)
For Distribute::
For Distribute::
setup(use_2to3=True,
# ...
)
setup(use_2to3=True,
# ...
)
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
...
...
@@ -526,30 +526,30 @@ One change between Python 2 and 3 that will require changing how you code is
accessing the currently raised exception. In Python 2 the syntax to access the
current exception is::
try:
raise Exception()
except Exception, exc:
# Current exception is 'exc'
pass
try:
raise Exception()
except Exception, exc:
# Current exception is 'exc'
pass
This syntax changed in Python 3 to::
try:
raise Exception()
except Exception as exc:
# Current exception is 'exc'
pass
try:
raise Exception()
except Exception as exc:
# Current exception is 'exc'
pass
Because of this syntax change you must change to capturing the current
exception to::
try:
raise Exception()
except Exception:
import sys
exc = sys.exc_info()[1]
# Current exception is 'exc'
pass
try:
raise Exception()
except Exception:
import sys
exc = sys.exc_info()[1]
# Current exception is 'exc'
pass
You can get more information about the raised exception from
:func:`sys.exc_info` than simply the current exception instance, but you most
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment