ยปCore Development>Code coverage>Lib/_compat_pickle.py

Python code coverage for Lib/_compat_pickle.py

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