fix_numliterals.py 768 Bytes
Newer Older
1 2 3 4 5 6 7
"""Fixer that turns 1L into 1, 0755 into 0o755.
"""
# Copyright 2007 Georg Brandl.
# Licensed to PSF under a Contributor Agreement.

# Local imports
from ..pgen2 import token
8
from .. import fixer_base
9
from ..fixer_util import Number
10 11


12
class FixNumliterals(fixer_base.BaseFix):
13 14
    # This is so simple that we don't need the pattern compiler.

15 16
    _accept_type = token.NUMBER

17 18
    def match(self, node):
        # Override
19
        return (node.value.startswith("0") or node.value[-1] in "Ll")
20 21 22 23 24 25 26 27

    def transform(self, node, results):
        val = node.value
        if val[-1] in 'Ll':
            val = val[:-1]
        elif val.startswith('0') and val.isdigit() and len(set(val)) > 1:
            val = "0o" + val[1:]

28
        return Number(val, prefix=node.prefix)