Getting at line numbers with parsermodule

Charles G Waldman cgw at fnal.gov
Tue Aug 10 17:29:22 EDT 1999


I'm trying to work up some syntax- and consistency-checking tools, and
rather than creating my own Python parser (a la "pylint") I'm trying
to use the parser module.

It's coming along OK (modulo not having enough free time to work on
it), except I've run into one snag.  I can find certain "bad" patterns 
in the AST - one thing in particular I'm looking for is code like 
"%s %s %s" % (1,2) - but once I've identified this code I have trouble 
printing out the line number in the source where the offending code 
occurred.  I tried to simply go throught the AST object (converted to
a tuple) and look for NEWLINE tokens, count these as I go throught the 
AST to get a current line-count.  This doesn't seem to work correctly
since there are fewer NEWLINE tokens than there were lines in the
original file. 

What I'd like to be able to do is, given any node in an AST object
returned by compile.suite, is to know the line number in the input
that corresponds to this node.  Is this doable?  Any suggestions?









More information about the Python-list mailing list