2014-06-17 47 views
1

這是我第一次嘗試擴展Python函數。 在Python我有花車的4維列表:用C擴展Python,numpy - 代碼行嗎?

import polarMM  
data = [[[(102.0, 815.0), (84.0, 791.0), (302.0, 806.0), (38.0, 801.0)], [(70.0, 696.0), (52.0, 653.0), (172.0, 649.0), (13.0, 647.0)], [(77.0, 696.0), (143.0, 653.0), (211.0, 649.0), (197.0, 647.0)], [(115.0, 815.0), (228.0, 791.0), (371.0, 806.0), (322.0, 801.0)]], [[(167.0, 815.0), (242.0, 791.0), (465.0, 806.0), (339.0, 801.0)], [(135.0, 696.0), (186.0, 653.0), (329.0, 649.0), (271.0, 647.0)], [(142.0, 696.0), (277.0, 653.0), (373.0, 649.0), (489.0, 647.0)], [(180.0, 815.0), (397.0, 791.0), (533.0, 806.0), (623.0, 801.0)]]] 
print polarMM.test(data) 

這裏爲c我的代碼;它工作之前,我把一行解析列表。 我不知道:我的環境不好或者這部分代碼錯了?飛機墜毀的

static PyObject * 
polarMM_test(PyObject *self, PyObject *args) 
    { 
    PyObject *objContours; 
    PyArrayObject *pyContours; 

    if (!PyArg_ParseTuple(args, "OO", &objContours, &objMMValues)) 
      return NULL; 

    // **** AND HERE CODE CRASHES *****: 
    pyContours = (PyArrayObject*) PyArray_FROMANY (objContours, PyArray_DOUBLE, 0, 4, NPY_IN_ARRAY); 

    return Py_BuildValue("s", "just test"); 
} 

堆棧跟蹤:

Exception Type: EXC_BAD_ACCESS (SIGSEGV) 
Exception Codes: KERN_INVALID_ADDRESS at 0x0000000000000228 
... 
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread 
0 polarMM.so      0x00000001002f1d1d polarMM_test + 45 (polarMM.c:63) 
1 org.python.python    0x00000001000c2fad PyEval_EvalFrameEx + 21405 
2 org.python.python    0x00000001000c4fb3 PyEval_EvalCodeEx + 2115 
3 org.python.python    0x00000001000c50d6 PyEval_EvalCode + 54 
4 org.python.python    0x00000001000e995e PyRun_FileExFlags + 174 
5 org.python.python    0x00000001000e9bfa PyRun_SimpleFileExFlags + 458 
6 org.python.python    0x0000000100100c0d Py_Main + 3101 
7 org.python.python    0x0000000100000f14 0x100000000 + 3860 

我工作在Mac OSX 10.9.3,而不是默認設置的Python 2.7,最新的numpy的。

+0

它看起來像你試圖解引用NULL指針。 (對不起,我不知道C API的更多具體細節。) – pts

+2

您是否記得調用'import_array()'(http://docs.scipy.org/doc/numpy/reference/c-api.array .html#import_array)在擴展模塊的初始化函數中?例如,請參閱http://docs.scipy.org/doc/numpy/user/c-info.how-to-extend.html#required-subroutine –

+0

沃倫,你無法想象我現在的感受 - 20小時後headbanging的用法和樣例: 你有權利。謝謝! –

回答

0

沃倫有權利:我忘了在模塊的初始化函數中調用import_array()。