Skip to content

Commit 4e95c89

Browse files
author
martin.v.loewis
committed
Merged revisions 67183,67191,67371 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3 ........ r67183 | benjamin.peterson | 2008-11-11 04:51:33 +0100 (Di, 11 Nov 2008) | 1 line handle 'import x as y' in fix_imports; this still needs more work... ........ r67191 | benjamin.peterson | 2008-11-12 00:24:51 +0100 (Mi, 12 Nov 2008) | 1 line super() is good ........ r67371 | benjamin.peterson | 2008-11-24 23:02:00 +0100 (Mo, 24 Nov 2008) | 1 line don't blow up in the metaclass fixer when assignments in the class statement aren't simple ........ git-svn-id: http://svn.python.org/projects/python/branches/py3k@67372 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 6a3c583 commit 4e95c89

3 files changed

Lines changed: 31 additions & 9 deletions

File tree

Lib/lib2to3/fixes/fix_import.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# Local imports
1414
from .. import fixer_base
1515
from os.path import dirname, join, exists, pathsep
16-
from ..fixer_util import FromImport
16+
from ..fixer_util import FromImport, syms
1717

1818
class FixImport(fixer_base.BaseFix):
1919

@@ -26,11 +26,14 @@ class FixImport(fixer_base.BaseFix):
2626
def transform(self, node, results):
2727
imp = results['imp']
2828

29+
mod_name = str(imp.children[0] if imp.type == syms.dotted_as_name \
30+
else imp)
31+
2932
if str(imp).startswith('.'):
3033
# Already a new-style import
3134
return
3235

33-
if not probably_a_local_import(str(imp), self.filename):
36+
if not probably_a_local_import(str(mod_name), self.filename):
3437
# I guess this is a global import -- skip it!
3538
return
3639

Lib/lib2to3/fixes/fix_metaclass.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,11 @@ def find_metas(cls_node):
110110
if simple_node.type == syms.simple_stmt and simple_node.children:
111111
expr_node = simple_node.children[0]
112112
if expr_node.type == syms.expr_stmt and expr_node.children:
113-
leaf_node = expr_node.children[0]
114-
if leaf_node.value == '__metaclass__':
113+
# Check if the expr_node is a simple assignment.
114+
left_node = expr_node.children[0]
115+
if isinstance(left_node, Leaf) and \
116+
left_node.value == '__metaclass__':
117+
# We found a assignment to __metaclass__.
115118
fixup_simple_stmt(node, i, simple_node)
116119
remove_trailing_newline(simple_node)
117120
yield (node, i, simple_node)

Lib/lib2to3/tests/test_fixers.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,7 +2622,7 @@ class Test_map(FixerTestCase):
26222622

26232623
def check(self, b, a):
26242624
self.unchanged("from future_builtins import map; " + b, a)
2625-
FixerTestCase.check(self, b, a)
2625+
super(Test_map, self).check(b, a)
26262626

26272627
def test_prefix_preservation(self):
26282628
b = """x = map( f, 'abc' )"""
@@ -2729,7 +2729,7 @@ class Test_zip(FixerTestCase):
27292729

27302730
def check(self, b, a):
27312731
self.unchanged("from future_builtins import zip; " + b, a)
2732-
FixerTestCase.check(self, b, a)
2732+
super(Test_zip, self).check(b, a)
27332733

27342734
def test_zip_basic(self):
27352735
b = """x = zip(a, b, c)"""
@@ -3274,7 +3274,7 @@ class Test_import(FixerTestCase):
32743274
fixer = "import"
32753275

32763276
def setUp(self):
3277-
FixerTestCase.setUp(self)
3277+
super(Test_import, self).setUp()
32783278
# Need to replace fix_import's exists method
32793279
# so we can check that it's doing the right thing
32803280
self.files_checked = []
@@ -3293,9 +3293,9 @@ def tearDown(self):
32933293

32943294
def check_both(self, b, a):
32953295
self.always_exists = True
3296-
FixerTestCase.check(self, b, a)
3296+
super(Test_import, self).check(b, a)
32973297
self.always_exists = False
3298-
FixerTestCase.unchanged(self, b)
3298+
super(Test_import, self).unchanged(b)
32993299

33003300
def test_files_checked(self):
33013301
def p(path):
@@ -3372,6 +3372,11 @@ def test_import(self):
33723372
a = "from . import foo, bar"
33733373
self.check_both(b, a)
33743374

3375+
def test_import_as(self):
3376+
b = "import foo as x"
3377+
a = "from . import foo as x"
3378+
self.check_both(b, a)
3379+
33753380
def test_dotted_import(self):
33763381
b = "import foo.bar"
33773382
a = "from . import foo.bar"
@@ -3766,6 +3771,17 @@ class X(expression(2 + 4), x**4, metaclass=Meta):
37663771
"""
37673772
self.check(b, a)
37683773

3774+
b = """
3775+
class X:
3776+
__metaclass__ = Meta
3777+
save.py = 23
3778+
"""
3779+
a = """
3780+
class X(metaclass=Meta):
3781+
save.py = 23
3782+
"""
3783+
self.check(b, a)
3784+
37693785

37703786
class Test_getcwdu(FixerTestCase):
37713787

0 commit comments

Comments
 (0)