我尝试使用c-api为python编写模块,但模块不会导入。这个最小的例子仍然存在问题:
#include <Python.h>
#include <stdio.h>
static PyMethodDef CAPMethods[] = {
{NULL, NULL, 0, NULL} /* Sentinel for end of array*/
};
static struct PyModuleDef CAPModule = {
PyModuleDef_HEAD_INIT,
"puzzler", /* name of module */
NULL, /* module documentation, may be NULL */
-1, /* -1 if the module keeps state in global variables. */
CAPMethods, /* Method table */
};
PyMODINIT_FUNC
PyInit_puzzler(void)
{
printf("Initializing...\n");
PyObject* module = PyModule_Create(&CAPModule);
if(module == NULL)
{
printf("Minimal Failed!\n");
Py_RETURN_NONE;
}
printf("Success!\n");
return module;
}
感觉我错过了一些非常明显的东西,但我不知道它是什么。为了完整性,这里是我的setup.py:
from distutils.core import setup, Extension
CAP = Extension(
'puzzler',
sources = ['minimal.c'],
)
setup( name = 'PuzzleSolver',
version = '1.0',
description = 'This is a demo package',
ext_modules = [CAP])
如果有人知道我的错误或想要了解有关我的设置的其他信息,请随时在评论中提问。我使用python setup.py install
(Python 3.3.3 (v3.3.3:c3896275c0f6, Nov 18 2013, 21:19:30) [MSC v.1600 64 bit (AMD64)] on win32
)编译。
以下是我试用时的python控制台输出:
>>> import puzzler
Initializing...
Minimal Failed!
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
SystemError: initialization of puzzler raised unreported exception