/Lib/lib2to3/fixes/fix_zip.py

http://unladen-swallow.googlecode.com/ · Python · 34 lines · 32 code · 0 blank · 2 comment · 0 complexity · 8e61d2105f3122181e793e7c9b4caf31 MD5 · raw file

  1. """
  2. Fixer that changes zip(seq0, seq1, ...) into list(zip(seq0, seq1, ...)
  3. unless there exists a 'from future_builtins import zip' statement in the
  4. top-level namespace.
  5. We avoid the transformation if the zip() call is directly contained in
  6. iter(<>), list(<>), tuple(<>), sorted(<>), ...join(<>), or for V in <>:.
  7. """
  8. # Local imports
  9. from .. import fixer_base
  10. from ..fixer_util import Name, Call, in_special_context
  11. class FixZip(fixer_base.ConditionalFix):
  12. PATTERN = """
  13. power< 'zip' args=trailer< '(' [any] ')' >
  14. >
  15. """
  16. skip_on = "future_builtins.zip"
  17. def transform(self, node, results):
  18. if self.should_skip(node):
  19. return
  20. if in_special_context(node):
  21. return None
  22. new = node.clone()
  23. new.set_prefix("")
  24. new = Call(Name("list"), [new])
  25. new.set_prefix(node.get_prefix())
  26. return new