我刚开始尝试在C中编写我的第一个Python扩展模块,并使用https://www.tutorialspoint.com/python/python_further_extensions.htm提供的说明
我在Linux Mint 18.1上,在其virtualenv版本中使用Python 3.6.1。
作为第一步,我编写了一个非常简约的Python模块版本,我计划编写。
这是我的C代码:
include <Python.h>
static PyObject* uniqueCombinations(PyObject* self)
{
return Py_BuildValue("s", "uniqueCombinations() return value (is of type 'string')");
}
static char uniqueCombinations_docs[] =
"usage: uniqueCombinations(lstSortableItems, comboSize)\n";
static PyMethodDef uniqueCombinations_funcs[] = {
{"uniqueCombinations", (PyCFunction)uniqueCombinations,
METH_NOARGS, uniqueCombinations_docs},
{NULL}
};
void inituniqueCombinations(void)
{
Py_InitModule3("uniqueCombinations", uniqueCombinations_funcs,
"Extension module uniqueCombinations v. 0.01");
}
这里是我用来编译C代码到uniqueCombinations.cpython-36m-x86_64-linux-gnu.so
的setup.py文件的代码:
from distutils.core import setup, Extension
setup(name='uniqueCombinations', version='0.01', \
ext_modules=[Extension('uniqueCombinations', ['uniqueCombinations_pythonCmodule_v-0.01_Cg.c'])])
令我惊讶的是,导入时模块无法正确加载:
>>> import uniqueCombinations
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: uniqueCombinations.cpython-36m-x86_64-linux-gnu.so: undefined symbol: Py_InitModule3
为什么我收到此错误?
我该怎么办才能正确加载已编译的模块?
这里的构建协议带有关于Py_InitModule3的隐式声明的警告:
$ python uniqueCombinations_pythonCmodule_v-0.01_setup.py_Cg.py build
running build
running build_ext
building 'uniqueCombinations' extension
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/usr/local/include/python3.6m -c uniqueCombinations_pythonCmodule_v-0.01_Cg.c -o build/temp.linux-x86_64-3.6/uniqueCombinations_pythonCmodule_v-0.01_Cg.o
uniqueCombinations_pythonCmodule_v-0.01_Cg.c: In function ‘inituniqueCombinations’:
uniqueCombinations_pythonCmodule_v-0.01_Cg.c:19:5: warning: implicit declaration of function ‘Py_InitModule3’ [-Wimplicit-function-declaration]
Py_InitModule3("uniqueCombinations", uniqueCombinations_funcs,
^
creating build/lib.linux-x86_64-3.6
gcc -pthread -shared build/temp.linux-x86_64-3.6/uniqueCombinations_pythonCmodule_v-0.01_Cg.o -o build/lib.linux-x86_64-3.6/uniqueCombinations.cpython-36m-x86_64-linux-gnu.so
答案 0 :(得分:9)
不再使用Py_InitModule(Py_InitModule3)。有必要改为创建PyModuleDef结构,然后将对它的引用传递给PyModule_Create。
在导入时创建的模块不会引发错误的C代码下面:
#include <Python.h>
static PyObject* uniqueCombinations(PyObject* self)
{
return Py_BuildValue("s", "uniqueCombinations() return value (is of type 'string')");
}
static char uniqueCombinations_docs[] =
"usage: uniqueCombinations(lstSortableItems, comboSize)\n";
/* deprecated:
static PyMethodDef uniqueCombinations_funcs[] = {
{"uniqueCombinations", (PyCFunction)uniqueCombinations,
METH_NOARGS, uniqueCombinations_docs},
{NULL}
};
use instead of the above: */
static PyMethodDef module_methods[] = {
{"uniqueCombinations", (PyCFunction) uniqueCombinations,
METH_NOARGS, uniqueCombinations_docs},
{NULL}
};
/* deprecated :
PyMODINIT_FUNC init_uniqueCombinations(void)
{
Py_InitModule3("uniqueCombinations", uniqueCombinations_funcs,
"Extension module uniqueCombinations v. 0.01");
}
*/
static struct PyModuleDef Combinations =
{
PyModuleDef_HEAD_INIT,
"Combinations", /* name of module */
"usage: Combinations.uniqueCombinations(lstSortableItems, comboSize)\n", /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
module_methods
};
PyMODINIT_FUNC PyInit_Combinations(void)
{
return PyModule_Create(&Combinations);
}
这里是构建协议:
$ python uniqueCombinations_pythonCmodule_v-0.01_setup.py_Cg.py build
running build
running build_ext
building 'Combinations' extension
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/usr/local/include/python3.6m -c uniqueCombinations_pythonCmodule_v-0.01_Cg.c -o build/temp.linux-x86_64-3.6/uniqueCombinations_pythonCmodule_v-0.01_Cg.o
gcc -pthread -shared build/temp.linux-x86_64-3.6/uniqueCombinations_pythonCmodule_v-0.01_Cg.o -o build/lib.linux-x86_64-3.6/Combinations.cpython-36m-x86_64-linux-gnu.so
以及成功导入的交互式Python控制台会话:
>>> import Combinations
>>> dir(Combinations)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'uniqueCombinations']
>>> Combinations.__doc__
'usage: Combinations.uniqueCombinations(lstSortableItems, comboSize)\n'
>>> Combinations.uniqueCombinations()
"uniqueCombinations() return value (is of type 'string')"
>>>