blob: 6e39d4aee3e8b29fe4acd30c51ac7196d7c0d6da [file] [log] [blame]
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00001# This module is used to map the old Python 2 names to the new names used in
2# Python 3 for the pickle module. This needed to make pickle streams
3# generated with Python 2 loadable by Python 3.
4
5# This is a copy of lib2to3.fixes.fix_imports.MAPPING. We cannot import
6# lib2to3 and use the mapping defined there, because lib2to3 uses pickle.
7# Thus, this could cause the module to be imported recursively.
8IMPORT_MAPPING = {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00009 '__builtin__' : 'builtins',
10 'copy_reg': 'copyreg',
11 'Queue': 'queue',
12 'SocketServer': 'socketserver',
13 'ConfigParser': 'configparser',
14 'repr': 'reprlib',
Antoine Pitroud9dfaa92009-06-04 20:32:06 +000015 'tkFileDialog': 'tkinter.filedialog',
Antoine Pitroud9dfaa92009-06-04 20:32:06 +000016 'tkSimpleDialog': 'tkinter.simpledialog',
17 'tkColorChooser': 'tkinter.colorchooser',
18 'tkCommonDialog': 'tkinter.commondialog',
19 'Dialog': 'tkinter.dialog',
20 'Tkdnd': 'tkinter.dnd',
21 'tkFont': 'tkinter.font',
22 'tkMessageBox': 'tkinter.messagebox',
23 'ScrolledText': 'tkinter.scrolledtext',
24 'Tkconstants': 'tkinter.constants',
25 'Tix': 'tkinter.tix',
26 'ttk': 'tkinter.ttk',
27 'Tkinter': 'tkinter',
28 'markupbase': '_markupbase',
29 '_winreg': 'winreg',
30 'thread': '_thread',
31 'dummy_thread': '_dummy_thread',
32 'dbhash': 'dbm.bsd',
33 'dumbdbm': 'dbm.dumb',
34 'dbm': 'dbm.ndbm',
35 'gdbm': 'dbm.gnu',
36 'xmlrpclib': 'xmlrpc.client',
Antoine Pitroud9dfaa92009-06-04 20:32:06 +000037 'SimpleXMLRPCServer': 'xmlrpc.server',
38 'httplib': 'http.client',
39 'htmlentitydefs' : 'html.entities',
40 'HTMLParser' : 'html.parser',
41 'Cookie': 'http.cookies',
42 'cookielib': 'http.cookiejar',
43 'BaseHTTPServer': 'http.server',
Antoine Pitroud9dfaa92009-06-04 20:32:06 +000044 'test.test_support': 'test.support',
45 'commands': 'subprocess',
Antoine Pitroud9dfaa92009-06-04 20:32:06 +000046 'urlparse' : 'urllib.parse',
47 'robotparser' : 'urllib.robotparser',
Serhiy Storchakabfe18242015-03-31 13:12:37 +030048 'urllib2': 'urllib.request',
49 'anydbm': 'dbm',
50 '_abcoll' : 'collections.abc',
Antoine Pitroud9dfaa92009-06-04 20:32:06 +000051}
52
53
54# This contains rename rules that are easy to handle. We ignore the more
55# complex stuff (e.g. mapping the names in the urllib and types modules).
56# These rules should be run before import names are fixed.
57NAME_MAPPING = {
58 ('__builtin__', 'xrange'): ('builtins', 'range'),
59 ('__builtin__', 'reduce'): ('functools', 'reduce'),
60 ('__builtin__', 'intern'): ('sys', 'intern'),
61 ('__builtin__', 'unichr'): ('builtins', 'chr'),
Serhiy Storchakabfe18242015-03-31 13:12:37 +030062 ('__builtin__', 'unicode'): ('builtins', 'str'),
Antoine Pitroud9dfaa92009-06-04 20:32:06 +000063 ('__builtin__', 'long'): ('builtins', 'int'),
64 ('itertools', 'izip'): ('builtins', 'zip'),
65 ('itertools', 'imap'): ('builtins', 'map'),
66 ('itertools', 'ifilter'): ('builtins', 'filter'),
67 ('itertools', 'ifilterfalse'): ('itertools', 'filterfalse'),
Serhiy Storchakabfe18242015-03-31 13:12:37 +030068 ('itertools', 'izip_longest'): ('itertools', 'zip_longest'),
69 ('UserDict', 'IterableUserDict'): ('collections', 'UserDict'),
70 ('UserList', 'UserList'): ('collections', 'UserList'),
71 ('UserString', 'UserString'): ('collections', 'UserString'),
72 ('whichdb', 'whichdb'): ('dbm', 'whichdb'),
73 ('_socket', 'fromfd'): ('socket', 'fromfd'),
74 ('_multiprocessing', 'Connection'): ('multiprocessing.connection', 'Connection'),
75 ('multiprocessing.process', 'Process'): ('multiprocessing.context', 'Process'),
76 ('multiprocessing.forking', 'Popen'): ('multiprocessing.popen_fork', 'Popen'),
77 ('urllib', 'ContentTooShortError'): ('urllib.error', 'ContentTooShortError'),
78 ('urllib', 'getproxies'): ('urllib.request', 'getproxies'),
79 ('urllib', 'pathname2url'): ('urllib.request', 'pathname2url'),
80 ('urllib', 'quote_plus'): ('urllib.parse', 'quote_plus'),
81 ('urllib', 'quote'): ('urllib.parse', 'quote'),
82 ('urllib', 'unquote_plus'): ('urllib.parse', 'unquote_plus'),
83 ('urllib', 'unquote'): ('urllib.parse', 'unquote'),
84 ('urllib', 'url2pathname'): ('urllib.request', 'url2pathname'),
85 ('urllib', 'urlcleanup'): ('urllib.request', 'urlcleanup'),
86 ('urllib', 'urlencode'): ('urllib.parse', 'urlencode'),
87 ('urllib', 'urlopen'): ('urllib.request', 'urlopen'),
88 ('urllib', 'urlretrieve'): ('urllib.request', 'urlretrieve'),
89 ('urllib2', 'HTTPError'): ('urllib.error', 'HTTPError'),
90 ('urllib2', 'URLError'): ('urllib.error', 'URLError'),
Antoine Pitroud9dfaa92009-06-04 20:32:06 +000091}
92
Walter Doerwald9d1dbca2013-12-02 11:41:01 +010093PYTHON2_EXCEPTIONS = (
94 "ArithmeticError",
95 "AssertionError",
96 "AttributeError",
97 "BaseException",
98 "BufferError",
99 "BytesWarning",
100 "DeprecationWarning",
101 "EOFError",
102 "EnvironmentError",
103 "Exception",
104 "FloatingPointError",
105 "FutureWarning",
106 "GeneratorExit",
107 "IOError",
108 "ImportError",
109 "ImportWarning",
110 "IndentationError",
111 "IndexError",
112 "KeyError",
113 "KeyboardInterrupt",
114 "LookupError",
115 "MemoryError",
116 "NameError",
117 "NotImplementedError",
118 "OSError",
119 "OverflowError",
120 "PendingDeprecationWarning",
121 "ReferenceError",
122 "RuntimeError",
123 "RuntimeWarning",
124 # StandardError is gone in Python 3, so we map it to Exception
125 "StopIteration",
126 "SyntaxError",
127 "SyntaxWarning",
128 "SystemError",
129 "SystemExit",
130 "TabError",
131 "TypeError",
132 "UnboundLocalError",
133 "UnicodeDecodeError",
134 "UnicodeEncodeError",
135 "UnicodeError",
136 "UnicodeTranslateError",
137 "UnicodeWarning",
138 "UserWarning",
139 "ValueError",
140 "Warning",
141 "ZeroDivisionError",
142)
143
Serhiy Storchakab9100e52015-03-31 16:49:26 +0300144try:
145 WindowsError
146except NameError:
147 pass
148else:
149 PYTHON2_EXCEPTIONS += ("WindowsError",)
150
Walter Doerwald9d1dbca2013-12-02 11:41:01 +0100151for excname in PYTHON2_EXCEPTIONS:
152 NAME_MAPPING[("exceptions", excname)] = ("builtins", excname)
153
Serhiy Storchakabfe18242015-03-31 13:12:37 +0300154MULTIPROCESSING_EXCEPTIONS = (
155 'AuthenticationError',
156 'BufferTooShort',
157 'ProcessError',
158 'TimeoutError',
159)
160
161for excname in MULTIPROCESSING_EXCEPTIONS:
162 NAME_MAPPING[("multiprocessing", excname)] = ("multiprocessing.context", excname)
Walter Doerwald9d1dbca2013-12-02 11:41:01 +0100163
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000164# Same, but for 3.x to 2.x
165REVERSE_IMPORT_MAPPING = dict((v, k) for (k, v) in IMPORT_MAPPING.items())
Serhiy Storchakabfe18242015-03-31 13:12:37 +0300166assert len(REVERSE_IMPORT_MAPPING) == len(IMPORT_MAPPING)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000167REVERSE_NAME_MAPPING = dict((v, k) for (k, v) in NAME_MAPPING.items())
Serhiy Storchakabfe18242015-03-31 13:12:37 +0300168assert len(REVERSE_NAME_MAPPING) == len(NAME_MAPPING)
169
170# Non-mutual mappings.
171
172IMPORT_MAPPING.update({
173 'cPickle': 'pickle',
174 '_elementtree': 'xml.etree.ElementTree',
175 'FileDialog': 'tkinter.filedialog',
176 'SimpleDialog': 'tkinter.simpledialog',
177 'DocXMLRPCServer': 'xmlrpc.server',
178 'SimpleHTTPServer': 'http.server',
179 'CGIHTTPServer': 'http.server',
180})
181
182REVERSE_IMPORT_MAPPING.update({
183 '_bz2': 'bz2',
184 '_dbm': 'dbm',
185 '_functools': 'functools',
186 '_gdbm': 'gdbm',
187 '_pickle': 'pickle',
188})
189
190NAME_MAPPING.update({
191 ('__builtin__', 'basestring'): ('builtins', 'str'),
192 ('exceptions', 'StandardError'): ('builtins', 'Exception'),
193 ('UserDict', 'UserDict'): ('collections', 'UserDict'),
194 ('socket', '_socketobject'): ('socket', 'SocketType'),
195})
196
197REVERSE_NAME_MAPPING.update({
198 ('_functools', 'reduce'): ('__builtin__', 'reduce'),
199 ('tkinter.filedialog', 'FileDialog'): ('FileDialog', 'FileDialog'),
200 ('tkinter.filedialog', 'LoadFileDialog'): ('FileDialog', 'LoadFileDialog'),
201 ('tkinter.filedialog', 'SaveFileDialog'): ('FileDialog', 'SaveFileDialog'),
202 ('tkinter.simpledialog', 'SimpleDialog'): ('SimpleDialog', 'SimpleDialog'),
203 ('xmlrpc.server', 'ServerHTMLDoc'): ('DocXMLRPCServer', 'ServerHTMLDoc'),
204 ('xmlrpc.server', 'XMLRPCDocGenerator'):
205 ('DocXMLRPCServer', 'XMLRPCDocGenerator'),
206 ('xmlrpc.server', 'DocXMLRPCRequestHandler'):
207 ('DocXMLRPCServer', 'DocXMLRPCRequestHandler'),
208 ('xmlrpc.server', 'DocXMLRPCServer'):
209 ('DocXMLRPCServer', 'DocXMLRPCServer'),
210 ('xmlrpc.server', 'DocCGIXMLRPCRequestHandler'):
211 ('DocXMLRPCServer', 'DocCGIXMLRPCRequestHandler'),
212 ('http.server', 'SimpleHTTPRequestHandler'):
213 ('SimpleHTTPServer', 'SimpleHTTPRequestHandler'),
214 ('http.server', 'CGIHTTPRequestHandler'):
215 ('CGIHTTPServer', 'CGIHTTPRequestHandler'),
216 ('_socket', 'socket'): ('socket', '_socketobject'),
217})
218
219PYTHON3_OSERROR_EXCEPTIONS = (
220 'BrokenPipeError',
221 'ChildProcessError',
222 'ConnectionAbortedError',
223 'ConnectionError',
224 'ConnectionRefusedError',
225 'ConnectionResetError',
226 'FileExistsError',
227 'FileNotFoundError',
228 'InterruptedError',
229 'IsADirectoryError',
230 'NotADirectoryError',
231 'PermissionError',
232 'ProcessLookupError',
233 'TimeoutError',
234)
235
236for excname in PYTHON3_OSERROR_EXCEPTIONS:
237 REVERSE_NAME_MAPPING[('builtins', excname)] = ('exceptions', 'OSError')