Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit93b4b47

Browse files
stuartebergbenjaminp
authored andcommitted
bpo-28837: Fix lib2to3 handling of map/zip/filter calls when followed with a 'trailer', e.g. zip()[x] (#24)
1 parent01fa9ae commit93b4b47

File tree

5 files changed

+110
-26
lines changed

5 files changed

+110
-26
lines changed

‎Lib/lib2to3/fixes/fix_filter.py‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515

1616
# Local imports
1717
from ..importfixer_base
18-
from ..fixer_utilimportName,Call,ListComp,in_special_context
18+
from ..pytreeimportNode
19+
from ..pygramimportpython_symbolsassyms
20+
from ..fixer_utilimportName,ArgList,ListComp,in_special_context
21+
1922

2023
classFixFilter(fixer_base.ConditionalFix):
2124
BM_compatible=True
@@ -34,16 +37,19 @@ class FixFilter(fixer_base.ConditionalFix):
3437
>
3538
')'
3639
>
40+
[extra_trailers=trailer*]
3741
>
3842
|
3943
power<
4044
'filter'
4145
trailer< '(' arglist< none='None' ',' seq=any > ')' >
46+
[extra_trailers=trailer*]
4247
>
4348
|
4449
power<
4550
'filter'
4651
args=trailer< '(' [any] ')' >
52+
[extra_trailers=trailer*]
4753
>
4854
"""
4955

@@ -53,23 +59,32 @@ def transform(self, node, results):
5359
ifself.should_skip(node):
5460
return
5561

62+
trailers= []
63+
if'extra_trailers'inresults:
64+
fortinresults['extra_trailers']:
65+
trailers.append(t.clone())
66+
5667
if"filter_lambda"inresults:
5768
new=ListComp(results.get("fp").clone(),
5869
results.get("fp").clone(),
5970
results.get("it").clone(),
6071
results.get("xp").clone())
72+
new=Node(syms.power, [new]+trailers,prefix="")
6173

6274
elif"none"inresults:
6375
new=ListComp(Name("_f"),
6476
Name("_f"),
6577
results["seq"].clone(),
6678
Name("_f"))
79+
new=Node(syms.power, [new]+trailers,prefix="")
6780

6881
else:
6982
ifin_special_context(node):
7083
returnNone
71-
new=node.clone()
84+
85+
args=results['args'].clone()
86+
new=Node(syms.power, [Name("filter"),args],prefix="")
87+
new=Node(syms.power, [Name("list"),ArgList([new])]+trailers)
7288
new.prefix=""
73-
new=Call(Name("list"), [new])
7489
new.prefix=node.prefix
7590
returnnew

‎Lib/lib2to3/fixes/fix_map.py‎

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
# Local imports
2323
from ..pgen2importtoken
2424
from ..importfixer_base
25-
from ..fixer_utilimportName,Call,ListComp,in_special_context
25+
from ..fixer_utilimportName,ArgList,Call,ListComp,in_special_context
2626
from ..pygramimportpython_symbolsassyms
27+
from ..pytreeimportNode
28+
2729

2830
classFixMap(fixer_base.ConditionalFix):
2931
BM_compatible=True
@@ -32,6 +34,7 @@ class FixMap(fixer_base.ConditionalFix):
3234
map_none=power<
3335
'map'
3436
trailer< '(' arglist< 'None' ',' arg=any [','] > ')' >
37+
[extra_trailers=trailer*]
3538
>
3639
|
3740
map_lambda=power<
@@ -47,10 +50,12 @@ class FixMap(fixer_base.ConditionalFix):
4750
>
4851
')'
4952
>
53+
[extra_trailers=trailer*]
5054
>
5155
|
5256
power<
53-
'map' trailer< '(' [arglist=any] ')' >
57+
'map' args=trailer< '(' [any] ')' >
58+
[extra_trailers=trailer*]
5459
>
5560
"""
5661

@@ -60,6 +65,11 @@ def transform(self, node, results):
6065
ifself.should_skip(node):
6166
return
6267

68+
trailers= []
69+
if'extra_trailers'inresults:
70+
fortinresults['extra_trailers']:
71+
trailers.append(t.clone())
72+
6373
ifnode.parent.type==syms.simple_stmt:
6474
self.warning(node,"You should use a for loop here")
6575
new=node.clone()
@@ -69,23 +79,32 @@ def transform(self, node, results):
6979
new=ListComp(results["xp"].clone(),
7080
results["fp"].clone(),
7181
results["it"].clone())
82+
new=Node(syms.power, [new]+trailers,prefix="")
83+
7284
else:
7385
if"map_none"inresults:
7486
new=results["arg"].clone()
87+
new.prefix=""
7588
else:
76-
if"arglist"inresults:
77-
args=results["arglist"]
78-
ifargs.type==syms.arglistand \
79-
args.children[0].type==token.NAMEand \
80-
args.children[0].value=="None":
89+
if"args"inresults:
90+
args=results["args"]
91+
ifargs.type==syms.trailerand \
92+
args.children[1].type==syms.arglistand \
93+
args.children[1].children[0].type==token.NAMEand \
94+
args.children[1].children[0].value=="None":
8195
self.warning(node,"cannot convert map(None, ...) "
8296
"with multiple arguments because map() "
8397
"now truncates to the shortest sequence")
8498
return
99+
100+
new=Node(syms.power, [Name("map"),args.clone()])
101+
new.prefix=""
102+
85103
ifin_special_context(node):
86104
returnNone
87-
new=node.clone()
105+
106+
new=Node(syms.power, [Name("list"),ArgList([new])]+trailers)
88107
new.prefix=""
89-
new=Call(Name("list"), [new])
108+
90109
new.prefix=node.prefix
91110
returnnew

‎Lib/lib2to3/fixes/fix_zip.py‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99

1010
# Local imports
1111
from ..importfixer_base
12-
from ..fixer_utilimportName,Call,in_special_context
12+
from ..pytreeimportNode
13+
from ..pygramimportpython_symbolsassyms
14+
from ..fixer_utilimportName,ArgList,in_special_context
15+
1316

1417
classFixZip(fixer_base.ConditionalFix):
1518

1619
BM_compatible=True
1720
PATTERN="""
18-
power< 'zip' args=trailer< '(' [any] ')' >
21+
power< 'zip' args=trailer< '(' [any] ')' > [trailers=trailer*]
1922
>
2023
"""
2124

@@ -28,8 +31,16 @@ def transform(self, node, results):
2831
ifin_special_context(node):
2932
returnNone
3033

31-
new=node.clone()
32-
new.prefix=""
33-
new=Call(Name("list"), [new])
34+
args=results['args'].clone()
35+
args.prefix=""
36+
37+
trailers= []
38+
if'trailers'inresults:
39+
trailers= [n.clone()forninresults['trailers']]
40+
fornintrailers:
41+
n.prefix=""
42+
43+
new=Node(syms.power, [Name("zip"),args],prefix="")
44+
new=Node(syms.power, [Name("list"),ArgList([new])]+trailers)
3445
new.prefix=node.prefix
3546
returnnew

‎Lib/lib2to3/tests/test_fixers.py‎

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2954,10 +2954,23 @@ def test_filter_basic(self):
29542954
a="""x = [x for x in range(10) if x%2 == 0]"""
29552955
self.check(b,a)
29562956

2957-
# XXX This (rare) case is not supported
2958-
## b = """x = filter(f, 'abc')[0]"""
2959-
## a = """x = list(filter(f, 'abc'))[0]"""
2960-
## self.check(b, a)
2957+
deftest_filter_trailers(self):
2958+
b="""x = filter(None, 'abc')[0]"""
2959+
a="""x = [_f for _f in 'abc' if _f][0]"""
2960+
self.check(b,a)
2961+
2962+
b="""x = len(filter(f, 'abc')[0])"""
2963+
a="""x = len(list(filter(f, 'abc'))[0])"""
2964+
self.check(b,a)
2965+
2966+
b="""x = filter(lambda x: x%2 == 0, range(10))[0]"""
2967+
a="""x = [x for x in range(10) if x%2 == 0][0]"""
2968+
self.check(b,a)
2969+
2970+
# Note the parens around x
2971+
b="""x = filter(lambda (x): x%2 == 0, range(10))[0]"""
2972+
a="""x = [x for x in range(10) if x%2 == 0][0]"""
2973+
self.check(b,a)
29612974

29622975
deftest_filter_nochange(self):
29632976
a="""b.join(filter(f, 'abc'))"""
@@ -3022,6 +3035,23 @@ def test_prefix_preservation(self):
30223035
a="""x = list(map( f, 'abc' ))"""
30233036
self.check(b,a)
30243037

3038+
deftest_map_trailers(self):
3039+
b="""x = map(f, 'abc')[0]"""
3040+
a="""x = list(map(f, 'abc'))[0]"""
3041+
self.check(b,a)
3042+
3043+
b="""x = map(None, l)[0]"""
3044+
a="""x = list(l)[0]"""
3045+
self.check(b,a)
3046+
3047+
b="""x = map(lambda x:x, l)[0]"""
3048+
a="""x = [x for x in l][0]"""
3049+
self.check(b,a)
3050+
3051+
b="""x = map(f, 'abc')[0][1]"""
3052+
a="""x = list(map(f, 'abc'))[0][1]"""
3053+
self.check(b,a)
3054+
30253055
deftest_trailing_comment(self):
30263056
b="""x = map(f, 'abc') # foo"""
30273057
a="""x = list(map(f, 'abc')) # foo"""
@@ -3066,11 +3096,6 @@ def test_map_basic(self):
30663096
"""
30673097
self.warns(b,a,"You should use a for loop here")
30683098

3069-
# XXX This (rare) case is not supported
3070-
## b = """x = map(f, 'abc')[0]"""
3071-
## a = """x = list(map(f, 'abc'))[0]"""
3072-
## self.check(b, a)
3073-
30743099
deftest_map_nochange(self):
30753100
a="""b.join(map(f, 'abc'))"""
30763101
self.unchanged(a)
@@ -3130,6 +3155,10 @@ def check(self, b, a):
31303155
super(Test_zip,self).check(b,a)
31313156

31323157
deftest_zip_basic(self):
3158+
b="""x = zip()"""
3159+
a="""x = list(zip())"""
3160+
self.check(b,a)
3161+
31333162
b="""x = zip(a, b, c)"""
31343163
a="""x = list(zip(a, b, c))"""
31353164
self.check(b,a)
@@ -3138,6 +3167,15 @@ def test_zip_basic(self):
31383167
a="""x = len(list(zip(a, b)))"""
31393168
self.check(b,a)
31403169

3170+
deftest_zip_trailers(self):
3171+
b="""x = zip(a, b, c)[0]"""
3172+
a="""x = list(zip(a, b, c))[0]"""
3173+
self.check(b,a)
3174+
3175+
b="""x = zip(a, b, c)[0][1]"""
3176+
a="""x = list(zip(a, b, c))[0][1]"""
3177+
self.check(b,a)
3178+
31413179
deftest_zip_nochange(self):
31423180
a="""b.join(zip(a, b))"""
31433181
self.unchanged(a)

‎Misc/ACKS‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ Andrew Bennetts
128128
Andy Bensky
129129
Bennett Benson
130130
Ezra Berch
131+
Stuart Berg
131132
Michel Van den Bergh
132133
Julian Berman
133134
Brice Berna

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp