File Lib/lib2to3/fixes/fix_filter.py changed (mode: 100644) (index bb6718cbf7..a7a5a154f6) |
... |
... |
Python 2.6 figure it out. |
15 |
15 |
|
|
16 |
16 |
# Local imports |
# Local imports |
17 |
17 |
from .. import fixer_base |
from .. import fixer_base |
18 |
|
from ..fixer_util import Name, Call, ListComp, in_special_context |
|
|
18 |
|
from ..pytree import Node |
|
19 |
|
from ..pygram import python_symbols as syms |
|
20 |
|
from ..fixer_util import Name, ArgList, ListComp, in_special_context |
|
21 |
|
|
19 |
22 |
|
|
20 |
23 |
class FixFilter(fixer_base.ConditionalFix): |
class FixFilter(fixer_base.ConditionalFix): |
21 |
24 |
BM_compatible = True |
BM_compatible = True |
|
... |
... |
class FixFilter(fixer_base.ConditionalFix): |
34 |
37 |
> |
> |
35 |
38 |
')' |
')' |
36 |
39 |
> |
> |
|
40 |
|
[extra_trailers=trailer*] |
37 |
41 |
> |
> |
38 |
42 |
| |
| |
39 |
43 |
power< |
power< |
40 |
44 |
'filter' |
'filter' |
41 |
45 |
trailer< '(' arglist< none='None' ',' seq=any > ')' > |
trailer< '(' arglist< none='None' ',' seq=any > ')' > |
|
46 |
|
[extra_trailers=trailer*] |
42 |
47 |
> |
> |
43 |
48 |
| |
| |
44 |
49 |
power< |
power< |
45 |
50 |
'filter' |
'filter' |
46 |
51 |
args=trailer< '(' [any] ')' > |
args=trailer< '(' [any] ')' > |
|
52 |
|
[extra_trailers=trailer*] |
47 |
53 |
> |
> |
48 |
54 |
""" |
""" |
49 |
55 |
|
|
|
... |
... |
class FixFilter(fixer_base.ConditionalFix): |
53 |
59 |
if self.should_skip(node): |
if self.should_skip(node): |
54 |
60 |
return |
return |
55 |
61 |
|
|
|
62 |
|
trailers = [] |
|
63 |
|
if 'extra_trailers' in results: |
|
64 |
|
for t in results['extra_trailers']: |
|
65 |
|
trailers.append(t.clone()) |
|
66 |
|
|
56 |
67 |
if "filter_lambda" in results: |
if "filter_lambda" in results: |
57 |
68 |
new = ListComp(results.get("fp").clone(), |
new = ListComp(results.get("fp").clone(), |
58 |
69 |
results.get("fp").clone(), |
results.get("fp").clone(), |
59 |
70 |
results.get("it").clone(), |
results.get("it").clone(), |
60 |
71 |
results.get("xp").clone()) |
results.get("xp").clone()) |
|
72 |
|
new = Node(syms.power, [new] + trailers, prefix="") |
61 |
73 |
|
|
62 |
74 |
elif "none" in results: |
elif "none" in results: |
63 |
75 |
new = ListComp(Name("_f"), |
new = ListComp(Name("_f"), |
64 |
76 |
Name("_f"), |
Name("_f"), |
65 |
77 |
results["seq"].clone(), |
results["seq"].clone(), |
66 |
78 |
Name("_f")) |
Name("_f")) |
|
79 |
|
new = Node(syms.power, [new] + trailers, prefix="") |
67 |
80 |
|
|
68 |
81 |
else: |
else: |
69 |
82 |
if in_special_context(node): |
if in_special_context(node): |
70 |
83 |
return None |
return None |
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) |
72 |
88 |
new.prefix = "" |
new.prefix = "" |
73 |
|
new = Call(Name("list"), [new]) |
|
74 |
89 |
new.prefix = node.prefix |
new.prefix = node.prefix |
75 |
90 |
return new |
return new |
File Lib/lib2to3/fixes/fix_map.py changed (mode: 100644) (index 9f966feede..78cf81c6f9) |
... |
... |
soon as the shortest argument is exhausted. |
22 |
22 |
# Local imports |
# Local imports |
23 |
23 |
from ..pgen2 import token |
from ..pgen2 import token |
24 |
24 |
from .. import fixer_base |
from .. import fixer_base |
25 |
|
from ..fixer_util import Name, Call, ListComp, in_special_context |
|
|
25 |
|
from ..fixer_util import Name, ArgList, Call, ListComp, in_special_context |
26 |
26 |
from ..pygram import python_symbols as syms |
from ..pygram import python_symbols as syms |
|
27 |
|
from ..pytree import Node |
|
28 |
|
|
27 |
29 |
|
|
28 |
30 |
class FixMap(fixer_base.ConditionalFix): |
class FixMap(fixer_base.ConditionalFix): |
29 |
31 |
BM_compatible = True |
BM_compatible = True |
|
... |
... |
class FixMap(fixer_base.ConditionalFix): |
32 |
34 |
map_none=power< |
map_none=power< |
33 |
35 |
'map' |
'map' |
34 |
36 |
trailer< '(' arglist< 'None' ',' arg=any [','] > ')' > |
trailer< '(' arglist< 'None' ',' arg=any [','] > ')' > |
|
37 |
|
[extra_trailers=trailer*] |
35 |
38 |
> |
> |
36 |
39 |
| |
| |
37 |
40 |
map_lambda=power< |
map_lambda=power< |
|
... |
... |
class FixMap(fixer_base.ConditionalFix): |
47 |
50 |
> |
> |
48 |
51 |
')' |
')' |
49 |
52 |
> |
> |
|
53 |
|
[extra_trailers=trailer*] |
50 |
54 |
> |
> |
51 |
55 |
| |
| |
52 |
56 |
power< |
power< |
53 |
|
'map' trailer< '(' [arglist=any] ')' > |
|
|
57 |
|
'map' args=trailer< '(' [any] ')' > |
|
58 |
|
[extra_trailers=trailer*] |
54 |
59 |
> |
> |
55 |
60 |
""" |
""" |
56 |
61 |
|
|
|
... |
... |
class FixMap(fixer_base.ConditionalFix): |
60 |
65 |
if self.should_skip(node): |
if self.should_skip(node): |
61 |
66 |
return |
return |
62 |
67 |
|
|
|
68 |
|
trailers = [] |
|
69 |
|
if 'extra_trailers' in results: |
|
70 |
|
for t in results['extra_trailers']: |
|
71 |
|
trailers.append(t.clone()) |
|
72 |
|
|
63 |
73 |
if node.parent.type == syms.simple_stmt: |
if node.parent.type == syms.simple_stmt: |
64 |
74 |
self.warning(node, "You should use a for loop here") |
self.warning(node, "You should use a for loop here") |
65 |
75 |
new = node.clone() |
new = node.clone() |
|
... |
... |
class FixMap(fixer_base.ConditionalFix): |
69 |
79 |
new = ListComp(results["xp"].clone(), |
new = ListComp(results["xp"].clone(), |
70 |
80 |
results["fp"].clone(), |
results["fp"].clone(), |
71 |
81 |
results["it"].clone()) |
results["it"].clone()) |
|
82 |
|
new = Node(syms.power, [new] + trailers, prefix="") |
|
83 |
|
|
72 |
84 |
else: |
else: |
73 |
85 |
if "map_none" in results: |
if "map_none" in results: |
74 |
86 |
new = results["arg"].clone() |
new = results["arg"].clone() |
|
87 |
|
new.prefix = "" |
75 |
88 |
else: |
else: |
76 |
|
if "arglist" in results: |
|
77 |
|
args = results["arglist"] |
|
78 |
|
if args.type == syms.arglist and \ |
|
79 |
|
args.children[0].type == token.NAME and \ |
|
80 |
|
args.children[0].value == "None": |
|
|
89 |
|
if "args" in results: |
|
90 |
|
args = results["args"] |
|
91 |
|
if args.type == syms.trailer and \ |
|
92 |
|
args.children[1].type == syms.arglist and \ |
|
93 |
|
args.children[1].children[0].type == token.NAME and \ |
|
94 |
|
args.children[1].children[0].value == "None": |
81 |
95 |
self.warning(node, "cannot convert map(None, ...) " |
self.warning(node, "cannot convert map(None, ...) " |
82 |
96 |
"with multiple arguments because map() " |
"with multiple arguments because map() " |
83 |
97 |
"now truncates to the shortest sequence") |
"now truncates to the shortest sequence") |
84 |
98 |
return |
return |
|
99 |
|
|
|
100 |
|
new = Node(syms.power, [Name("map"), args.clone()]) |
|
101 |
|
new.prefix = "" |
|
102 |
|
|
85 |
103 |
if in_special_context(node): |
if in_special_context(node): |
86 |
104 |
return None |
return None |
87 |
|
new = node.clone() |
|
|
105 |
|
|
|
106 |
|
new = Node(syms.power, [Name("list"), ArgList([new])] + trailers) |
88 |
107 |
new.prefix = "" |
new.prefix = "" |
89 |
|
new = Call(Name("list"), [new]) |
|
|
108 |
|
|
90 |
109 |
new.prefix = node.prefix |
new.prefix = node.prefix |
91 |
110 |
return new |
return new |
File Lib/lib2to3/fixes/fix_zip.py changed (mode: 100644) (index 8f36a94fb4..52c28df6aa) |
... |
... |
iter(<>), list(<>), tuple(<>), sorted(<>), ...join(<>), or for V in <>:. |
9 |
9 |
|
|
10 |
10 |
# Local imports |
# Local imports |
11 |
11 |
from .. import fixer_base |
from .. import fixer_base |
12 |
|
from ..fixer_util import Name, Call, in_special_context |
|
|
12 |
|
from ..pytree import Node |
|
13 |
|
from ..pygram import python_symbols as syms |
|
14 |
|
from ..fixer_util import Name, ArgList, in_special_context |
|
15 |
|
|
13 |
16 |
|
|
14 |
17 |
class FixZip(fixer_base.ConditionalFix): |
class FixZip(fixer_base.ConditionalFix): |
15 |
18 |
|
|
16 |
19 |
BM_compatible = True |
BM_compatible = True |
17 |
20 |
PATTERN = """ |
PATTERN = """ |
18 |
|
power< 'zip' args=trailer< '(' [any] ')' > |
|
|
21 |
|
power< 'zip' args=trailer< '(' [any] ')' > [trailers=trailer*] |
19 |
22 |
> |
> |
20 |
23 |
""" |
""" |
21 |
24 |
|
|
|
... |
... |
class FixZip(fixer_base.ConditionalFix): |
28 |
31 |
if in_special_context(node): |
if in_special_context(node): |
29 |
32 |
return None |
return None |
30 |
33 |
|
|
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' in results: |
|
39 |
|
trailers = [n.clone() for n in results['trailers']] |
|
40 |
|
for n in trailers: |
|
41 |
|
n.prefix = "" |
|
42 |
|
|
|
43 |
|
new = Node(syms.power, [Name("zip"), args], prefix="") |
|
44 |
|
new = Node(syms.power, [Name("list"), ArgList([new])] + trailers) |
34 |
45 |
new.prefix = node.prefix |
new.prefix = node.prefix |
35 |
46 |
return new |
return new |
File Lib/lib2to3/tests/test_fixers.py changed (mode: 100644) (index b3f2680725..3e1a255737) |
... |
... |
class Test_filter(FixerTestCase): |
2954 |
2954 |
a = """x = [x for x in range(10) if x%2 == 0]""" |
a = """x = [x for x in range(10) if x%2 == 0]""" |
2955 |
2955 |
self.check(b, a) |
self.check(b, a) |
2956 |
2956 |
|
|
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 |
|
def test_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) |
2961 |
2974 |
|
|
2962 |
2975 |
def test_filter_nochange(self): |
def test_filter_nochange(self): |
2963 |
2976 |
a = """b.join(filter(f, 'abc'))""" |
a = """b.join(filter(f, 'abc'))""" |
|
... |
... |
class Test_map(FixerTestCase): |
3022 |
3035 |
a = """x = list(map( f, 'abc' ))""" |
a = """x = list(map( f, 'abc' ))""" |
3023 |
3036 |
self.check(b, a) |
self.check(b, a) |
3024 |
3037 |
|
|
|
3038 |
|
def test_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 |
|
|
3025 |
3055 |
def test_trailing_comment(self): |
def test_trailing_comment(self): |
3026 |
3056 |
b = """x = map(f, 'abc') # foo""" |
b = """x = map(f, 'abc') # foo""" |
3027 |
3057 |
a = """x = list(map(f, 'abc')) # foo""" |
a = """x = list(map(f, 'abc')) # foo""" |
|
... |
... |
class Test_map(FixerTestCase): |
3066 |
3096 |
""" |
""" |
3067 |
3097 |
self.warns(b, a, "You should use a for loop here") |
self.warns(b, a, "You should use a for loop here") |
3068 |
3098 |
|
|
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 |
|
|
|
3074 |
3099 |
def test_map_nochange(self): |
def test_map_nochange(self): |
3075 |
3100 |
a = """b.join(map(f, 'abc'))""" |
a = """b.join(map(f, 'abc'))""" |
3076 |
3101 |
self.unchanged(a) |
self.unchanged(a) |
|
... |
... |
class Test_zip(FixerTestCase): |
3130 |
3155 |
super(Test_zip, self).check(b, a) |
super(Test_zip, self).check(b, a) |
3131 |
3156 |
|
|
3132 |
3157 |
def test_zip_basic(self): |
def test_zip_basic(self): |
|
3158 |
|
b = """x = zip()""" |
|
3159 |
|
a = """x = list(zip())""" |
|
3160 |
|
self.check(b, a) |
|
3161 |
|
|
3133 |
3162 |
b = """x = zip(a, b, c)""" |
b = """x = zip(a, b, c)""" |
3134 |
3163 |
a = """x = list(zip(a, b, c))""" |
a = """x = list(zip(a, b, c))""" |
3135 |
3164 |
self.check(b, a) |
self.check(b, a) |
|
... |
... |
class Test_zip(FixerTestCase): |
3138 |
3167 |
a = """x = len(list(zip(a, b)))""" |
a = """x = len(list(zip(a, b)))""" |
3139 |
3168 |
self.check(b, a) |
self.check(b, a) |
3140 |
3169 |
|
|
|
3170 |
|
def test_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 |
|
|
3141 |
3179 |
def test_zip_nochange(self): |
def test_zip_nochange(self): |
3142 |
3180 |
a = """b.join(zip(a, b))""" |
a = """b.join(zip(a, b))""" |
3143 |
3181 |
self.unchanged(a) |
self.unchanged(a) |