我一直在用python试验ASTs。我想通过在运行时转换AST来修改方法。编译python AST到方法
我可以通过使用inspect.getsource()
和 来获得预编译方法的来源我可以根据AST访问者的要求修改AST。
这可能是相当幼稚,但我希望能够编译AST和做一个类似于:
myClass.method.__func__.__code__ = compile(newAST, '<string>', 'exec')
但是编译只接受与ast.Module作为root AST。 有没有办法编译一个ast.FunctionDef,或从编译的(和空的)模块代码检索功能代码对象?
任何指向这类信息的信息指针,将不胜感激。我见过的AST例子只是处理简单的表达式。
我意识到我只需要在名称空间中执行模块,然后我就可以访问正常的结构。 所以模式是:
src = inspect.getsource(myClass.myMethod)
astFromSrc = ast.parse(unindent(src)) # need to remove extra indent from method
transform(astFromSrc.body) # transform the AST as you need
ast.fix_missing_locations(astFromSrc) # fix up line numbers etc
compiled = compile(astFromSrc, '<string>', 'exec') # compile the module AST
####### From here is the part I was missing
myScope = {} # make an empty namespace
exec compiled in myScope # now myScope contains a proper compiled function
# Now replace the original with the modified version
myClass.myMethod.__func__.__code__ = myScope['myMethod'].__code__
但现在我还有一个问题: ,以便修改一次时进行,之后从.pyc文件文件加载如何插槽进入正常的Python构建过程呢?
既然您发现了您的问题的答案,请将其作为答案发布并接受。 – cpburnz