PyMODINIT_FUNC和PyModule_Create之间的区别 [英] Difference between PyMODINIT_FUNC and PyModule_Create
问题描述
如果我理解正确,
-
Python 2.X中的
- PyMODINIT_FUNC已在Python3.X中由
PyModule_Create
取代 -
都返回
PyObject*
,但是,在Python 3.X中,模块的初始化函数 必须 将PyObject*
返回到模块-即
- PyMODINIT_FUNC in Python 2.X has been replaced by
PyModule_Create
in Python3.X Both return
PyObject*
, however, in Python 3.X, the module's initialization function MUST return thePyObject*
to the module - i.e.
PyMODINIT_FUNC
PyInit_spam(void)
{
return PyModule_Create(&spammodule);
}
而在Python2.X中,则没有必要-即
whereas in Python2.X, this is not necessary - i.e.
PyMODINIT_FUNC
initspam(void)
{
(void) Py_InitModule("spam", SpamMethods);
}
因此,我的健全性检查问题是:
So, my sanity checking questions are:
- 我的理解正确吗?
- 为什么要进行此更改?
现在,我仅在试验非常简单的Python C扩展案例.也许如果我做更多的事情,答案很明显,或者如果我正在尝试将Python嵌入其他东西中…….
Right now, I'm only experimenting with very simple cases of C-extensions of Python. Perhaps if I were doing more, the answer to this would be obvious, or maybe if I were trying to embed Python into something else....
推荐答案
-
是的,您的理解是正确的.您必须使用返回类型PyMODINIT_FUNC从初始化函数返回新的模块对象. (PyMODINIT_FUNC声明该函数在python2中返回void,并在python3中返回PyObject *.)
Yes, your understanding is correct. You must return the new module object from the initing function with return type PyMODINIT_FUNC. (PyMODINIT_FUNC declares the function to return void in python2, and to return PyObject* in python3.)
我只能推测做出更改的人员的动机,但是我相信这样做是为了可以更轻松地识别导入模块的错误(您可以从module-init函数返回NULL)在python3中,如果出现问题).
I can only speculate as to the motivations of the people who made the change, but I believe it was so that errors in importing the module could be more easily identified (you can return NULL from the module-init function in python3 if something went wrong).
这篇关于PyMODINIT_FUNC和PyModule_Create之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!