【发布时间】:2013-04-05 11:25:18
【问题描述】:
我是 python 的新手,我正在尝试使用在 C 上编写的模块启动 python 脚本。当我尝试启动 python 脚本时,我收到分段错误(核心转储)错误。 这是一个C代码:
// input_device.c
#include "Python.h"
#include "input.h"
static PyObject* input_device_open(PyObject* self, PyObject* id)
{
int fd, nr;
PyObject* pyfd;
if (!PyInt_Check(id))
return NULL;
nr = (int)PyInt_AsLong(id);
fd = device_open(nr, 0);
if (fd == -1)
return NULL;
pyfd = PyInt_FromLong(fd);
Py_INCREF(pyfd);
return pyfd;
}
static PyMethodDef module_methods[] =
{
{ "device_open", (PyCFunction)input_device_open, METH_VARARGS, "..." },
{ NULL, NULL, 0, NULL }
};
PyMODINIT_FUNC initinput_device(void)
{
Py_InitModule4("input_device", module_methods, "wrapper methods", 0, PYTHON_API_VERSION);
}
和python脚本:
from input_device import device_open
device_open(1)
有人可以看看并指出我正确的方向,我做错了什么。提前致谢。
【问题讨论】:
标签: python c python-2.7