有没有办法以编程方式生成Python字节码?
我想使用 Python 解释器并尝试创建一个小型 DSL 。是否有任何模块可以让我执行类似理论代码的操作(类似于 LINQ 表达式树)?
expression_tree = Function(
Print(
String('Hello world!')
)
)
compile_to_bytecode(expression_tree)
或者生成 Python 源代码会更容易吗?使用 C 或 SWIG 或 Cython 可以让这变得更容易吗?
I want to hack around with the Python interpreter and try creating a small DSL . Is there any module where I can do something like this theoretical code (similar to LINQ expression trees)?
expression_tree = Function(
Print(
String('Hello world!')
)
)
compile_to_bytecode(expression_tree)
Or would it just be easier to just generate Python source code? Could this be made easier by using C or SWIG or Cython?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
正如另一个答案所暗示的那样,通过ast工作并将树编译成字节码可能是最简单的;生成源代码并编译它们,几乎一样好。
但是,要探索较低级别的方法,请查看此页面中的链接;我发现 byteplay 特别有用(目前不适用于 2.6 或 3.*,只有 2.4 或 2.5,但我认为将其修复为 2.6 应该很容易,正如目前在其跟踪器中讨论的那样)。我没有使用 Phil Eby 的类似功能 BytecodeAssembler,但考虑到作者的声誉,我确信它是值得一看!
Working via
ast
and compiling the tree into bytecode, as another answer suggests, is probably simplest; generating sources and compiling those, almost as good.However, to explore lower-level approaches, check out the links from this page; I've found byteplay especially useful (currently doesn't work on 2.6 nor 3.*, only 2.4 or 2.5, but I think fixing it for 2.6 should be easy, as currently discussed in its tracker). I have not used Phil Eby's similarly-featured BytecodeAssembler, but given the author's reputation I'm sure it's worth checking it out!
在 Python 2.X 中,您通常会使用
编译器
module 及其ast
子模块(但请注意,此模块自 2.6 版起已弃用)。在 Python 3.X 中,您只需使用ast
。两者都提供了一个compile()函数,该函数将从源代码/AST转换为“可以通过exec语句或eval()执行的代码对象”代码>。”
In Python 2.X, you would typically approach this with the
compiler
module and itsast
sub-module (but note this module is deprecated since version 2.6). In Python 3.X, you would use justast
.Both offer a
compile()
function that will go from source/AST to "a code object that can be executed by theexec
statement oreval()
."生成 Python 代码并运行它更容易。如果这样做,您还可以更轻松地调试它,因为调试器可以显示实际的源代码。另请参阅 Python 杂志七月号上的 Malte Borchs 文章,其中他讨论了这一点以及其他内容。
It's easier to generate Python code and run it. If you do that you can also debug it more easily since there is actual source for the debugger to display. See also Malte Borchs article in the July issue of Python magazine, where he talks about this amongst other things.
Fernando Meyer 最近写了一篇博文,解释如何使用
# 编码
指令指定您自己的 Python 扩展。示例(实际格式定义位于 pyspec.py 和 tokenizer.py):Fernando Meyer recently wrote a blog post explaining how to use the
# coding
directive to specify your own extensions to Python. Example (the actual format definition is in pyspec.py and tokenizer.py):更新 Python3 - 还有非常有趣的汇编器
zachariahreed/byteasm
。实际上是 Py3 中唯一为我工作的。它有非常好的&干净的API:
Updating for Python3 - there's also very interesting assembler
zachariahreed/byteasm
.Actually the only one working for me in Py3. It has very nice & clean API:
查看此处找到的反汇编程序模块:
http://docs.python.org/library/dis。 html
Check out the disassembler module found here:
http://docs.python.org/library/dis.html