使用C扩展Python,numpy是代码好吗?

时间:2014-06-17 18:29:00

标签: python c macos numpy

这是我第一次尝试扩展Python函数。 在Python中,我有4维浮点列表:

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)

这是我的代码;在我将一行放入解析列表之前,它工作正常。 我不知道:我的环境不好还是这部分代码错了?

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。

1 个答案:

答案 0 :(得分:0)

Warren有权:我忘了在模块的初始化函数中调用import_array()。