Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
allendowney
GitHub Repository: allendowney/cpython
Path: blob/main/Lib/_compat_pickle.py
12 views
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.
8
IMPORT_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
'ttk': 'tkinter.ttk',
26
'Tkinter': 'tkinter',
27
'markupbase': '_markupbase',
28
'_winreg': 'winreg',
29
'thread': '_thread',
30
'dummy_thread': '_dummy_thread',
31
'dbhash': 'dbm.bsd',
32
'dumbdbm': 'dbm.dumb',
33
'dbm': 'dbm.ndbm',
34
'gdbm': 'dbm.gnu',
35
'xmlrpclib': 'xmlrpc.client',
36
'SimpleXMLRPCServer': 'xmlrpc.server',
37
'httplib': 'http.client',
38
'htmlentitydefs' : 'html.entities',
39
'HTMLParser' : 'html.parser',
40
'Cookie': 'http.cookies',
41
'cookielib': 'http.cookiejar',
42
'BaseHTTPServer': 'http.server',
43
'test.test_support': 'test.support',
44
'commands': 'subprocess',
45
'urlparse' : 'urllib.parse',
46
'robotparser' : 'urllib.robotparser',
47
'urllib2': 'urllib.request',
48
'anydbm': 'dbm',
49
'_abcoll' : 'collections.abc',
50
}
51
52
53
# This contains rename rules that are easy to handle. We ignore the more
54
# complex stuff (e.g. mapping the names in the urllib and types modules).
55
# These rules should be run before import names are fixed.
56
NAME_MAPPING = {
57
('__builtin__', 'xrange'): ('builtins', 'range'),
58
('__builtin__', 'reduce'): ('functools', 'reduce'),
59
('__builtin__', 'intern'): ('sys', 'intern'),
60
('__builtin__', 'unichr'): ('builtins', 'chr'),
61
('__builtin__', 'unicode'): ('builtins', 'str'),
62
('__builtin__', 'long'): ('builtins', 'int'),
63
('itertools', 'izip'): ('builtins', 'zip'),
64
('itertools', 'imap'): ('builtins', 'map'),
65
('itertools', 'ifilter'): ('builtins', 'filter'),
66
('itertools', 'ifilterfalse'): ('itertools', 'filterfalse'),
67
('itertools', 'izip_longest'): ('itertools', 'zip_longest'),
68
('UserDict', 'IterableUserDict'): ('collections', 'UserDict'),
69
('UserList', 'UserList'): ('collections', 'UserList'),
70
('UserString', 'UserString'): ('collections', 'UserString'),
71
('whichdb', 'whichdb'): ('dbm', 'whichdb'),
72
('_socket', 'fromfd'): ('socket', 'fromfd'),
73
('_multiprocessing', 'Connection'): ('multiprocessing.connection', 'Connection'),
74
('multiprocessing.process', 'Process'): ('multiprocessing.context', 'Process'),
75
('multiprocessing.forking', 'Popen'): ('multiprocessing.popen_fork', 'Popen'),
76
('urllib', 'ContentTooShortError'): ('urllib.error', 'ContentTooShortError'),
77
('urllib', 'getproxies'): ('urllib.request', 'getproxies'),
78
('urllib', 'pathname2url'): ('urllib.request', 'pathname2url'),
79
('urllib', 'quote_plus'): ('urllib.parse', 'quote_plus'),
80
('urllib', 'quote'): ('urllib.parse', 'quote'),
81
('urllib', 'unquote_plus'): ('urllib.parse', 'unquote_plus'),
82
('urllib', 'unquote'): ('urllib.parse', 'unquote'),
83
('urllib', 'url2pathname'): ('urllib.request', 'url2pathname'),
84
('urllib', 'urlcleanup'): ('urllib.request', 'urlcleanup'),
85
('urllib', 'urlencode'): ('urllib.parse', 'urlencode'),
86
('urllib', 'urlopen'): ('urllib.request', 'urlopen'),
87
('urllib', 'urlretrieve'): ('urllib.request', 'urlretrieve'),
88
('urllib2', 'HTTPError'): ('urllib.error', 'HTTPError'),
89
('urllib2', 'URLError'): ('urllib.error', 'URLError'),
90
}
91
92
PYTHON2_EXCEPTIONS = (
93
"ArithmeticError",
94
"AssertionError",
95
"AttributeError",
96
"BaseException",
97
"BufferError",
98
"BytesWarning",
99
"DeprecationWarning",
100
"EOFError",
101
"EnvironmentError",
102
"Exception",
103
"FloatingPointError",
104
"FutureWarning",
105
"GeneratorExit",
106
"IOError",
107
"ImportError",
108
"ImportWarning",
109
"IndentationError",
110
"IndexError",
111
"KeyError",
112
"KeyboardInterrupt",
113
"LookupError",
114
"MemoryError",
115
"NameError",
116
"NotImplementedError",
117
"OSError",
118
"OverflowError",
119
"PendingDeprecationWarning",
120
"ReferenceError",
121
"RuntimeError",
122
"RuntimeWarning",
123
# StandardError is gone in Python 3, so we map it to Exception
124
"StopIteration",
125
"SyntaxError",
126
"SyntaxWarning",
127
"SystemError",
128
"SystemExit",
129
"TabError",
130
"TypeError",
131
"UnboundLocalError",
132
"UnicodeDecodeError",
133
"UnicodeEncodeError",
134
"UnicodeError",
135
"UnicodeTranslateError",
136
"UnicodeWarning",
137
"UserWarning",
138
"ValueError",
139
"Warning",
140
"ZeroDivisionError",
141
)
142
143
try:
144
WindowsError
145
except NameError:
146
pass
147
else:
148
PYTHON2_EXCEPTIONS += ("WindowsError",)
149
150
for excname in PYTHON2_EXCEPTIONS:
151
NAME_MAPPING[("exceptions", excname)] = ("builtins", excname)
152
153
MULTIPROCESSING_EXCEPTIONS = (
154
'AuthenticationError',
155
'BufferTooShort',
156
'ProcessError',
157
'TimeoutError',
158
)
159
160
for excname in MULTIPROCESSING_EXCEPTIONS:
161
NAME_MAPPING[("multiprocessing", excname)] = ("multiprocessing.context", excname)
162
163
# Same, but for 3.x to 2.x
164
REVERSE_IMPORT_MAPPING = dict((v, k) for (k, v) in IMPORT_MAPPING.items())
165
assert len(REVERSE_IMPORT_MAPPING) == len(IMPORT_MAPPING)
166
REVERSE_NAME_MAPPING = dict((v, k) for (k, v) in NAME_MAPPING.items())
167
assert len(REVERSE_NAME_MAPPING) == len(NAME_MAPPING)
168
169
# Non-mutual mappings.
170
171
IMPORT_MAPPING.update({
172
'cPickle': 'pickle',
173
'_elementtree': 'xml.etree.ElementTree',
174
'FileDialog': 'tkinter.filedialog',
175
'SimpleDialog': 'tkinter.simpledialog',
176
'DocXMLRPCServer': 'xmlrpc.server',
177
'SimpleHTTPServer': 'http.server',
178
'CGIHTTPServer': 'http.server',
179
# For compatibility with broken pickles saved in old Python 3 versions
180
'UserDict': 'collections',
181
'UserList': 'collections',
182
'UserString': 'collections',
183
'whichdb': 'dbm',
184
'StringIO': 'io',
185
'cStringIO': 'io',
186
})
187
188
REVERSE_IMPORT_MAPPING.update({
189
'_bz2': 'bz2',
190
'_dbm': 'dbm',
191
'_functools': 'functools',
192
'_gdbm': 'gdbm',
193
'_pickle': 'pickle',
194
})
195
196
NAME_MAPPING.update({
197
('__builtin__', 'basestring'): ('builtins', 'str'),
198
('exceptions', 'StandardError'): ('builtins', 'Exception'),
199
('UserDict', 'UserDict'): ('collections', 'UserDict'),
200
('socket', '_socketobject'): ('socket', 'SocketType'),
201
})
202
203
REVERSE_NAME_MAPPING.update({
204
('_functools', 'reduce'): ('__builtin__', 'reduce'),
205
('tkinter.filedialog', 'FileDialog'): ('FileDialog', 'FileDialog'),
206
('tkinter.filedialog', 'LoadFileDialog'): ('FileDialog', 'LoadFileDialog'),
207
('tkinter.filedialog', 'SaveFileDialog'): ('FileDialog', 'SaveFileDialog'),
208
('tkinter.simpledialog', 'SimpleDialog'): ('SimpleDialog', 'SimpleDialog'),
209
('xmlrpc.server', 'ServerHTMLDoc'): ('DocXMLRPCServer', 'ServerHTMLDoc'),
210
('xmlrpc.server', 'XMLRPCDocGenerator'):
211
('DocXMLRPCServer', 'XMLRPCDocGenerator'),
212
('xmlrpc.server', 'DocXMLRPCRequestHandler'):
213
('DocXMLRPCServer', 'DocXMLRPCRequestHandler'),
214
('xmlrpc.server', 'DocXMLRPCServer'):
215
('DocXMLRPCServer', 'DocXMLRPCServer'),
216
('xmlrpc.server', 'DocCGIXMLRPCRequestHandler'):
217
('DocXMLRPCServer', 'DocCGIXMLRPCRequestHandler'),
218
('http.server', 'SimpleHTTPRequestHandler'):
219
('SimpleHTTPServer', 'SimpleHTTPRequestHandler'),
220
('http.server', 'CGIHTTPRequestHandler'):
221
('CGIHTTPServer', 'CGIHTTPRequestHandler'),
222
('_socket', 'socket'): ('socket', '_socketobject'),
223
})
224
225
PYTHON3_OSERROR_EXCEPTIONS = (
226
'BrokenPipeError',
227
'ChildProcessError',
228
'ConnectionAbortedError',
229
'ConnectionError',
230
'ConnectionRefusedError',
231
'ConnectionResetError',
232
'FileExistsError',
233
'FileNotFoundError',
234
'InterruptedError',
235
'IsADirectoryError',
236
'NotADirectoryError',
237
'PermissionError',
238
'ProcessLookupError',
239
'TimeoutError',
240
)
241
242
for excname in PYTHON3_OSERROR_EXCEPTIONS:
243
REVERSE_NAME_MAPPING[('builtins', excname)] = ('exceptions', 'OSError')
244
245
PYTHON3_IMPORTERROR_EXCEPTIONS = (
246
'ModuleNotFoundError',
247
)
248
249
for excname in PYTHON3_IMPORTERROR_EXCEPTIONS:
250
REVERSE_NAME_MAPPING[('builtins', excname)] = ('exceptions', 'ImportError')
251
del excname
252
253