[Python-Dev] Two laments about CPython's AST Nodes

"Martin v. Löwis" martin at v.loewis.de
Fri Aug 21 00:11:03 CEST 2009


> x = compile("def foo():pass", "foo", "exec", _ast.PyCF_ONLY_AST)
> 
> Does x contain real AST nodes or does it contain mirror structures
> (feel free to just tell me: don't be lazy, go read the code).

It only contains a mirror structure. See
pythonrun.c:Py_CompileStringFlags, and the (generated) PyAST_mod2obj
function. There is no way for a Python script to get hold of the
real AST.

> I do use the ASDL to generate this stuff, but again, the real and the
> mirror nodes are not separated ATM, and that is what makes it
> difficult.

Couldn't you just generate a check function for your tree that
would be invoked before you try to process a tree that a
script got access to?

> That is quite reasonable, I'll withdraw gripe #1 -- in fact the reason
> I have already implemented this in Jython is that there is already
> real world use out there.  I still need to understand gripe #2 a
> little better before I back down on that one.

If you are asking that a type check is made on assigning a value to
these fields - I'm not quite sure whether you could implement that
check reliably. Wouldn't it be possible to bypass it by filling a
value directly into __dict__?

If you can come up with a patch that checks in a reliable manner,
I would be in favor of adding that (in 2.7 and 3.2), taking out
the corresponding checks when converting to the internal AST.

Regards,
Martin


More information about the Python-Dev mailing list