fix_division.py 904 B

12345678910111213141516171819202122232425262728
  1. u"""
  2. Fixer for division: from __future__ import division if needed
  3. """
  4. from lib2to3 import fixer_base
  5. from libfuturize.fixer_util import token, future_import
  6. def match_division(node):
  7. u"""
  8. __future__.division redefines the meaning of a single slash for division,
  9. so we match that and only that.
  10. """
  11. slash = token.SLASH
  12. return node.type == slash and not node.next_sibling.type == slash and \
  13. not node.prev_sibling.type == slash
  14. class FixDivision(fixer_base.BaseFix):
  15. run_order = 4 # this seems to be ignored?
  16. def match(self, node):
  17. u"""
  18. Since the tree needs to be fixed once and only once if and only if it
  19. matches, then we can start discarding matches after we make the first.
  20. """
  21. return match_division(node)
  22. def transform(self, node, results):
  23. future_import(u"division", node)