diff --git a/Lib/lib2to3/fixes/fix_import.py b/Lib/lib2to3/fixes/fix_import.py index 29b00318fec..69515468967 100644 --- a/Lib/lib2to3/fixes/fix_import.py +++ b/Lib/lib2to3/fixes/fix_import.py @@ -18,7 +18,7 @@ from ..fixer_util import FromImport class FixImport(fixer_base.BaseFix): PATTERN = """ - import_from< type='from' imp=any 'import' any > + import_from< type='from' imp=any 'import' ['('] any [')'] > | import_name< type='import' imp=any > """ diff --git a/Lib/lib2to3/fixes/fix_imports.py b/Lib/lib2to3/fixes/fix_imports.py index e7e7a75bbec..38e868b777d 100644 --- a/Lib/lib2to3/fixes/fix_imports.py +++ b/Lib/lib2to3/fixes/fix_imports.py @@ -66,9 +66,9 @@ def build_pattern(mapping=MAPPING): yield """import_name< 'import' ((%s) | dotted_as_names< any* (%s) any* >) > """ % (mod_list, mod_list) - yield """import_from< 'from' (%s) 'import' + yield """import_from< 'from' (%s) 'import' ['('] ( any | import_as_name< any 'as' any > | - import_as_names< any* >) > + import_as_names< any* >) [')'] > """ % mod_name_list yield """import_name< 'import' dotted_as_name< (%s) 'as' any > > diff --git a/Lib/lib2to3/fixes/fix_metaclass.py b/Lib/lib2to3/fixes/fix_metaclass.py index 7479024b3d2..c2f4b7f07ff 100644 --- a/Lib/lib2to3/fixes/fix_metaclass.py +++ b/Lib/lib2to3/fixes/fix_metaclass.py @@ -35,8 +35,9 @@ def has_metaclass(parent): elif node.type == syms.simple_stmt and node.children: expr_node = node.children[0] if expr_node.type == syms.expr_stmt and expr_node.children: - leaf_node = expr_node.children[0] - if leaf_node.value == '__metaclass__': + left_side = expr_node.children[0] + if isinstance(left_side, Leaf) and \ + left_side.value == '__metaclass__': return True return False @@ -165,12 +166,10 @@ class FixMetaclass(fixer_base.BaseFix): if node.children[3].type == syms.arglist: arglist = node.children[3] # Node(classdef, ['class', 'name', '(', 'Parent', ')', ':', suite]) - elif isinstance(node.children[3], Leaf): + else: parent = node.children[3].clone() arglist = Node(syms.arglist, [parent]) node.set_child(3, arglist) - else: - raise ValueError("Unexpected class inheritance arglist") elif len(node.children) == 6: # Node(classdef, ['class', 'name', '(', ')', ':', suite]) # 0 1 2 3 4 5 diff --git a/Lib/lib2to3/refactor.py b/Lib/lib2to3/refactor.py index 8f7fe9cd61e..4fe18bad416 100755 --- a/Lib/lib2to3/refactor.py +++ b/Lib/lib2to3/refactor.py @@ -363,10 +363,9 @@ class RefactoringTool(object): self.log_error("Can't create %s: %s", filename, err) return try: - try: - f.write(new_text) - except os.error, err: - self.log_error("Can't write %s: %s", filename, err) + f.write(new_text) + except os.error, err: + self.log_error("Can't write %s: %s", filename, err) finally: f.close() self.log_debug("Wrote changes to %s", filename) diff --git a/Lib/lib2to3/tests/test_fixers.py b/Lib/lib2to3/tests/test_fixers.py index 76243bf60dc..1de7e1ba741 100755 --- a/Lib/lib2to3/tests/test_fixers.py +++ b/Lib/lib2to3/tests/test_fixers.py @@ -1450,6 +1450,10 @@ class Test_imports(FixerTestCase): a = "from %s import foo, bar" % new self.check(b, a) + b = "from %s import (yes, no)" % old + a = "from %s import (yes, no)" % new + self.check(b, a) + def test_import_module_as(self): for old, new in self.modules.items(): b = "import %s as foo_bar" % old @@ -3345,6 +3349,10 @@ class Test_import(FixerTestCase): a = "from .foo import bar" self.check_both(b, a) + b = "from foo import (bar, baz)" + a = "from .foo import (bar, baz)" + self.check_both(b, a) + def test_dotted_from(self): b = "from green.eggs import ham" a = "from .green.eggs import ham" @@ -3624,6 +3632,12 @@ class Test_metaclass(FixerTestCase): """ self.unchanged(s) + s = """ + class X: + a[23] = 74 + """ + self.unchanged(s) + def test_comments(self): b = """ class X: @@ -3732,6 +3746,26 @@ class Test_metaclass(FixerTestCase): a = """class m(a, arg=23, metaclass=Meta): pass""" self.check(b, a) + b = """ + class X(expression(2 + 4)): + __metaclass__ = Meta + """ + a = """ + class X(expression(2 + 4), metaclass=Meta): + pass + """ + self.check(b, a) + + b = """ + class X(expression(2 + 4), x**4): + __metaclass__ = Meta + """ + a = """ + class X(expression(2 + 4), x**4, metaclass=Meta): + pass + """ + self.check(b, a) + class Test_getcwdu(FixerTestCase):