1# 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 = {
9    '__builtin__' : 'builtins',
10    'copy_reg': 'copyreg',
11    'Queue': 'queue',
12    'SocketServer': 'socketserver',
13    'ConfigParser': 'configparser',
14    'repr': 'reprlib',
15    'tkFileDialog': 'tkinter.filedialog',
16    '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',
37    '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',
44    'test.test_support': 'test.support',
45    'commands': 'subprocess',
46    'urlparse' : 'urllib.parse',
47    'robotparser' : 'urllib.robotparser',
48    'urllib2': 'urllib.request',
49    'anydbm': 'dbm',
50    '_abcoll' : 'collections.abc',
51}
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'),
62    ('__builtin__', 'unicode'):    ('builtins', 'str'),
63    ('__builtin__', 'long'):       ('builtins', 'int'),
64    ('itertools', 'izip'):         ('builtins', 'zip'),
65    ('itertools', 'imap'):         ('builtins', 'map'),
66    ('itertools', 'ifilter'):      ('builtins', 'filter'),
67    ('itertools', 'ifilterfalse'): ('itertools', 'filterfalse'),
68    ('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'),
91}
92
93PYTHON2_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
144try:
145    WindowsError
146except NameError:
147    pass
148else:
149    PYTHON2_EXCEPTIONS += ("WindowsError",)
150
151for excname in PYTHON2_EXCEPTIONS:
152    NAME_MAPPING[("exceptions", excname)] = ("builtins", excname)
153
154MULTIPROCESSING_EXCEPTIONS = (
155    'AuthenticationError',
156    'BufferTooShort',
157    'ProcessError',
158    'TimeoutError',
159)
160
161for excname in MULTIPROCESSING_EXCEPTIONS:
162    NAME_MAPPING[("multiprocessing", excname)] = ("multiprocessing.context", excname)
163
164# Same, but for 3.x to 2.x
165REVERSE_IMPORT_MAPPING = dict((v, k) for (k, v) in IMPORT_MAPPING.items())
166assert len(REVERSE_IMPORT_MAPPING) == len(IMPORT_MAPPING)
167REVERSE_NAME_MAPPING = dict((v, k) for (k, v) in NAME_MAPPING.items())
168assert 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    # For compatibility with broken pickles saved in old Python 3 versions
181    'UserDict': 'collections',
182    'UserList': 'collections',
183    'UserString': 'collections',
184    'whichdb': 'dbm',
185    'StringIO':  'io',
186    'cStringIO': 'io',
187})
188
189REVERSE_IMPORT_MAPPING.update({
190    '_bz2': 'bz2',
191    '_dbm': 'dbm',
192    '_functools': 'functools',
193    '_gdbm': 'gdbm',
194    '_pickle': 'pickle',
195})
196
197NAME_MAPPING.update({
198    ('__builtin__', 'basestring'): ('builtins', 'str'),
199    ('exceptions', 'StandardError'): ('builtins', 'Exception'),
200    ('UserDict', 'UserDict'): ('collections', 'UserDict'),
201    ('socket', '_socketobject'): ('socket', 'SocketType'),
202})
203
204REVERSE_NAME_MAPPING.update({
205    ('_functools', 'reduce'): ('__builtin__', 'reduce'),
206    ('tkinter.filedialog', 'FileDialog'): ('FileDialog', 'FileDialog'),
207    ('tkinter.filedialog', 'LoadFileDialog'): ('FileDialog', 'LoadFileDialog'),
208    ('tkinter.filedialog', 'SaveFileDialog'): ('FileDialog', 'SaveFileDialog'),
209    ('tkinter.simpledialog', 'SimpleDialog'): ('SimpleDialog', 'SimpleDialog'),
210    ('xmlrpc.server', 'ServerHTMLDoc'): ('DocXMLRPCServer', 'ServerHTMLDoc'),
211    ('xmlrpc.server', 'XMLRPCDocGenerator'):
212        ('DocXMLRPCServer', 'XMLRPCDocGenerator'),
213    ('xmlrpc.server', 'DocXMLRPCRequestHandler'):
214        ('DocXMLRPCServer', 'DocXMLRPCRequestHandler'),
215    ('xmlrpc.server', 'DocXMLRPCServer'):
216        ('DocXMLRPCServer', 'DocXMLRPCServer'),
217    ('xmlrpc.server', 'DocCGIXMLRPCRequestHandler'):
218        ('DocXMLRPCServer', 'DocCGIXMLRPCRequestHandler'),
219    ('http.server', 'SimpleHTTPRequestHandler'):
220        ('SimpleHTTPServer', 'SimpleHTTPRequestHandler'),
221    ('http.server', 'CGIHTTPRequestHandler'):
222        ('CGIHTTPServer', 'CGIHTTPRequestHandler'),
223    ('_socket', 'socket'): ('socket', '_socketobject'),
224})
225
226PYTHON3_OSERROR_EXCEPTIONS = (
227    'BrokenPipeError',
228    'ChildProcessError',
229    'ConnectionAbortedError',
230    'ConnectionError',
231    'ConnectionRefusedError',
232    'ConnectionResetError',
233    'FileExistsError',
234    'FileNotFoundError',
235    'InterruptedError',
236    'IsADirectoryError',
237    'NotADirectoryError',
238    'PermissionError',
239    'ProcessLookupError',
240    'TimeoutError',
241)
242
243for excname in PYTHON3_OSERROR_EXCEPTIONS:
244    REVERSE_NAME_MAPPING[('builtins', excname)] = ('exceptions', 'OSError')
245
246PYTHON3_IMPORTERROR_EXCEPTIONS = (
247    'ModuleNotFoundError',
248)
249
250for excname in PYTHON3_IMPORTERROR_EXCEPTIONS:
251    REVERSE_NAME_MAPPING[('builtins', excname)] = ('exceptions', 'ImportError')
252