-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
17 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,17 @@ | ||
# Copyright 2008 Armin Ronacher. | ||
# Licensed to PSF under a Contributor Agreement. | ||
|
||
from lib2to3 import fixer_base | ||
from lib2to3.fixer_util import Call, Name, touch_import, in_special_context | ||
from lib2to3 import fixer_util | ||
from lib2to3.fixes import fix_map | ||
|
||
|
||
class FixMap(fixer_base.ConditionalFix): | ||
class FixMap(fix_map.FixMap): | ||
|
||
BM_compatible = True | ||
order = "pre" | ||
skip_on = "six.moves.map" | ||
|
||
PATTERN = """ | ||
power< 'map' | ||
trailer< '(' | ||
arglist< ( | ||
not(argument<any '=' any>) any ',' | ||
(not(argument<any '=' any>) any)+ | ||
) > | ||
')' > | ||
> | ||
""" | ||
|
||
def transform(self, node, results): | ||
if self.should_skip(node): | ||
# Make the fixer idempotent - if six.moves.map is already imported, | ||
# skip it. should_skip() caches the state the first time we check, | ||
# so it doesn't skip after *we've* added the six.moves import. | ||
return | ||
|
||
touch_import(u'six.moves', u'map', node) | ||
if in_special_context(node): | ||
# The node is somewhere where it only needs to be an iterator, | ||
# e.g. a for loop - don't wrap in list() | ||
return | ||
|
||
new = node.clone() | ||
new.prefix = "" | ||
new = Call(Name("list"), [new]) | ||
new.prefix = node.prefix | ||
return new | ||
result = super(FixMap, self).transform(node, results) | ||
# Always use the import even if no change is required so as to have | ||
# improved performance in iterator contexts even on Python 2.7. | ||
fixer_util.touch_import(u'six.moves', u'map', node) | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters