File: fix_numliterals.py

package info (click to toggle)
python-fissix 24.4.24-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,156 kB
  • sloc: python: 16,578; sh: 56; makefile: 48
file content (39 lines) | stat: -rw-r--r-- 1,007 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""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
from .. import fixer_base
from ..fixer_util import Number


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

    _accept_type = token.NUMBER

    def is_long(self, node):
        return node.value[-1] in "Ll"

    def is_octal(self, node):
        return (
            node.value.startswith("0")
            and node.value.isdigit()
            and len(set(node.value)) > 1
        )

    def match(self, node):
        # Override
        return self.is_long(node) or self.is_octal(node)

    def transform(self, node, results):
        val = node.value
        if self.is_long(node):
            return Number(node.value[:-1], prefix=node.prefix)
        elif self.is_octal(node):
            return Number("0o" + node.value[1:], prefix=node.prefix)

        return None