python 2.5 and ast

Andrea Crotti andrea.crotti.0 at gmail.com
Fri Dec 2 12:01:47 EST 2011


And on a related topic, how can I actually detect other types of 
imports, for example
__import__

Doing a dump I get this:

In [113]: ast.dump(ast.parse('__import__("module")'))
Out[113]: "Module(body=[Expr(value=Call(func=Name(id='__import__', 
ctx=Load()), args=[Str(s='module')], keywords=[], starargs=None, 
kwargs=None))])"

So the visitor should be quite smart, and the string passed can't be 
always be done.
I think the easiest way is just to use regexp and look for them warning 
the user that there might be other
stupid imports, the harder way is to try to detect it and if the string 
passed is actually known at compile-time
use it, otherwise warn the user.



More information about the Python-list mailing list