Skip to content

Commit 39816c9

Browse files
committed
Fix renamed stdlib modules
For #34
1 parent 638cba8 commit 39816c9

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

libmodernize/fixes/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
'libmodernize.fixes.fix_basestring',
3232
'libmodernize.fixes.fix_dict_six',
3333
'libmodernize.fixes.fix_filter',
34+
'libermodernize.fixes.fix_imports_six',
3435
'libmodernize.fixes.fix_input_six',
3536
'libmodernize.fixes.fix_map',
3637
'libmodernize.fixes.fix_metaclass',

libmodernize/fixes/fix_imports_six.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from lib2to3.fixes import fix_imports
2+
3+
4+
class FixImportsSix(fix_imports.FixImports):
5+
6+
mapping = {
7+
'__builtin__': 'six.moves.builtins',
8+
'_winreg': 'six.moves.winreg',
9+
'BaseHTTPServer': 'six.moves.BaseHTTPServer',
10+
'CGIHTTPServer': 'six.moves.CGIHTTPServer',
11+
'ConfigParser': 'six.moves.configparser',
12+
'copy_reg': 'six.moves.copyreg',
13+
'Cookie': 'six.moves.http_cookies',
14+
'cookielib': 'six.moves.http_cookiejar',
15+
'cPickle': 'six.moves.cPickle',
16+
'Dialog': 'six.moves.tkinter_dialog',
17+
'dummy_thread': 'six.moves._dummy_thread',
18+
# cStringIO.StringIO()
19+
# email.MIMEBase
20+
# email.MIMEMultipart
21+
# email.MIMENonMultipart
22+
# email.MIMEText
23+
'FileDialog': 'six.moves.tkinter_filedialog',
24+
'gdbm': 'six.moves.dbm_gnu',
25+
'htmlentitydefs': 'six.moves.html_entities',
26+
'HTMLParser': 'six.moves.html_parser',
27+
'httplib': 'six.moves.http_client',
28+
# intern()
29+
# itertools.ifilter()
30+
# itertools.ifilterfalse()
31+
# itertools.imap()
32+
# itertools.izip()
33+
# itertools.zip_longest()
34+
# pipes.quote
35+
'Queue': 'six.moves.queue',
36+
# reduce()
37+
# reload()
38+
'repr': 'six.moves.reprlib',
39+
'robotparser': 'six.moves.urllib_robotparser',
40+
'ScrolledText': 'six.moves.tkinter_scrolledtext',
41+
'SimpleDialog': 'six.moves.tkinter_simpledialog',
42+
'SimpleHTTPServer': 'six.moves.SimpleHTTPServer',
43+
'SimpleXMLRPCServer': 'six.moves.xmlrpc_server',
44+
'SocketServer': 'six.moves.socketserver',
45+
'thread': 'six.moves._thread',
46+
'Tix': 'six.moves.tkinter_tix',
47+
'tkColorChooser': 'six.moves.tkinter_colorchooser',
48+
'tkCommonDialog': 'six.moves.tkinter_commondialog',
49+
'Tkconstants': 'six.moves.tkinter_constants',
50+
'Tkdnd': 'six.moves.tkinter_dnd',
51+
'tkFileDialog': 'six.moves.tkinter_filedialog',
52+
'tkFont': 'six.moves.tkinter_font',
53+
'Tkinter': 'six.moves.tkinter',
54+
'tkMessageBox': 'six.moves.tkinter_messagebox',
55+
'tkSimpleDialog': 'six.moves.tkinter_tksimpledialog',
56+
'ttk': 'six.moves.tkinter_ttk',
57+
# urllib
58+
# UserDict.UserDict
59+
# UserList.UserList
60+
# UserString.UserString
61+
'xmlrpclib': 'six.moves.xmlrpc_client',
62+
}

tests/test_fix_imports_six.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from __future__ import absolute_import
2+
3+
import sys
4+
5+
try:
6+
from six.moves import tkinter
7+
except ImportError:
8+
tkinter = None
9+
10+
from libmodernize.fixes import fix_imports_six
11+
12+
from utils import check_on_input
13+
14+
15+
MOVED_MODULE = ("""\
16+
import ConfigParser
17+
ConfigParser.ConfigParser()
18+
""", """\
19+
import six.moves.configparser
20+
six.moves.configparser.ConfigParser()
21+
""")
22+
23+
MOVED_MODULE_FROMLIST = ("""\
24+
from ConfigParser import ConfigParser
25+
ConfigParser()
26+
""", """\
27+
from six.moves.configparser import ConfigParser
28+
ConfigParser()
29+
""")
30+
31+
32+
def test_moved_module():
33+
check_on_input(*MOVED_MODULE)
34+
35+
def test_moved_module_fromlist():
36+
check_on_input(*MOVED_MODULE_FROMLIST)
37+
38+
def test_validate_mapping():
39+
for py2_name, six_name in fix_imports_six.FixImportsSix.mapping.items():
40+
try:
41+
__import__(py2_name)
42+
__import__(six_name)
43+
except ImportError as exc:
44+
if 'tkinter' in six_name and tkinter is not None:
45+
raise
46+
elif 'winreg' in six_name and sys.platform.startswith('win'):
47+
raise

0 commit comments

Comments
 (0)