|
1 | 1 | # Copyright 2008 Armin Ronacher.
|
2 | 2 | # Licensed to PSF under a Contributor Agreement.
|
3 | 3 |
|
4 |
| -from lib2to3 import fixer_base |
5 |
| -from lib2to3.fixer_util import Name, Call, touch_import, in_special_context |
| 4 | +from lib2to3 import fixer_util |
| 5 | +from lib2to3.fixes import fix_zip |
6 | 6 |
|
7 | 7 |
|
8 |
| -class FixZip(fixer_base.ConditionalFix): |
| 8 | +class FixZip(fix_zip.FixZip): |
9 | 9 |
|
10 |
| - BM_compatible = True |
11 |
| - order = "pre" |
12 | 10 | skip_on = "six.moves.zip"
|
13 | 11 |
|
14 |
| - PATTERN = """ |
15 |
| - power< 'zip' |
16 |
| - trailer< '(' |
17 |
| - ( not(arglist | argument<any '=' any>) any* | |
18 |
| - arglist < not(argument<any '=' any>) any* > ) |
19 |
| - ')' > |
20 |
| - > |
21 |
| - """ |
22 |
| - |
23 | 12 | def transform(self, node, results):
|
24 |
| - if self.should_skip(node): |
25 |
| - # Make the fixer idempotent - if six.moves.zip is already imported, |
26 |
| - # skip it. should_skip() caches the state the first time we check, |
27 |
| - # so it doesn't skip after *we've* added the six.moves import. |
28 |
| - return |
29 |
| - |
30 |
| - touch_import(u'six.moves', u'zip', node) |
31 |
| - if in_special_context(node): |
32 |
| - # The node is somewhere where it only needs to be an iterator, |
33 |
| - # e.g. a for loop - don't wrap in list() |
34 |
| - return |
35 |
| - |
36 |
| - new = node.clone() |
37 |
| - new.prefix = "" |
38 |
| - new = Call(Name("list"), [new]) |
39 |
| - new.prefix = node.prefix |
40 |
| - return new |
| 13 | + result = super(FixZip, self).transform(node, results) |
| 14 | + # Always use six.moves.zip so that even Python 2.7 gets performance |
| 15 | + # boost from using itertools in iterator contexts. |
| 16 | + fixer_util.touch_import(u'six.moves', u'zip', node) |
| 17 | + return result |
0 commit comments