mirror of
https://github.com/python/cpython.git
synced 2025-07-29 06:05:00 +00:00

svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3 ........ r61730 | martin.v.loewis | 2008-03-22 02:20:58 +0100 (Sa, 22 Mär 2008) | 2 lines More explicit relative imports. ........ r61755 | david.wolever | 2008-03-22 21:33:52 +0100 (Sa, 22 Mär 2008) | 1 line Fixing #2446 -- 2to3 now translates 'import foo' to 'from . import foo' ........ r61824 | david.wolever | 2008-03-24 01:30:24 +0100 (Mo, 24 Mär 2008) | 3 lines Fixed a bug where 'from itertools import izip' would return 'from itertools import' ........
45 lines
1.6 KiB
Python
45 lines
1.6 KiB
Python
""" Fixer for imports of itertools.(imap|ifilter|izip|ifilterfalse) """
|
|
|
|
# Local imports
|
|
from . import basefix
|
|
from .util import BlankLine
|
|
|
|
class FixItertoolsImports(basefix.BaseFix):
|
|
PATTERN = """
|
|
import_from< 'from' 'itertools' 'import' imports=any >
|
|
""" %(locals())
|
|
|
|
def transform(self, node, results):
|
|
imports = results['imports']
|
|
children = imports.children[:] or [imports]
|
|
for child in children:
|
|
if not hasattr(child, 'value'):
|
|
# Handle 'import ... as ...'
|
|
continue
|
|
if child.value in ('imap', 'izip', 'ifilter'):
|
|
# The value must be set to none in case child == import,
|
|
# so that the test for empty imports will work out
|
|
child.value = None
|
|
child.remove()
|
|
elif child.value == 'ifilterfalse':
|
|
node.changed()
|
|
child.value = 'filterfalse'
|
|
|
|
# Make sure the import statement is still sane
|
|
children = imports.children[:] or [imports]
|
|
remove_comma = True
|
|
for child in children:
|
|
if remove_comma and getattr(child, 'value', None) == ',':
|
|
child.remove()
|
|
else:
|
|
remove_comma ^= True
|
|
|
|
if unicode(children[-1]) == ',':
|
|
children[-1].remove()
|
|
|
|
# If there are no imports left, just get rid of the entire statement
|
|
if not (imports.children or getattr(imports, 'value', None)):
|
|
p = node.get_prefix()
|
|
node = BlankLine()
|
|
node.prefix = p
|
|
return node
|