使用 C++ 逐行执行 python 脚本Python API
我的目标最终是执行一个 python 脚本,一次一行操作我的 C++ 程序中的值,并在 python 脚本中的语句之间返回执行到我的 C++ 程序。现在,我试图一次一行地向 python 解释器提供我的脚本。但它需要一个上下文,而我不知道如何构建它。任何人都可以给我指出一些好的教程(文档对此不是很好)。
我选择了最能回答我的问题的答案,但我相信这对于某些应用程序来说可能不足以控制。 适用于这些应用程序的答案可能涉及 Python API 中的较低级别调用。如果您有可以对执行进行更多控制的答案,请回答该问题。
我在这个问题之后又问了另一个问题,因为后来我遇到了不同的问题,这些问题是密切相关的。链接:Python C API - 停止执行(稍后继续)
My goal is ultimately to execute a python script that manipulates values in my C++ program, one line at a time, returning execution to my C++ program between statements in the python script. Right now, I'm been attempting to feed the python interpreter my script one line at a time. But it wants a context, and I have no idea how to construct that. Can anyone point me to some good tutorials (the documentation is not very good for this).
I chose the answer that most closely answered my question, but I believe this may not be enough control for some applications.
An answer that works for those applications might involve lower-level calls in the Python API. Please answer the question if you have an answer that grants more control over execution.
I Asked another question following this one, because I encountered different problems afterwards which are very closely related. Link: Python C API - Stopping Execution (and continuing it later)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为一次输入一行脚本会很困难。查看 sys.settrace() 来设置在每行执行时调用的函数。您还可以使用
PyEval_SetTrace
在 C API 中设置它,形式略有不同。I think it will be difficult to feed a script one line at a time. Look into
sys.settrace()
to set a function that is invoked at each line of execution. You can also set it in the C API usingPyEval_SetTrace
, in a slightly different form.我不能 100% 确定我完全理解你的目标,但大约 8 个月前我想做类似的事情。我希望能够从 python 脚本驱动我的 C++ 应用程序。我用的是win32、Qt、gcc。事实证明,现在 gdb(gcc 的调试器)可以通过 python 编写脚本。我花了很多时间阅读和几天的工作,但效果很好。我不必在我的 C++ 源代码中添加任何额外的代码!
I am not 100% sure that I fully understand your goal, but some 8 months ago I wanted to do something similar. I wanted to be able to drive my c++ application from python scripts. I was on win32, Qt, gcc. It turns out that these days gdb, the debugger for gcc, can be scripted via python. It took me a lot of reading and few days of work, but it worked well. I did not have to add any extra code into my c++ sources!
好吧,我告诉你......我认为最好的办法是将你的 python 脚本转换为 C++ 应用程序。这样您就可以将有效且有用的代码嵌入到源代码中。
如果你的 c 应用程序不是那么大,也许你更喜欢为 python 做一个 c 扩展并将 python 代码转到主应用程序..
无论哪种方式,我都会在 Cython 中完成。
如果您愿意,可以使用以下问题来了解如何完成这项工作: Convert Python 程序到 C/C++ 代码?
well i tell you what.. i think the best thing to do is to convert your python script to c++ app. this how you'll have working and useful code to embed in your source.
if your c app is not that big, maybe you prefer to make a c-extension for python and turn the python code to the main app..
either way, i would do it in Cython.
you can use the following question to understand how to do this job if you'd like: Convert Python program to C/C++ code?