fix_next.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. u"""
  2. Fixer for:
  3. it.__next__() -> it.next().
  4. next(it) -> it.next().
  5. """
  6. from lib2to3.pgen2 import token
  7. from lib2to3.pygram import python_symbols as syms
  8. from lib2to3 import fixer_base
  9. from lib2to3.fixer_util import Name, Call, find_binding, Attr
  10. bind_warning = u"Calls to builtin next() possibly shadowed by global binding"
  11. class FixNext(fixer_base.BaseFix):
  12. PATTERN = u"""
  13. power< base=any+ trailer< '.' attr='__next__' > any* >
  14. |
  15. power< head='next' trailer< '(' arg=any ')' > any* >
  16. |
  17. classdef< 'class' base=any+ ':'
  18. suite< any*
  19. funcdef< 'def'
  20. attr='__next__'
  21. parameters< '(' NAME ')' > any+ >
  22. any* > >
  23. """
  24. def transform(self, node, results):
  25. assert results
  26. base = results.get(u"base")
  27. attr = results.get(u"attr")
  28. head = results.get(u"head")
  29. arg_ = results.get(u"arg")
  30. if arg_:
  31. arg = arg_.clone()
  32. head.replace(Attr(Name(unicode(arg),prefix=head.prefix),
  33. Name(u"next")))
  34. arg_.remove()
  35. elif base:
  36. attr.replace(Name(u"next", prefix=attr.prefix))