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

Skip to content

Commit cf60382

Browse files
committed
Merged revisions 66117 via svnmerge from
svn+ssh://[email protected]/python/trunk ................ r66117 | benjamin.peterson | 2008-09-01 12:17:22 -0500 (Mon, 01 Sep 2008) | 25 lines Merged revisions 65887,65889,65967-65968,65981 via svnmerge from svn+ssh://[email protected]/sandbox/trunk/2to3/lib2to3 ........ r65887 | benjamin.peterson | 2008-08-19 17:45:04 -0500 (Tue, 19 Aug 2008) | 1 line allow the raw_input fixer to handle calls after the raw_input (ie. raw_input().split()) ........ r65889 | benjamin.peterson | 2008-08-19 18:11:03 -0500 (Tue, 19 Aug 2008) | 1 line no need for 2.4 compatibility now ........ r65967 | benjamin.peterson | 2008-08-21 18:43:37 -0500 (Thu, 21 Aug 2008) | 1 line allow a Call to have no arguments ........ r65968 | benjamin.peterson | 2008-08-21 18:45:13 -0500 (Thu, 21 Aug 2008) | 1 line add a fixer for sys.exc_info etc by Jeff Balogh #2357 ........ r65981 | benjamin.peterson | 2008-08-22 15:41:30 -0500 (Fri, 22 Aug 2008) | 1 line add a fixer to add parenthese for list and gen comps #2367 ........ ................
1 parent 2cb598f commit cf60382

6 files changed

Lines changed: 185 additions & 13 deletions

File tree

Lib/lib2to3/fixer_util.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ def Dot():
5151

5252
def ArgList(args, lparen=LParen(), rparen=RParen()):
5353
"""A parenthesised argument list, used by Call()"""
54-
return Node(syms.trailer,
55-
[lparen.clone(),
56-
Node(syms.arglist, args),
57-
rparen.clone()])
54+
node = Node(syms.trailer, [lparen.clone(), rparen.clone()])
55+
if args:
56+
node.insert_child(1, Node(syms.arglist, args))
57+
return node
5858

59-
def Call(func_name, args, prefix=None):
59+
def Call(func_name, args=None, prefix=None):
6060
"""A function call"""
6161
node = Node(syms.power, [func_name, ArgList(args)])
6262
if prefix is not None:

Lib/lib2to3/fixes/fix_paren.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""Fixer that addes parentheses where they are required
2+
3+
This converts ``[x for x in 1, 2]`` to ``[x for x in (1, 2)]``."""
4+
5+
# By Taek Joo Kim and Benjamin Peterson
6+
7+
# Local imports
8+
from .. import fixer_base
9+
from ..fixer_util import LParen, RParen
10+
11+
# XXX This doesn't support nested for loops like [x for x in 1, 2 for x in 1, 2]
12+
class FixParen(fixer_base.BaseFix):
13+
PATTERN = """
14+
atom< ('[' | '(')
15+
(listmaker< any
16+
comp_for<
17+
'for' NAME 'in'
18+
target=testlist_safe< any (',' any)+ [',']
19+
>
20+
[any]
21+
>
22+
>
23+
|
24+
testlist_gexp< any
25+
comp_for<
26+
'for' NAME 'in'
27+
target=testlist_safe< any (',' any)+ [',']
28+
>
29+
[any]
30+
>
31+
>)
32+
(']' | ')') >
33+
"""
34+
35+
def transform(self, node, results):
36+
target = results["target"]
37+
38+
lparen = LParen()
39+
lparen.set_prefix(target.get_prefix())
40+
target.set_prefix("") # Make it hug the parentheses
41+
target.insert_child(0, lparen)
42+
target.append_child(RParen())

Lib/lib2to3/fixes/fix_raw_input.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class FixRawInput(fixer_base.BaseFix):
99

1010
PATTERN = """
11-
power< name='raw_input' trailer< '(' [any] ')' > >
11+
power< name='raw_input' trailer< '(' [any] ')' > any* >
1212
"""
1313

1414
def transform(self, node, results):

Lib/lib2to3/fixes/fix_sys_exc.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""Fixer for sys.exc_{type, value, traceback}
2+
3+
sys.exc_type -> sys.exc_info()[0]
4+
sys.exc_value -> sys.exc_info()[1]
5+
sys.exc_traceback -> sys.exc_info()[2]
6+
"""
7+
8+
# By Jeff Balogh and Benjamin Peterson
9+
10+
# Local imports
11+
from .. import fixer_base
12+
from ..fixer_util import Attr, Call, Name, Number, Subscript, Node, syms
13+
14+
class FixSysExc(fixer_base.BaseFix):
15+
# This order matches the ordering of sys.exc_info().
16+
exc_info = ["exc_type", "exc_value", "exc_traceback"]
17+
PATTERN = """
18+
power< 'sys' trailer< dot='.' attribute=(%s) > >
19+
""" % '|'.join("'%s'" % e for e in exc_info)
20+
21+
def transform(self, node, results):
22+
sys_attr = results["attribute"][0]
23+
index = Number(self.exc_info.index(sys_attr.value))
24+
25+
call = Call(Name("exc_info"), prefix=sys_attr.get_prefix())
26+
attr = Attr(Name("sys"), call)
27+
attr[1].children[0].set_prefix(results["dot"].get_prefix())
28+
attr.append(Subscript(index))
29+
return Node(syms.power, attr, prefix=node.get_prefix())

Lib/lib2to3/refactor.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,7 @@ def main(fixer_dir, args=None):
6969
return 2
7070

7171
# Set up logging handler
72-
if sys.version_info < (2, 4):
73-
hdlr = logging.StreamHandler()
74-
fmt = logging.Formatter('%(name)s: %(message)s')
75-
hdlr.setFormatter(fmt)
76-
logging.root.addHandler(hdlr)
77-
else:
78-
logging.basicConfig(format='%(name)s: %(message)s', level=logging.INFO)
72+
logging.basicConfig(format='%(name)s: %(message)s', level=logging.INFO)
7973

8074
# Initialize the refactoring tool
8175
rt = RefactoringTool(fixer_dir, options)

Lib/lib2to3/tests/test_fixers.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,21 @@ def test_4(self):
13401340
a = """x = input(foo(a) + 6)"""
13411341
self.check(b, a)
13421342

1343+
def test_5(self):
1344+
b = """x = raw_input(invite).split()"""
1345+
a = """x = input(invite).split()"""
1346+
self.check(b, a)
1347+
1348+
def test_6(self):
1349+
b = """x = raw_input(invite) . split ()"""
1350+
a = """x = input(invite) . split ()"""
1351+
self.check(b, a)
1352+
1353+
def test_8(self):
1354+
b = "x = int(raw_input())"
1355+
a = "x = int(input())"
1356+
self.check(b, a)
1357+
13431358
class Test_funcattrs(FixerTestCase):
13441359
fixer = "funcattrs"
13451360

@@ -3330,6 +3345,98 @@ def test_prefix(self):
33303345
"""
33313346
self.check_both(b, a)
33323347

3348+
class Test_sys_exc(FixerTestCase):
3349+
fixer = "sys_exc"
3350+
3351+
def test_0(self):
3352+
b = "sys.exc_type"
3353+
a = "sys.exc_info()[0]"
3354+
self.check(b, a)
3355+
3356+
def test_1(self):
3357+
b = "sys.exc_value"
3358+
a = "sys.exc_info()[1]"
3359+
self.check(b, a)
3360+
3361+
def test_2(self):
3362+
b = "sys.exc_traceback"
3363+
a = "sys.exc_info()[2]"
3364+
self.check(b, a)
3365+
3366+
def test_3(self):
3367+
b = "sys.exc_type # Foo"
3368+
a = "sys.exc_info()[0] # Foo"
3369+
self.check(b, a)
3370+
3371+
def test_4(self):
3372+
b = "sys. exc_type"
3373+
a = "sys. exc_info()[0]"
3374+
self.check(b, a)
3375+
3376+
def test_5(self):
3377+
b = "sys .exc_type"
3378+
a = "sys .exc_info()[0]"
3379+
self.check(b, a)
3380+
3381+
3382+
class Test_paren(FixerTestCase):
3383+
fixer = "paren"
3384+
3385+
def test_0(self):
3386+
b = """[i for i in 1, 2 ]"""
3387+
a = """[i for i in (1, 2) ]"""
3388+
self.check(b, a)
3389+
3390+
def test_1(self):
3391+
b = """[i for i in 1, 2, ]"""
3392+
a = """[i for i in (1, 2,) ]"""
3393+
self.check(b, a)
3394+
3395+
def test_2(self):
3396+
b = """[i for i in 1, 2 ]"""
3397+
a = """[i for i in (1, 2) ]"""
3398+
self.check(b, a)
3399+
3400+
def test_3(self):
3401+
b = """[i for i in 1, 2 if i]"""
3402+
a = """[i for i in (1, 2) if i]"""
3403+
self.check(b, a)
3404+
3405+
def test_4(self):
3406+
b = """[i for i in 1, 2 ]"""
3407+
a = """[i for i in (1, 2) ]"""
3408+
self.check(b, a)
3409+
3410+
def test_5(self):
3411+
b = """(i for i in 1, 2)"""
3412+
a = """(i for i in (1, 2))"""
3413+
self.check(b, a)
3414+
3415+
def test_6(self):
3416+
b = """(i for i in 1 ,2 if i)"""
3417+
a = """(i for i in (1 ,2) if i)"""
3418+
self.check(b, a)
3419+
3420+
def test_unchanged_0(self):
3421+
s = """[i for i in (1, 2)]"""
3422+
self.unchanged(s)
3423+
3424+
def test_unchanged_1(self):
3425+
s = """[i for i in foo()]"""
3426+
self.unchanged(s)
3427+
3428+
def test_unchanged_2(self):
3429+
s = """[i for i in (1, 2) if nothing]"""
3430+
self.unchanged(s)
3431+
3432+
def test_unchanged_3(self):
3433+
s = """(i for i in (1, 2))"""
3434+
self.unchanged(s)
3435+
3436+
def test_unchanged_4(self):
3437+
s = """[i for i in m]"""
3438+
self.unchanged(s)
3439+
33333440

33343441
if __name__ == "__main__":
33353442
import __main__

0 commit comments

Comments
 (0)