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

"Martin v. Löwis" martin at v.loewis.de
Tue Feb 13 08:00:15 CET 2007


Collin Winter schrieb:

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 can be done, I think.

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.

Please look at Python.asdl. This things are really belong to sum nodes, and Add, Sub etc. are really operators.

I think they are singletons, and I don't mind making it a guarantee that they are: i.e. all operators that have no fields and where the sum has no attributes.

If you want to write "is", you can also write

node.op.__class__ is Add

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.

Notice that you cannot restore the original source code, anyway. You cannot tell whether something was else:if or elif.

I don't understand why you need the line number of the else keyword (which, as I said, may not exist in the source) for code coverage: The 'else' is never executed (only the statements in it are).

Regards, Martin



More information about the Python-Dev mailing list