_compat_pickle.py 8.6 KB

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