[Python-Dev] Recent experience with the _ast module (original) (raw)

Brett Cannon brett at python.org
Tue Feb 13 01:47:19 CET 2007


On 2/12/07, Collin Winter <collinw at gmail.com> wrote:

I've been working this past week at converting the stdlib's compiler package to use 2.5's new ast module instead of the package's own AST. Overall, ast was a joy to work with, save for the following nitpicks:

1) There are times when the fields attribute on some AST nodes is None; if this was done to indicate that a given node has no AST-related attributes, it would be much easier if fields was [] in this case. As it is, I had to special-case "node.fields is None" in the visitor so that I don't accidentally iterate over it.

That makes total sense to me.

2) {BinOp,AugAssign,BoolOp,etc}.op is an instance of, eg, Add, Sub, Mult, Mod, meaning you have to dispatch based on tests like "isinstance(node.op, x)" or "type(node.op) is x". I would much, much prefer to spell this "node.op is x", ie, use "node.op = Add" rather than the current "node.op = Add()" when constructing the nodes.

I can't think of a reason, off the top of my head, why they can't be singletons. It actually makes sense since they are basically an enumeration value in the AST grammar.

3) I'd like there to be an Else() node for If.orelse, While.orelse, etc. My motivation is that I need the lineno and coloffset values of the "else" statement for a code-coverage utility; as it is, I have to find the last lineno of the if/while/etc suite and the first lineno of the "else" suite and then search between those two for the "else" statement.

Ouch. I don't know how much work it would be to make this change, but it seems reasonable to want.

Thoughts?

They all sound reasonable. And it's nice to have a wanted feature be found from actual use. =)

-Brett



More information about the Python-Dev mailing list