ยปCore Development>Code coverage>Lib/lib2to3/fixes/fix_itertools.py

Python code coverage for Lib/lib2to3/fixes/fix_itertools.py

#countcontent
1n/a""" Fixer for itertools.(imap|ifilter|izip) --> (map|filter|zip) and
2n/a itertools.ifilterfalse --> itertools.filterfalse (bugs 2360-2363)
3n/a
4n/a imports from itertools are fixed in fix_itertools_import.py
5n/a
6n/a If itertools is imported as something else (ie: import itertools as it;
7n/a it.izip(spam, eggs)) method calls will not get fixed.
8n/a """
9n/a
10n/a# Local imports
11n/afrom .. import fixer_base
12n/afrom ..fixer_util import Name
13n/a
14n/aclass FixItertools(fixer_base.BaseFix):
15n/a BM_compatible = True
16n/a it_funcs = "('imap'|'ifilter'|'izip'|'izip_longest'|'ifilterfalse')"
17n/a PATTERN = """
18n/a power< it='itertools'
19n/a trailer<
20n/a dot='.' func=%(it_funcs)s > trailer< '(' [any] ')' > >
21n/a |
22n/a power< func=%(it_funcs)s trailer< '(' [any] ')' > >
23n/a """ %(locals())
24n/a
25n/a # Needs to be run after fix_(map|zip|filter)
26n/a run_order = 6
27n/a
28n/a def transform(self, node, results):
29n/a prefix = None
30n/a func = results['func'][0]
31n/a if ('it' in results and
32n/a func.value not in ('ifilterfalse', 'izip_longest')):
33n/a dot, it = (results['dot'], results['it'])
34n/a # Remove the 'itertools'
35n/a prefix = it.prefix
36n/a it.remove()
37n/a # Replace the node which contains ('.', 'function') with the
38n/a # function (to be consistent with the second part of the pattern)
39n/a dot.remove()
40n/a func.parent.replace(func)
41n/a
42n/a prefix = prefix or func.prefix
43n/a func.replace(Name(func.value[1:], prefix=prefix))