2014-06-17 49 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()。