cpython: 8b2668e60aef Lib/_compat_pickle.py (original) (raw)

Minor code cleanup.

Raymond Hettinger python@rcn.com

date

Fri, 16 Mar 2012 11:48:12 -0700

parents

0805ea211122

children

7d3297f127ae

line wrap: on

line source

This module is used to map the old Python 2 names to the new names used in

Python 3 for the pickle module. This needed to make pickle streams

generated with Python 2 loadable by Python 3.

This is a copy of lib2to3.fixes.fix_imports.MAPPING. We cannot import

lib2to3 and use the mapping defined there, because lib2to3 uses pickle.

Thus, this could cause the module to be imported recursively.

IMPORT_MAPPING = { 'StringIO': 'io', 'cStringIO': 'io', 'cPickle': 'pickle', 'builtin' : 'builtins', 'copy_reg': 'copyreg', 'Queue': 'queue', 'SocketServer': 'socketserver', 'ConfigParser': 'configparser', 'repr': 'reprlib', 'FileDialog': 'tkinter.filedialog', 'tkFileDialog': 'tkinter.filedialog', 'SimpleDialog': 'tkinter.simpledialog', 'tkSimpleDialog': 'tkinter.simpledialog', 'tkColorChooser': 'tkinter.colorchooser', 'tkCommonDialog': 'tkinter.commondialog', 'Dialog': 'tkinter.dialog', 'Tkdnd': 'tkinter.dnd', 'tkFont': 'tkinter.font', 'tkMessageBox': 'tkinter.messagebox', 'ScrolledText': 'tkinter.scrolledtext', 'Tkconstants': 'tkinter.constants', 'Tix': 'tkinter.tix', 'ttk': 'tkinter.ttk', 'Tkinter': 'tkinter', 'markupbase': '_markupbase', '_winreg': 'winreg', 'thread': '_thread', 'dummy_thread': '_dummy_thread', 'dbhash': 'dbm.bsd', 'dumbdbm': 'dbm.dumb', 'dbm': 'dbm.ndbm', 'gdbm': 'dbm.gnu', 'xmlrpclib': 'xmlrpc.client', 'DocXMLRPCServer': 'xmlrpc.server', 'SimpleXMLRPCServer': 'xmlrpc.server', 'httplib': 'http.client', 'htmlentitydefs' : 'html.entities', 'HTMLParser' : 'html.parser', 'Cookie': 'http.cookies', 'cookielib': 'http.cookiejar', 'BaseHTTPServer': 'http.server', 'SimpleHTTPServer': 'http.server', 'CGIHTTPServer': 'http.server', 'test.test_support': 'test.support', 'commands': 'subprocess', 'UserString' : 'collections', 'UserList' : 'collections', 'urlparse' : 'urllib.parse', 'robotparser' : 'urllib.robotparser', 'whichdb': 'dbm', 'anydbm': 'dbm' }

This contains rename rules that are easy to handle. We ignore the more

complex stuff (e.g. mapping the names in the urllib and types modules).

These rules should be run before import names are fixed.

NAME_MAPPING = { ('builtin', 'xrange'): ('builtins', 'range'), ('builtin', 'reduce'): ('functools', 'reduce'), ('builtin', 'intern'): ('sys', 'intern'), ('builtin', 'unichr'): ('builtins', 'chr'), ('builtin', 'basestring'): ('builtins', 'str'), ('builtin', 'long'): ('builtins', 'int'), ('itertools', 'izip'): ('builtins', 'zip'), ('itertools', 'imap'): ('builtins', 'map'), ('itertools', 'ifilter'): ('builtins', 'filter'), ('itertools', 'ifilterfalse'): ('itertools', 'filterfalse'), }

Same, but for 3.x to 2.x

REVERSE_IMPORT_MAPPING = dict((v, k) for (k, v) in IMPORT_MAPPING.items()) REVERSE_NAME_MAPPING = dict((v, k) for (k, v) in NAME_MAPPING.items())