나는 파이썬에서 초보자이며 파이썬 스크립트를 시작하려고 할 때 세그멘테이션 오류 (코어 덤프) 오류가 발생한다.분할 오류 (코어 덤프 됨). 파이썬에서 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);
}
와 파이썬 스크립트 : 가 여기에 C 코드
from input_device import device_open
device_open(1)
누군가를 살펴보고 올바른 방향으로 날 지점 수, 내가 잘못 뭐하는 거지. 미리 감사드립니다.
NULL이 유효하다고 생각하지 않습니다. 예외를 발생 시키거나 Py_None을 반환 할 필요가 있습니다 (Py_INCREF (Py_None)을 잊지 마세요!). –
감사합니다 Brandon, Py_INCREF (pyfd); 오류가 발생했습니다. –