Luk*_*uke 8 c python python-c-api python-3.x
我想创建一个带有嵌入式python解释器和基本调试功能的应用程序.现在我在API中搜索可用于逐步运行代码的函数,并获取正在(或即将执行)的当前代码行的编号.
在进行跟踪和分析时,官方Python文档对我来说似乎有点不足.例如,没有关于返回值含义的信息Py_tracefunc.
到目前为止,我已经汇总了以下内容:
#include <Python.h>
static int lineCounter = 0;
int trace(PyObject *obj, PyFrameObject *frame, int what, PyObject *arg)
{
if(what == PyTrace_LINE)
{
lineCounter += 1;
printf("line %d\n", lineCounter);
}
return 0;
}
int main(int argc, char *argv[])
{
wchar_t *program = Py_DecodeLocale(argv[0], NULL);
if (program == NULL) {
fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
exit(1);
}
Py_SetProgramName(program); /* optional but recommended */
Py_Initialize();
PyEval_SetTrace(trace, NULL);
char *code = "def adder(a, b):\n"
" return a + b\n"
"x = 3\n"
"y = 4\n"
"print(adder(x, y))\n";
PyRun_SimpleString(code);
Py_Finalize();
PyMem_RawFree(program);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,编译器输出以下错误:
hello.c:5:26: error: unknown type name ‘PyFrameObject’
int trace(PyObject *obj, PyFrameObject *frame, int what, PyObject *arg)
^
Run Code Online (Sandbox Code Playgroud)
我在ManjaroLinux上运行并使用以下代码编译以上内容:
gcc -o hello hello.c -I/usr/include/python3.5m -Wno-unused-result -Wsign-compare -Wunreachable-code -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong --param=ssp-buffer-size=4 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -L/usr/lib -lpython3.5m -lpthread -ldl -lutil -lm -Xlinker -export-dynamic
Run Code Online (Sandbox Code Playgroud)
我发现我可以替换PyFrameObject,struct _frame然后编程编译,但每个人都知道这是一个肮脏的黑客,而不是解决方案.
可执行文件输出以下内容:
line 1
line 2
line 3
line 4
line 5
7
Run Code Online (Sandbox Code Playgroud)
但我希望跟踪脚本的执行流程(即:从第3行开始,然后是4,5,然后由于函数调用,2).
我找不到任何关于逐步执行的事情.
您能否推荐一些关于Python C API的其他资源,包括更多信息和一些主题介绍?
我用赏金给了答案,因为它无论如何都会过期.但是,我仍在寻找,并将感谢上述其他问题的答案.
Run Code Online (Sandbox Code Playgroud)hello.c:5:26: error: unknown type name ‘PyFrameObject’
此错误表示PyFrameObject尚未声明.我做了一个谷歌搜索,它显示我在Python源代码树中的frameobject.h是声明该结构的地方.
我希望你能添加这条线
#include <frameobject.h>
Run Code Online (Sandbox Code Playgroud)
解决这个问题.
| 归档时间: |
|
| 查看次数: |
754 次 |
| 最近记录: |