fix_numliterals.py 768 B

12345678910111213141516171819202122232425262728
  1. """Fixer that turns 1L into 1, 0755 into 0o755.
  2. """
  3. # Copyright 2007 Georg Brandl.
  4. # Licensed to PSF under a Contributor Agreement.
  5. # Local imports
  6. from ..pgen2 import token
  7. from .. import fixer_base
  8. from ..fixer_util import Number
  9. class FixNumliterals(fixer_base.BaseFix):
  10. # This is so simple that we don't need the pattern compiler.
  11. _accept_type = token.NUMBER
  12. def match(self, node):
  13. # Override
  14. return (node.value.startswith("0") or node.value[-1] in "Ll")
  15. def transform(self, node, results):
  16. val = node.value
  17. if val[-1] in 'Ll':
  18. val = val[:-1]
  19. elif val.startswith('0') and val.isdigit() and len(set(val)) > 1:
  20. val = "0o" + val[1:]
  21. return Number(val, prefix=node.prefix)