Re: [Python-ideas] parser in stdlib

On 10 May 2007, at 18.39, Aaron Brady wrote:
That's because it's a builtin module, written in C. It's Python/ parsermodule.c in the source distributions. What exactly are you suggesting should be possible? Are you trying to programmatically change the parser?

Yes. The relevant code snippet: class Transformer: <snip> def parsesuite(self, text): """Return a modified parse tree for the given suite text.""" return self.transform(parser.suite(text)) Hence to allusion to Van Rossum's note, for the record.

I'd also like to get an attribute in class objects containing the first line number, and can't see why not. Code objects have them; new.code() requires them. I've treated dynamic creation on the newsgroup, and inspect.findsource(object): if isclass(object): name = object.__name__ pat = re.compile(r'^(\s*)class\s*' + name + r'\b') # make some effort to find the best matching class definition: # use the one with the least indentation, which is the one # that's most probably not inside a function definition. candidates = [] cleans up quite a bit. Aaron Brady

"Collin Winter" <collinw@gmail.com> | That is not and will not be possible using Python's built-in parser. One can manipulate the AST that the parser produces. (This is how the 2to3 converter tool will work.) Perhaps this will at least partially meet the OP's needs. Beats head-butting a wall. tjr

I've very much looked into it. In fact, it gave rise to the follow-up idea of attaching a `firstlineno' attribute to class objects. (18:12 American Central Time.) Rather costly, 50% of the attributes, but stepping through the InteractiveCodeGenerator class hasn't failed yet.

Yes. The relevant code snippet: class Transformer: <snip> def parsesuite(self, text): """Return a modified parse tree for the given suite text.""" return self.transform(parser.suite(text)) Hence to allusion to Van Rossum's note, for the record.

I'd also like to get an attribute in class objects containing the first line number, and can't see why not. Code objects have them; new.code() requires them. I've treated dynamic creation on the newsgroup, and inspect.findsource(object): if isclass(object): name = object.__name__ pat = re.compile(r'^(\s*)class\s*' + name + r'\b') # make some effort to find the best matching class definition: # use the one with the least indentation, which is the one # that's most probably not inside a function definition. candidates = [] cleans up quite a bit. Aaron Brady

"Collin Winter" <collinw@gmail.com> | That is not and will not be possible using Python's built-in parser. One can manipulate the AST that the parser produces. (This is how the 2to3 converter tool will work.) Perhaps this will at least partially meet the OP's needs. Beats head-butting a wall. tjr

I've very much looked into it. In fact, it gave rise to the follow-up idea of attaching a `firstlineno' attribute to class objects. (18:12 American Central Time.) Rather costly, 50% of the attributes, but stepping through the InteractiveCodeGenerator class hasn't failed yet.

This is better. Inspect.getsource( AClass ) can be wrong. Raise exceptions before that happens. import inspect class A: b=1 class A: b=2 B=A print inspect.getsource(B) Output is: class A: b=1 Which is not what I asked it. Return candidates maybe?
participants (4)
-
Aaron Brady
-
Adam Atlas
-
Collin Winter
-
Terry Reedy