Skip to content

Commit ada7be6

Browse files
committed
Merge branch 'master' into simplification
2 parents 11d3d01 + ab4b7a8 commit ada7be6

File tree

6 files changed

+123
-11
lines changed

6 files changed

+123
-11
lines changed

libmodernize/fixes/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
'libmodernize.fixes.fix_basestring',
3131
'libmodernize.fixes.fix_dict_six',
3232
'libmodernize.fixes.fix_filter',
33+
'libmodernize.fixes.fix_imports_six',
3334
'libmodernize.fixes.fix_input_six',
3435
'libmodernize.fixes.fix_map',
3536
'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+
}

libmodernize/fixes/fix_open.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@
44
import libmodernize
55

66

7-
class FixOpen(fixer_base.ConditionalFix):
7+
class FixOpen(fixer_base.BaseFix):
88

99
BM_compatible = True
10-
order = "pre"
11-
skip_on = "io.open"
12-
10+
# Fixers don't directly stack, so make sure the 'file' case is covered.
1311
PATTERN = """
14-
power< 'open' trailer< '(' any+ ')' > >
12+
power< ('open' | 'file') trailer< '(' any+ ')' > >
1513
"""
1614

1715
def transform(self, node, results):
18-
if self.should_skip(node):
19-
return
2016
libmodernize.touch_import(u'io', u'open', node)

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

tests/test_fix_open.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
OPEN = ("""\
7-
open('some/path')
7+
{0}('some/path')
88
""", """\
99
from __future__ import absolute_import
1010
from io import open
@@ -13,7 +13,13 @@
1313

1414

1515
def test_open():
16-
check_on_input(*OPEN, extra_flags=['-f', 'libmodernize.fixes.fix_open'])
16+
check_on_input(OPEN[0].format('open'), OPEN[1],
17+
extra_flags=['-f', 'libmodernize.fixes.fix_open'])
1718

1819
def test_open_optional():
19-
check_on_input(OPEN[0], OPEN[0])
20+
check_on_input(OPEN[0].format('open'), OPEN[0].format('open'))
21+
22+
def test_file():
23+
flags = ['-f', 'libmodernize.fixes.fix_open',
24+
'-f', 'libmodernize.fixes.fix_file']
25+
check_on_input(OPEN[0].format('file'), OPEN[1], extra_flags=flags)

tests/test_fixes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def check_existence(prefix, module_names):
1515
dotted_prefix = prefix + '.'
1616
for module_name in module_names:
1717
if not module_name.startswith(dotted_prefix):
18-
msg = '{0!r} does not start with {1!r}'.format(module_name, lib2to3_prefix)
18+
msg = '{0!r} does not start with {1!r}'.format(module_name, prefix)
1919
raise AssertionError(msg)
2020
try:
2121
__import__(module_name)

0 commit comments

Comments
 (0)