我一直在尝试使用 Python 中的 AST。我想通过在运行时转换AST来修改方法。
使用 inspect.getsource()
,我可以获取预编译方法的源代码,并且可以通过使用AST访问器根据需要修改AST。
这可能相当幼稚,但我希望能够编译AST并执行类似以下操作的内容:
myClass.method.__func__.__code__ = compile(newAST, '<string>', 'exec')
但是,编译器只会接受以ast.Module为根的AST。是否有一种方法可以仅编译ast.FunctionDef,或者从已编译的(并且其他部分为空)Module代码中检索函数代码对象?
如果有任何涵盖此类内容的信息指针,将不胜感激。我看到的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__
但是我现在有另一个问题:如何将其插入到普通的Python构建过程中,以便只进行一次修改,然后从.pyc文件中加载?
exec compiled in myScope
语法是什么意思? - Samuel Marks