PEP0238 lament

Skip Montanaro skip at pobox.com
Mon Jul 23 12:08:53 EDT 2001


    Terry> (Just noticed: http://python.sourceforge.net/peps/pep-0238.html
    Terry> now proposes a *2 year* int/int warning period starting with 2.3.
    Terry> It still needs a stipulation that someone write an intdiv checker
    Terry> to help automate replacements.)

Here's a quick div finder (got some off-by-one errors trying to track
NEWLINE tokens, hence the self.line hack).

Skip

import tokenize
import sys

class TokenSorter:
    def __init__(self, f):
        self.tokens = {}
        self.filename = f
        self.line = ""
        self.linenumber = 0
        self.lastprinted = 0

    def tokeneater(self, typ, val, (sr, sc), (er, ec), line):
        if self.line != line:
            self.linenumber += 1
            self.line = line
        if (tokenize.tok_name[typ] == "OP" and
            val == "/" and
            self.lastprinted != self.linenumber):
            print "%s(%d): %s" % (self.filename, self.linenumber,
                                   line.rstrip())
            self.lastprinted = self.linenumber

def main():
    for fn in sys.argv[1:]:
        try:
            f = open(fn)
        except IOError:
            pass
        else:
            ts = TokenSorter(fn)
            try:
                tokenize.tokenize(f.readline, ts.tokeneater)
            except tokenize.TokenError:
                pass

if __name__ == "__main__":
    main()





More information about the Python-list mailing list