blob: 8bb1cf80afa55918c068f2139a18dc2abf262f72 [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",
Emily Morehouse8f59ee02019-01-24 16:49:56 -0700131 "TargetScopeError",
Walter Doerwald9d1dbca2013-12-02 11:41:01 +0100132 "TypeError",
133 "UnboundLocalError",
134 "UnicodeDecodeError",
135 "UnicodeEncodeError",
136 "UnicodeError",
137 "UnicodeTranslateError",
138 "UnicodeWarning",
139 "UserWarning",
140 "ValueError",
141 "Warning",
142 "ZeroDivisionError",
143)
144
Serhiy Storchakab9100e52015-03-31 16:49:26 +0300145try:
146 WindowsError
147except NameError:
148 pass
149else:
150 PYTHON2_EXCEPTIONS += ("WindowsError",)
151
Walter Doerwald9d1dbca2013-12-02 11:41:01 +0100152for excname in PYTHON2_EXCEPTIONS:
153 NAME_MAPPING[("exceptions", excname)] = ("builtins", excname)
154
Serhiy Storchakabfe18242015-03-31 13:12:37 +0300155MULTIPROCESSING_EXCEPTIONS = (
156 'AuthenticationError',
157 'BufferTooShort',
158 'ProcessError',
159 'TimeoutError',
160)
161
162for excname in MULTIPROCESSING_EXCEPTIONS:
163 NAME_MAPPING[("multiprocessing", excname)] = ("multiprocessing.context", excname)
Walter Doerwald9d1dbca2013-12-02 11:41:01 +0100164
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000165# Same, but for 3.x to 2.x
166REVERSE_IMPORT_MAPPING = dict((v, k) for (k, v) in IMPORT_MAPPING.items())
Serhiy Storchakabfe18242015-03-31 13:12:37 +0300167assert len(REVERSE_IMPORT_MAPPING) == len(IMPORT_MAPPING)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000168REVERSE_NAME_MAPPING = dict((v, k) for (k, v) in NAME_MAPPING.items())
Serhiy Storchakabfe18242015-03-31 13:12:37 +0300169assert len(REVERSE_NAME_MAPPING) == len(NAME_MAPPING)
170
171# Non-mutual mappings.
172
173IMPORT_MAPPING.update({
174 'cPickle': 'pickle',
175 '_elementtree': 'xml.etree.ElementTree',
176 'FileDialog': 'tkinter.filedialog',
177 'SimpleDialog': 'tkinter.simpledialog',
178 'DocXMLRPCServer': 'xmlrpc.server',
179 'SimpleHTTPServer': 'http.server',
180 'CGIHTTPServer': 'http.server',
Serhiy Storchaka111c7b92016-01-18 21:35:22 +0200181 # For compatibility with broken pickles saved in old Python 3 versions
182 'UserDict': 'collections',
183 'UserList': 'collections',
184 'UserString': 'collections',
185 'whichdb': 'dbm',
186 'StringIO': 'io',
187 'cStringIO': 'io',
Serhiy Storchakabfe18242015-03-31 13:12:37 +0300188})
189
190REVERSE_IMPORT_MAPPING.update({
191 '_bz2': 'bz2',
192 '_dbm': 'dbm',
193 '_functools': 'functools',
194 '_gdbm': 'gdbm',
195 '_pickle': 'pickle',
196})
197
198NAME_MAPPING.update({
199 ('__builtin__', 'basestring'): ('builtins', 'str'),
200 ('exceptions', 'StandardError'): ('builtins', 'Exception'),
201 ('UserDict', 'UserDict'): ('collections', 'UserDict'),
202 ('socket', '_socketobject'): ('socket', 'SocketType'),
203})
204
205REVERSE_NAME_MAPPING.update({
206 ('_functools', 'reduce'): ('__builtin__', 'reduce'),
207 ('tkinter.filedialog', 'FileDialog'): ('FileDialog', 'FileDialog'),
208 ('tkinter.filedialog', 'LoadFileDialog'): ('FileDialog', 'LoadFileDialog'),
209 ('tkinter.filedialog', 'SaveFileDialog'): ('FileDialog', 'SaveFileDialog'),
210 ('tkinter.simpledialog', 'SimpleDialog'): ('SimpleDialog', 'SimpleDialog'),
211 ('xmlrpc.server', 'ServerHTMLDoc'): ('DocXMLRPCServer', 'ServerHTMLDoc'),
212 ('xmlrpc.server', 'XMLRPCDocGenerator'):
213 ('DocXMLRPCServer', 'XMLRPCDocGenerator'),
214 ('xmlrpc.server', 'DocXMLRPCRequestHandler'):
215 ('DocXMLRPCServer', 'DocXMLRPCRequestHandler'),
216 ('xmlrpc.server', 'DocXMLRPCServer'):
217 ('DocXMLRPCServer', 'DocXMLRPCServer'),
218 ('xmlrpc.server', 'DocCGIXMLRPCRequestHandler'):
219 ('DocXMLRPCServer', 'DocCGIXMLRPCRequestHandler'),
220 ('http.server', 'SimpleHTTPRequestHandler'):
221 ('SimpleHTTPServer', 'SimpleHTTPRequestHandler'),
222 ('http.server', 'CGIHTTPRequestHandler'):
223 ('CGIHTTPServer', 'CGIHTTPRequestHandler'),
224 ('_socket', 'socket'): ('socket', '_socketobject'),
225})
226
227PYTHON3_OSERROR_EXCEPTIONS = (
228 'BrokenPipeError',
229 'ChildProcessError',
230 'ConnectionAbortedError',
231 'ConnectionError',
232 'ConnectionRefusedError',
233 'ConnectionResetError',
234 'FileExistsError',
235 'FileNotFoundError',
236 'InterruptedError',
237 'IsADirectoryError',
238 'NotADirectoryError',
239 'PermissionError',
240 'ProcessLookupError',
241 'TimeoutError',
242)
243
244for excname in PYTHON3_OSERROR_EXCEPTIONS:
245 REVERSE_NAME_MAPPING[('builtins', excname)] = ('exceptions', 'OSError')
Eric Snowc9432652016-09-07 15:42:32 -0700246
247PYTHON3_IMPORTERROR_EXCEPTIONS = (
248 'ModuleNotFoundError',
249)
250
251for excname in PYTHON3_IMPORTERROR_EXCEPTIONS:
252 REVERSE_NAME_MAPPING[('builtins', excname)] = ('exceptions', 'ImportError')