Skip to content

Commit

Permalink
Fix renamed stdlib modules
Browse files Browse the repository at this point in the history
For #34
  • Loading branch information
brettcannon committed Sep 19, 2014
1 parent 638cba8 commit 39816c9
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
1 change: 1 addition & 0 deletions libmodernize/fixes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
62 changes: 62 additions & 0 deletions libmodernize/fixes/fix_imports_six.py
Original file line number Diff line number Diff line change
@@ -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',
}
47 changes: 47 additions & 0 deletions tests/test_fix_imports_six.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 39816c9

Please sign in to comment.