Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ba55818

Browse files
committed
Merged revisions 67180 via svnmerge from
svn+ssh://[email protected]/python/trunk ................ r67180 | benjamin.peterson | 2008-11-10 16:11:12 -0600 (Mon, 10 Nov 2008) | 29 lines Merged revisions 66985,67170,67173,67177-67179 via svnmerge from svn+ssh://[email protected]/sandbox/trunk/2to3/lib2to3 ........ r66985 | benjamin.peterson | 2008-10-20 16:43:46 -0500 (Mon, 20 Oct 2008) | 1 line no need to use nested try, except, finally ........ r67170 | benjamin.peterson | 2008-11-08 12:28:31 -0600 (Sat, 08 Nov 2008) | 1 line fix #4271: fix_imports didn't recognize imports with parenthesis (ie from x import (a, b)) ........ r67173 | benjamin.peterson | 2008-11-08 17:42:08 -0600 (Sat, 08 Nov 2008) | 1 line consolidate test ........ r67177 | benjamin.peterson | 2008-11-09 21:52:52 -0600 (Sun, 09 Nov 2008) | 1 line let the metclass fixer handle complex assignments in the class body gracefully ........ r67178 | benjamin.peterson | 2008-11-10 15:26:43 -0600 (Mon, 10 Nov 2008) | 1 line the metaclass fixers shouldn't die when bases are not a simple name ........ r67179 | benjamin.peterson | 2008-11-10 15:29:58 -0600 (Mon, 10 Nov 2008) | 1 line allow the fix_import pattern to catch from imports with parenthesis ........ ................
1 parent 065ba70 commit ba55818

5 files changed

Lines changed: 44 additions & 12 deletions

File tree

Lib/lib2to3/fixes/fix_import.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
class FixImport(fixer_base.BaseFix):
1919

2020
PATTERN = """
21-
import_from< type='from' imp=any 'import' any >
21+
import_from< type='from' imp=any 'import' ['('] any [')'] >
2222
|
2323
import_name< type='import' imp=any >
2424
"""

Lib/lib2to3/fixes/fix_imports.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ def build_pattern(mapping=MAPPING):
6666
yield """import_name< 'import' ((%s)
6767
| dotted_as_names< any* (%s) any* >) >
6868
""" % (mod_list, mod_list)
69-
yield """import_from< 'from' (%s) 'import'
69+
yield """import_from< 'from' (%s) 'import' ['(']
7070
( any | import_as_name< any 'as' any > |
71-
import_as_names< any* >) >
71+
import_as_names< any* >) [')'] >
7272
""" % mod_name_list
7373
yield """import_name< 'import'
7474
dotted_as_name< (%s) 'as' any > >

Lib/lib2to3/fixes/fix_metaclass.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ def has_metaclass(parent):
3535
elif node.type == syms.simple_stmt and node.children:
3636
expr_node = node.children[0]
3737
if expr_node.type == syms.expr_stmt and expr_node.children:
38-
leaf_node = expr_node.children[0]
39-
if leaf_node.value == '__metaclass__':
38+
left_side = expr_node.children[0]
39+
if isinstance(left_side, Leaf) and \
40+
left_side.value == '__metaclass__':
4041
return True
4142
return False
4243

@@ -165,12 +166,10 @@ def transform(self, node, results):
165166
if node.children[3].type == syms.arglist:
166167
arglist = node.children[3]
167168
# Node(classdef, ['class', 'name', '(', 'Parent', ')', ':', suite])
168-
elif isinstance(node.children[3], Leaf):
169+
else:
169170
parent = node.children[3].clone()
170171
arglist = Node(syms.arglist, [parent])
171172
node.set_child(3, arglist)
172-
else:
173-
raise ValueError("Unexpected class inheritance arglist")
174173
elif len(node.children) == 6:
175174
# Node(classdef, ['class', 'name', '(', ')', ':', suite])
176175
# 0 1 2 3 4 5

Lib/lib2to3/refactor.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,10 +363,9 @@ def write_file(self, new_text, filename, old_text):
363363
self.log_error("Can't create %s: %s", filename, err)
364364
return
365365
try:
366-
try:
367-
f.write(new_text)
368-
except os.error as err:
369-
self.log_error("Can't write %s: %s", filename, err)
366+
f.write(new_text)
367+
except os.error as err:
368+
self.log_error("Can't write %s: %s", filename, err)
370369
finally:
371370
f.close()
372371
self.log_debug("Wrote changes to %s", filename)

Lib/lib2to3/tests/test_fixers.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,6 +1450,10 @@ def test_import_from(self):
14501450
a = "from %s import foo, bar" % new
14511451
self.check(b, a)
14521452

1453+
b = "from %s import (yes, no)" % old
1454+
a = "from %s import (yes, no)" % new
1455+
self.check(b, a)
1456+
14531457
def test_import_module_as(self):
14541458
for old, new in self.modules.items():
14551459
b = "import %s as foo_bar" % old
@@ -3345,6 +3349,10 @@ def test_from(self):
33453349
a = "from .foo import bar"
33463350
self.check_both(b, a)
33473351

3352+
b = "from foo import (bar, baz)"
3353+
a = "from .foo import (bar, baz)"
3354+
self.check_both(b, a)
3355+
33483356
def test_dotted_from(self):
33493357
b = "from green.eggs import ham"
33503358
a = "from .green.eggs import ham"
@@ -3624,6 +3632,12 @@ def __metaclass__(self): pass
36243632
"""
36253633
self.unchanged(s)
36263634

3635+
s = """
3636+
class X:
3637+
a[23] = 74
3638+
"""
3639+
self.unchanged(s)
3640+
36273641
def test_comments(self):
36283642
b = """
36293643
class X:
@@ -3732,6 +3746,26 @@ class X(clsA, clsB, metaclass=Meta):
37323746
a = """class m(a, arg=23, metaclass=Meta): pass"""
37333747
self.check(b, a)
37343748

3749+
b = """
3750+
class X(expression(2 + 4)):
3751+
__metaclass__ = Meta
3752+
"""
3753+
a = """
3754+
class X(expression(2 + 4), metaclass=Meta):
3755+
pass
3756+
"""
3757+
self.check(b, a)
3758+
3759+
b = """
3760+
class X(expression(2 + 4), x**4):
3761+
__metaclass__ = Meta
3762+
"""
3763+
a = """
3764+
class X(expression(2 + 4), x**4, metaclass=Meta):
3765+
pass
3766+
"""
3767+
self.check(b, a)
3768+
37353769

37363770
class Test_getcwdu(FixerTestCase):
37373771

0 commit comments

Comments
 (0)