From 39816c9a2e5faf885de7dcc29e7a95226566a4cd Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Fri, 19 Sep 2014 16:04:18 -0400 Subject: [PATCH] Fix renamed stdlib modules For #34 --- libmodernize/fixes/__init__.py | 1 + libmodernize/fixes/fix_imports_six.py | 62 +++++++++++++++++++++++++++ tests/test_fix_imports_six.py | 47 ++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 libmodernize/fixes/fix_imports_six.py create mode 100644 tests/test_fix_imports_six.py diff --git a/libmodernize/fixes/__init__.py b/libmodernize/fixes/__init__.py index 287a9b9..7cec534 100644 --- a/libmodernize/fixes/__init__.py +++ b/libmodernize/fixes/__init__.py @@ -31,6 +31,7 @@ 'libmodernize.fixes.fix_basestring', 'libmodernize.fixes.fix_dict_six', 'libmodernize.fixes.fix_filter', + 'libermodernize.fixes.fix_imports_six', 'libmodernize.fixes.fix_input_six', 'libmodernize.fixes.fix_map', 'libmodernize.fixes.fix_metaclass', diff --git a/libmodernize/fixes/fix_imports_six.py b/libmodernize/fixes/fix_imports_six.py new file mode 100644 index 0000000..138c44b --- /dev/null +++ b/libmodernize/fixes/fix_imports_six.py @@ -0,0 +1,62 @@ +from lib2to3.fixes import fix_imports + + +class FixImportsSix(fix_imports.FixImports): + + mapping = { + '__builtin__': 'six.moves.builtins', + '_winreg': 'six.moves.winreg', + 'BaseHTTPServer': 'six.moves.BaseHTTPServer', + 'CGIHTTPServer': 'six.moves.CGIHTTPServer', + 'ConfigParser': 'six.moves.configparser', + 'copy_reg': 'six.moves.copyreg', + 'Cookie': 'six.moves.http_cookies', + 'cookielib': 'six.moves.http_cookiejar', + 'cPickle': 'six.moves.cPickle', + 'Dialog': 'six.moves.tkinter_dialog', + 'dummy_thread': 'six.moves._dummy_thread', + # cStringIO.StringIO() + # email.MIMEBase + # email.MIMEMultipart + # email.MIMENonMultipart + # email.MIMEText + 'FileDialog': 'six.moves.tkinter_filedialog', + 'gdbm': 'six.moves.dbm_gnu', + 'htmlentitydefs': 'six.moves.html_entities', + 'HTMLParser': 'six.moves.html_parser', + 'httplib': 'six.moves.http_client', + # intern() + # itertools.ifilter() + # itertools.ifilterfalse() + # itertools.imap() + # itertools.izip() + # itertools.zip_longest() + # pipes.quote + 'Queue': 'six.moves.queue', + # reduce() + # reload() + 'repr': 'six.moves.reprlib', + 'robotparser': 'six.moves.urllib_robotparser', + 'ScrolledText': 'six.moves.tkinter_scrolledtext', + 'SimpleDialog': 'six.moves.tkinter_simpledialog', + 'SimpleHTTPServer': 'six.moves.SimpleHTTPServer', + 'SimpleXMLRPCServer': 'six.moves.xmlrpc_server', + 'SocketServer': 'six.moves.socketserver', + 'thread': 'six.moves._thread', + 'Tix': 'six.moves.tkinter_tix', + 'tkColorChooser': 'six.moves.tkinter_colorchooser', + 'tkCommonDialog': 'six.moves.tkinter_commondialog', + 'Tkconstants': 'six.moves.tkinter_constants', + 'Tkdnd': 'six.moves.tkinter_dnd', + 'tkFileDialog': 'six.moves.tkinter_filedialog', + 'tkFont': 'six.moves.tkinter_font', + 'Tkinter': 'six.moves.tkinter', + 'tkMessageBox': 'six.moves.tkinter_messagebox', + 'tkSimpleDialog': 'six.moves.tkinter_tksimpledialog', + 'ttk': 'six.moves.tkinter_ttk', + # urllib + # UserDict.UserDict + # UserList.UserList + # UserString.UserString + 'xmlrpclib': 'six.moves.xmlrpc_client', + } diff --git a/tests/test_fix_imports_six.py b/tests/test_fix_imports_six.py new file mode 100644 index 0000000..3f3eb35 --- /dev/null +++ b/tests/test_fix_imports_six.py @@ -0,0 +1,47 @@ +from __future__ import absolute_import + +import sys + +try: + from six.moves import tkinter +except ImportError: + tkinter = None + +from libmodernize.fixes import fix_imports_six + +from utils import check_on_input + + +MOVED_MODULE = ("""\ +import ConfigParser +ConfigParser.ConfigParser() +""", """\ +import six.moves.configparser +six.moves.configparser.ConfigParser() +""") + +MOVED_MODULE_FROMLIST = ("""\ +from ConfigParser import ConfigParser +ConfigParser() +""", """\ +from six.moves.configparser import ConfigParser +ConfigParser() +""") + + +def test_moved_module(): + check_on_input(*MOVED_MODULE) + +def test_moved_module_fromlist(): + check_on_input(*MOVED_MODULE_FROMLIST) + +def test_validate_mapping(): + for py2_name, six_name in fix_imports_six.FixImportsSix.mapping.items(): + try: + __import__(py2_name) + __import__(six_name) + except ImportError as exc: + if 'tkinter' in six_name and tkinter is not None: + raise + elif 'winreg' in six_name and sys.platform.startswith('win'): + raise