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

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

#countcontent
1n/a""" Fixer for imports of itertools.(imap|ifilter|izip|ifilterfalse) """
2n/a
3n/a# Local imports
4n/afrom lib2to3 import fixer_base
5n/afrom lib2to3.fixer_util import BlankLine, syms, token
6n/a
7n/a
8n/aclass FixItertoolsImports(fixer_base.BaseFix):
9n/a BM_compatible = True
10n/a PATTERN = """
11n/a import_from< 'from' 'itertools' 'import' imports=any >
12n/a """ %(locals())
13n/a
14n/a def transform(self, node, results):
15n/a imports = results['imports']
16n/a if imports.type == syms.import_as_name or not imports.children:
17n/a children = [imports]
18n/a else:
19n/a children = imports.children
20n/a for child in children[::2]:
21n/a if child.type == token.NAME:
22n/a member = child.value
23n/a name_node = child
24n/a elif child.type == token.STAR:
25n/a # Just leave the import as is.
26n/a return
27n/a else:
28n/a assert child.type == syms.import_as_name
29n/a name_node = child.children[0]
30n/a member_name = name_node.value
31n/a if member_name in ('imap', 'izip', 'ifilter'):
32n/a child.value = None
33n/a child.remove()
34n/a elif member_name in ('ifilterfalse', 'izip_longest'):
35n/a node.changed()
36n/a name_node.value = ('filterfalse' if member_name[1] == 'f'
37n/a else 'zip_longest')
38n/a
39n/a # Make sure the import statement is still sane
40n/a children = imports.children[:] or [imports]
41n/a remove_comma = True
42n/a for child in children:
43n/a if remove_comma and child.type == token.COMMA:
44n/a child.remove()
45n/a else:
46n/a remove_comma ^= True
47n/a
48n/a while children and children[-1].type == token.COMMA:
49n/a children.pop().remove()
50n/a
51n/a # If there are no imports left, just get rid of the entire statement
52n/a if (not (imports.children or getattr(imports, 'value', None)) or
53n/a imports.parent is None):
54n/a p = node.prefix
55n/a node = BlankLine()
56n/a node.prefix = p
57n/a return node