0

나는 많은 시간 동안 오류로 싸우고 있었고, 무슨 일이 일어나고 있는지, 그리고 왜 효과가 없는지 아이디어가 부족했습니다.생성자에서리스트를 얻는 파이썬 객체 확장은 결코 생성 단계 (SIGSEV)를 통과하지 못합니다. 이유는 무엇입니까?

우선, 나는 C 확장을 통해 파이썬을위한 새로운 객체 유형을 만들려고한다. 이 객체는리스트, numpy 배열 등을 사용하여 생성됩니다 ...하지만이 부분을 작동시킬 수도 없습니다. 실패

실제 최소한의 코드는 다음과 같습니다

#include <python2.7/Python.h> 
    #include <python2.7/structmember.h> 
    #include <numpy/arrayobject.h> 
    #include "../python/NumpyHelperFuncs.h" 
    #include <cmath> 
    #include <algorithm> 
    #include <iostream> 
    using namespace std; 

    typedef struct { 
     PyObject_HEAD 
    } CondensedMatrix; 

    static void condensedMatrix_dealloc(CondensedMatrix* self){ 
     self->ob_type->tp_free((PyObject*)self); 
    } 

    static PyObject* condensedMatrix_new(PyTypeObject *type, PyObject *args, PyObject *kwds){ 
     CondensedMatrix *self; 
     self = (CondensedMatrix*) type->tp_alloc(type, 0); 
     return (PyObject *) self; 
    } 

    static int condensedMatrix_init(CondensedMatrix *self, PyObject *args, PyObject *kwds){ 
     PyObject* input; 
     PyArrayObject* rmsd_numpy_array; 
     cout<<"Minimum class creation"<<flush<<endl; 

     if (! PyArg_ParseTuple(args, "O",&input)){ 
      PyErr_SetString(PyExc_RuntimeError, "Error parsing parameters."); 
      return -1; 
     } 
     rmsd_numpy_array = (PyArrayObject *) PyArray_ContiguousFromObject(input, PyArray_FLOAT, 1, 1); 
     Py_DECREF(rmsd_numpy_array); 
     return 0; 
    } 

    static PyMemberDef condensedMatrix_members[] = { 
     {NULL} /* Sentinel */ 
    }; 


    static PyMethodDef condensedMatrix_methods[] = { 
     {NULL} /* Sentinel */ 
    }; 

    static PyTypeObject CondensedMatrixType = { 
     PyObject_HEAD_INIT(NULL) 
     0,            /*ob_size*/ 
     "condensedMatrix.CondensedMatrixType",   /*tp_name*/ 
     sizeof(CondensedMatrix), /*tp_basicsize*/ 
     0,       /*tp_itemsize*/ 
     (destructor)condensedMatrix_dealloc, /*tp_dealloc*/ 
     0,       /*tp_print*/ 
     0,       /*tp_getattr*/ 
     0,       /*tp_setattr*/ 
     0,       /*tp_compare*/ 
     0,       /*tp_repr*/ 
     0,       /*tp_as_number*/ 
     0,       /*tp_as_sequence*/ 
     0,       /*tp_as_mapping*/ 
     0,       /*tp_hash */ 
     0,       /*tp_call*/ 
     0,       /*tp_str*/ 
     0,       /*tp_getattro*/ 
     0,       /*tp_setattro*/ 
     0,       /*tp_as_buffer*/ 
     Py_TPFLAGS_DEFAULT| Py_TPFLAGS_BASETYPE ,      /*tp_flags*/ 
     "Condensed matrix as in pdist",   /* tp_doc */ 
     0,      /* tp_traverse */ 
     0,      /* tp_clear */ 
     0,      /* tp_richcompare */ 
     0,      /* tp_weaklistoffset */ 
     0,       /* tp_iter */ 
     0,       /* tp_iternext */ 
     condensedMatrix_methods, /* tp_methods */ 
     condensedMatrix_members, /* tp_members */ 
     0,       /* tp_getset */ 
     0,       /* tp_base */ 
     0,       /* tp_dict */ 
     0,       /* tp_descr_get */ 
     0,       /* tp_descr_set */ 
     0,       /* tp_dictoffset */ 
     (initproc)condensedMatrix_init, /* tp_init */ 
     0,        /* tp_alloc */ 
     condensedMatrix_new,    /* tp_new */ 
    }; 

    #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ 
    #define PyMODINIT_FUNC void 
    #endif 

    PyMODINIT_FUNC initcondensedMatrix(void){ 
     PyObject* module; 

     if (PyType_Ready(&CondensedMatrixType) < 0) 
      return; 

     module = Py_InitModule3("condensedMatrix", NULL,"Fast Access Condensed Matrix"); 
     if (module == NULL) 
       return; 

     Py_INCREF(&CondensedMatrixType); 
     PyModule_AddObject(module, "CondensedMatrix", (PyObject*) &CondensedMatrixType); 
    } 

이 코드는 팁 (및 복사/붙여 넣기) 다음과 같은 기록 된 : 나는 그것이 작동하는 제비 갈매기 코드를 테스트 한 http://docs.python.org/extending/newtypes.html

, 하지만 네 번째 매개 변수를 추가하여 목록을 얻으면 sigsev도 함께 종료됩니다.

gcc -pthread -fno-strict-aliasing -fmessage-length=0 -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector -funwind-tables -fasynchronous-unwind-tables -fPIC -c matrix.cpp 

g++ -pthread -shared matrix/matrix.o -lpython2.7 -o condensedMatrix.so -fopenmp 

그리고 함께 사용 :

제가 위에서 코드를 컴파일 이름 경우 == '메인' data_con = [3,4,5,6] 매트릭스 = CondensedMatrix (data_con)

은 또한 사람이 왜 알고 있나요

PyArg_ParseTuple SegFaults in CApi을 읽었습니다 그것은 pyarg_parsetuple 이후 segfault를합니까?

우분투 12.04, GCC 4.6.3, 파이썬 2.7.3

감사합니다!

+0

가에 디버거를 연결하고 그것을 통해 단계? – favoretti

답변

0

그래서 필요한 유일한 것은 'import_array();'라는 오류가 발생하기 때문에 오류를 해결하는 것이 실제로 쉬운 것처럼 보입니다. init 함수의 문장 :

PyMODINIT_FUNC initcondensedMatrix(void){ 
    PyObject* module; 

    if (PyType_Ready(&CondensedMatrixType) < 0) 
     return; 

    module = Py_InitModule3("condensedMatrix", NULL,"Fast Access Condensed Matrix"); 
    if (module == NULL) 
      return; 

    Py_INCREF(&CondensedMatrixType); 
    PyModule_AddObject(module, "CondensedMatrix", (PyObject*) &CondensedMatrixType); 
    import_array(); 
} 

그리고 그 모든했다 ...

관련 문제