mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 23:10:17 +00:00
454 lines
7.7 KiB
Plaintext
454 lines
7.7 KiB
Plaintext
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>Porting code to Python 3 with 2to3 - Dive into Python 3</title>
|
|
<link rel="stylesheet" type="text/css" href="dip3.css">
|
|
</head>
|
|
<body>
|
|
<h1>Porting code to Python 3 with <code>2to3</code></h1>
|
|
|
|
<ol class="toc">
|
|
</ol>
|
|
|
|
<section>
|
|
<h2 id="ne">ne</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
<table>
|
|
<tr>
|
|
<th>Python 2</th>
|
|
<th>Python 3</th>
|
|
</tr>
|
|
<tr>
|
|
<td><code>if x <> y:</code></td>
|
|
<td><code>if x != y:</code></td>
|
|
</tr>
|
|
</table>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="has_key">has_key</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="apply">apply</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="intern">intern</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="print">print</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="exec">exec</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="repr">repr</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="except">except</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="raise">raise</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="throw">throw</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="long">long</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="dict">dict</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="xrange">xrange</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="raw_input">raw_input</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="funcattrs">funcattrs</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="xreadlines">xreadlines</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="imports">imports</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="imports2">imports2</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
1533: fixer = None
|
|
<section>
|
|
<h2 id="urllib">urllib</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="input">input</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="tuple_params">tuple_params</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="methodattrs">methodattrs</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="next">next</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="nonzero">nonzero</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="numliterals">numliterals</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="renames">renames</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="unicode">unicode</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="callable">callable</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="filter">filter</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="map">map</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="zip">zip</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="standarderror">standarderror</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="types">types</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="idioms">idioms</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="basestring">basestring</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="buffer">buffer</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="future">future</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="itertools">itertools</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
3256: fixer = 'itertools_imports'
|
|
<section>
|
|
<h2 id="import">import</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="set_literal">set_literal</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="sys_exc">sys_exc</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
<section>
|
|
<h2 id="paren">paren</h2>
|
|
|
|
<p>FIXME intro</p>
|
|
|
|
</section>
|
|
|
|
3657: fixer = 'metaclass'
|
|
3822: fixer = 'getcwdu'
|
|
</ol>
|
|
|
|
# Lib/lib2to3/tests/test_fixers.py
|
|
|
|
- <>
|
|
- has_key
|
|
- apply()
|
|
- intern()
|
|
- print
|
|
- exec()
|
|
- repr()
|
|
- except
|
|
- raise
|
|
- throw
|
|
- long()
|
|
- dict()
|
|
- xrange()
|
|
- raw_input()
|
|
- input()
|
|
- func_* attributes
|
|
- xreadlines()
|
|
- imports of modules that have been renamed:
|
|
# Lib/lib2to3/fixes/fix_imports.py
|
|
MAPPING = {'StringIO': 'io',
|
|
'cStringIO': 'io',
|
|
'cPickle': 'pickle',
|
|
'__builtin__' : 'builtins',
|
|
'copy_reg': 'copyreg',
|
|
'Queue': 'queue',
|
|
'SocketServer': 'socketserver',
|
|
'ConfigParser': 'configparser',
|
|
'repr': 'reprlib',
|
|
'FileDialog': 'tkinter.filedialog',
|
|
'tkFileDialog': 'tkinter.filedialog',
|
|
'SimpleDialog': 'tkinter.simpledialog',
|
|
'tkSimpleDialog': 'tkinter.simpledialog',
|
|
'tkColorChooser': 'tkinter.colorchooser',
|
|
'tkCommonDialog': 'tkinter.commondialog',
|
|
'Dialog': 'tkinter.dialog',
|
|
'Tkdnd': 'tkinter.dnd',
|
|
'tkFont': 'tkinter.font',
|
|
'tkMessageBox': 'tkinter.messagebox',
|
|
'ScrolledText': 'tkinter.scrolledtext',
|
|
'turtle': 'tkinter.turtle',
|
|
'Tkconstants': 'tkinter.constants',
|
|
'Tix': 'tkinter.tix',
|
|
'Tkinter': 'tkinter',
|
|
'markupbase': '_markupbase',
|
|
'_winreg': 'winreg',
|
|
'thread': '_thread',
|
|
'dummy_thread': '_dummy_thread',
|
|
# anydbm and whichdb are handled by fix_imports2
|
|
'dbhash': 'dbm.bsd',
|
|
'dumbdbm': 'dbm.dumb',
|
|
'dbm': 'dbm.ndbm',
|
|
'gdbm': 'dbm.gnu',
|
|
'xmlrpclib': 'xmlrpc.client',
|
|
'DocXMLRPCServer': 'xmlrpc.server',
|
|
'SimpleXMLRPCServer': 'xmlrpc.server',
|
|
'httplib': 'http.client',
|
|
'Cookie': 'http.cookies',
|
|
'cookielib': 'http.cookiejar',
|
|
'BaseHTTPServer': 'http.server',
|
|
'SimpleHTTPServer': 'http.server',
|
|
'CGIHTTPServer': 'http.server',
|
|
#'test.test_support': 'test.support',
|
|
'commands': 'subprocess',
|
|
'UserString' : 'collections',
|
|
'UserList' : 'collections',
|
|
'urlparse' : 'urllib.parse',
|
|
'robotparser' : 'urllib.robotparser',
|
|
}
|
|
- imports of urllib/urllib2
|
|
# Lib/lib2to3/fixes/fix_urllib.py
|
|
MAPPING = {'urllib': [
|
|
('urllib.request',
|
|
['URLOpener', 'FancyURLOpener', 'urlretrieve',
|
|
'_urlopener', 'urlcleanup']),
|
|
('urllib.parse',
|
|
['quote', 'quote_plus', 'unquote', 'unquote_plus',
|
|
'urlencode', 'pahtname2url', 'url2pathname']),
|
|
('urllib.error',
|
|
['ContentTooShortError'])],
|
|
'urllib2' : [
|
|
('urllib.request',
|
|
['urlopen', 'install_opener', 'build_opener',
|
|
'Request', 'OpenerDirector', 'BaseHandler',
|
|
'HTTPDefaultErrorHandler', 'HTTPRedirectHandler',
|
|
'HTTPCookieProcessor', 'ProxyHandler',
|
|
'HTTPPasswordMgr',
|
|
'HTTPPasswordMgrWithDefaultRealm',
|
|
'AbstractBasicAuthHandler',
|
|
'HTTPBasicAuthHandler', 'ProxyBasicAuthHandler',
|
|
'AbstractDigestAuthHandler',
|
|
'HTTPDigestAuthHander', 'ProxyDigestAuthHandler',
|
|
'HTTPHandler', 'HTTPSHandler', 'FileHandler',
|
|
'FTPHandler', 'CacheFTPHandler',
|
|
'UnknownHandler']),
|
|
('urllib.error',
|
|
['URLError', 'HTTPError'])],
|
|
}
|
|
- tuple params (?!??!)
|
|
- __class__ special attribute
|
|
- __nonzero__ special method
|
|
- next()
|
|
- sys.maxint and sys.maxsize
|
|
- number literals
|
|
- Unicode string literals
|
|
- callable()
|
|
- filter()
|
|
- map()
|
|
- zip()
|
|
- StandardError()
|
|
- types module
|
|
- basestring type
|
|
- itertools functions
|
|
- itertools imports
|
|
- relative imports
|
|
- sys.exc_type, sys.exc_value, sys.exc_traceback
|
|
- list comprehensions over tuples
|
|
- os.getcwdu()
|
|
- metaclasses
|
|
- set literals (EXPLICIT)
|
|
- whitespace around commas (EXPLICIT)
|
|
- buffer() (EXPLICIT)
|
|
- common idioms (EXPLICIT)
|
|
- while 1
|
|
- type(X) == T, type(X) != T
|
|
- type(X) is T, type(X) is not T
|
|
- T is type(X), T is not type(X)
|
|
- list.sort()
|