【发布时间】:2014-12-14 23:20:18
【问题描述】:
我的应用程序嵌入了 python。该应用程序通过添加它自己的函数来扩展嵌入式 python:
PyObject *ReadDPoint(PyObject *self, PyObject *args)
然后应用程序运行调用 ReadDPoint 扩展函数的不同 python 脚本(模块)。 任务是确定调用的 ReadDPoint 函数内的模块名称,调用该函数的模块的名称。 我希望解决方案类似于以下之一:
PyObject *module_name = PyObject_GetAttrString(self, "__name__")
或
char *module_name = PyModule_GetName(self)
但我惊讶地发现,在扩展函数 ReadDpoint 内部,self 等于 NULL,并且引用它会导致崩溃。 有没有办法确定 C 函数中的模块名称? 谢谢!
【问题讨论】:
-
这些代码都没有将函数添加到模块中。
-
Python 中的函数属于它们定义的模块,而不是被调用的模块。如果您能够从 Python 代码中调用它,您的扩展模块必须已经注册了它。
self指向方法的对象,即unless it is a bound method; it is NULL
标签: python embedding extending