Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 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 = { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 9 | '__builtin__' : 'builtins', |
| 10 | 'copy_reg': 'copyreg', |
| 11 | 'Queue': 'queue', |
| 12 | 'SocketServer': 'socketserver', |
| 13 | 'ConfigParser': 'configparser', |
| 14 | 'repr': 'reprlib', |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 15 | 'tkFileDialog': 'tkinter.filedialog', |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 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', |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 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', |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 44 | 'test.test_support': 'test.support', |
| 45 | 'commands': 'subprocess', |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 46 | 'urlparse' : 'urllib.parse', |
| 47 | 'robotparser' : 'urllib.robotparser', |
Serhiy Storchaka | bfe1824 | 2015-03-31 13:12:37 +0300 | [diff] [blame] | 48 | 'urllib2': 'urllib.request', |
| 49 | 'anydbm': 'dbm', |
| 50 | '_abcoll' : 'collections.abc', |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 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. |
| 57 | NAME_MAPPING = { |
| 58 | ('__builtin__', 'xrange'): ('builtins', 'range'), |
| 59 | ('__builtin__', 'reduce'): ('functools', 'reduce'), |
| 60 | ('__builtin__', 'intern'): ('sys', 'intern'), |
| 61 | ('__builtin__', 'unichr'): ('builtins', 'chr'), |
Serhiy Storchaka | bfe1824 | 2015-03-31 13:12:37 +0300 | [diff] [blame] | 62 | ('__builtin__', 'unicode'): ('builtins', 'str'), |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 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'), |
Serhiy Storchaka | bfe1824 | 2015-03-31 13:12:37 +0300 | [diff] [blame] | 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'), |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Walter Doerwald | 9d1dbca | 2013-12-02 11:41:01 +0100 | [diff] [blame] | 93 | PYTHON2_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 | |
Serhiy Storchaka | b9100e5 | 2015-03-31 16:49:26 +0300 | [diff] [blame] | 144 | try: |
| 145 | WindowsError |
| 146 | except NameError: |
| 147 | pass |
| 148 | else: |
| 149 | PYTHON2_EXCEPTIONS += ("WindowsError",) |
| 150 | |
Walter Doerwald | 9d1dbca | 2013-12-02 11:41:01 +0100 | [diff] [blame] | 151 | for excname in PYTHON2_EXCEPTIONS: |
| 152 | NAME_MAPPING[("exceptions", excname)] = ("builtins", excname) |
| 153 | |
Serhiy Storchaka | bfe1824 | 2015-03-31 13:12:37 +0300 | [diff] [blame] | 154 | MULTIPROCESSING_EXCEPTIONS = ( |
| 155 | 'AuthenticationError', |
| 156 | 'BufferTooShort', |
| 157 | 'ProcessError', |
| 158 | 'TimeoutError', |
| 159 | ) |
| 160 | |
| 161 | for excname in MULTIPROCESSING_EXCEPTIONS: |
| 162 | NAME_MAPPING[("multiprocessing", excname)] = ("multiprocessing.context", excname) |
Walter Doerwald | 9d1dbca | 2013-12-02 11:41:01 +0100 | [diff] [blame] | 163 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 164 | # Same, but for 3.x to 2.x |
| 165 | REVERSE_IMPORT_MAPPING = dict((v, k) for (k, v) in IMPORT_MAPPING.items()) |
Serhiy Storchaka | bfe1824 | 2015-03-31 13:12:37 +0300 | [diff] [blame] | 166 | assert len(REVERSE_IMPORT_MAPPING) == len(IMPORT_MAPPING) |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 167 | REVERSE_NAME_MAPPING = dict((v, k) for (k, v) in NAME_MAPPING.items()) |
Serhiy Storchaka | bfe1824 | 2015-03-31 13:12:37 +0300 | [diff] [blame] | 168 | assert len(REVERSE_NAME_MAPPING) == len(NAME_MAPPING) |
| 169 | |
| 170 | # Non-mutual mappings. |
| 171 | |
| 172 | IMPORT_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 | }) |
| 181 | |
| 182 | REVERSE_IMPORT_MAPPING.update({ |
| 183 | '_bz2': 'bz2', |
| 184 | '_dbm': 'dbm', |
| 185 | '_functools': 'functools', |
| 186 | '_gdbm': 'gdbm', |
| 187 | '_pickle': 'pickle', |
| 188 | }) |
| 189 | |
| 190 | NAME_MAPPING.update({ |
| 191 | ('__builtin__', 'basestring'): ('builtins', 'str'), |
| 192 | ('exceptions', 'StandardError'): ('builtins', 'Exception'), |
| 193 | ('UserDict', 'UserDict'): ('collections', 'UserDict'), |
| 194 | ('socket', '_socketobject'): ('socket', 'SocketType'), |
| 195 | }) |
| 196 | |
| 197 | REVERSE_NAME_MAPPING.update({ |
| 198 | ('_functools', 'reduce'): ('__builtin__', 'reduce'), |
| 199 | ('tkinter.filedialog', 'FileDialog'): ('FileDialog', 'FileDialog'), |
| 200 | ('tkinter.filedialog', 'LoadFileDialog'): ('FileDialog', 'LoadFileDialog'), |
| 201 | ('tkinter.filedialog', 'SaveFileDialog'): ('FileDialog', 'SaveFileDialog'), |
| 202 | ('tkinter.simpledialog', 'SimpleDialog'): ('SimpleDialog', 'SimpleDialog'), |
| 203 | ('xmlrpc.server', 'ServerHTMLDoc'): ('DocXMLRPCServer', 'ServerHTMLDoc'), |
| 204 | ('xmlrpc.server', 'XMLRPCDocGenerator'): |
| 205 | ('DocXMLRPCServer', 'XMLRPCDocGenerator'), |
| 206 | ('xmlrpc.server', 'DocXMLRPCRequestHandler'): |
| 207 | ('DocXMLRPCServer', 'DocXMLRPCRequestHandler'), |
| 208 | ('xmlrpc.server', 'DocXMLRPCServer'): |
| 209 | ('DocXMLRPCServer', 'DocXMLRPCServer'), |
| 210 | ('xmlrpc.server', 'DocCGIXMLRPCRequestHandler'): |
| 211 | ('DocXMLRPCServer', 'DocCGIXMLRPCRequestHandler'), |
| 212 | ('http.server', 'SimpleHTTPRequestHandler'): |
| 213 | ('SimpleHTTPServer', 'SimpleHTTPRequestHandler'), |
| 214 | ('http.server', 'CGIHTTPRequestHandler'): |
| 215 | ('CGIHTTPServer', 'CGIHTTPRequestHandler'), |
| 216 | ('_socket', 'socket'): ('socket', '_socketobject'), |
| 217 | }) |
| 218 | |
| 219 | PYTHON3_OSERROR_EXCEPTIONS = ( |
| 220 | 'BrokenPipeError', |
| 221 | 'ChildProcessError', |
| 222 | 'ConnectionAbortedError', |
| 223 | 'ConnectionError', |
| 224 | 'ConnectionRefusedError', |
| 225 | 'ConnectionResetError', |
| 226 | 'FileExistsError', |
| 227 | 'FileNotFoundError', |
| 228 | 'InterruptedError', |
| 229 | 'IsADirectoryError', |
| 230 | 'NotADirectoryError', |
| 231 | 'PermissionError', |
| 232 | 'ProcessLookupError', |
| 233 | 'TimeoutError', |
| 234 | ) |
| 235 | |
| 236 | for excname in PYTHON3_OSERROR_EXCEPTIONS: |
| 237 | REVERSE_NAME_MAPPING[('builtins', excname)] = ('exceptions', 'OSError') |