Python - SystemError: NULL result without error in PyObject call

别等时光非礼了梦想. 提交于 2019-12-23 09:36:58

问题


The story: I'm trying to interface from C to Python in order to use the faster computational speed of C for an existing Python code. I already had some success, also with passing NumPy arrays - but now there seems to be an issue and I can't resolve it. This is the code:

#define FORMAT_VALUE_T  "d"
char format_buffer[32];

typedef struct
    {
        PyObject_HEAD
        PyArrayObject *invmat;
        unsigned order;
        value_t weight, *buffer;
    } Det;

    typedef double value_t;

    typedef struct
    {
        PyObject_HEAD
        Det *det;
        value_t *row, *covs, ratio, star;
    } DetAppendMove;

    static int append_init(DetAppendMove *self, PyObject *args, PyObject *kwds)
    {
        value_t star, *temp;
        PyArrayObject *row, *col;
        PyObject *result = Py_BuildValue("(i)",1);
        Det *dete;

        snprintf(format_buffer, sizeof(format_buffer), "%s%s", "O!O!O!", FORMAT_VALUE_T);
        if (PyArg_ParseTuple(args, format_buffer, &DetType, &dete, &PyArray_Type, &row, &PyArray_Type, &col, &star))
        {   
            self->det = dete;
            temp = (value_t*)self->det->buffer;
        }
        else
        {
            result = Py_BuildValue("(i)",-1);
        }
        return result;
    }

It's not really doing anything by now, I just wanted to check if I'm able to pass those arrays.As the title says, I'm getting the following error message:

SystemError: NULL result without error in PyObject call

This is interesting, since I already passed some arrays once (did it the same way..) and usually these arrays are maybe 100x100 if even. Usually people complained about very large arrays..

I'm using Ubuntu 14.04 on a 64Bit machine, Python V2.7.6 and Numpy 1.8.2

It would be awesome if one of you could help me - I have no idea what's gone wrong here..

EDIT: I didn't figure out the issue yet, but sometimes it works, sometimes it crashes with the error from above.. I have absolutely no clue what this could be - anybody?


回答1:


Recently someone showed me the answer in another post:

When you return NULL from a c function exposed to python you must set the error message before, since returning NULL means an error happened.

If an error happened and you are returning NULL because of that then, use PyErr_SetString(), if no error happened, then use

Py_RETURN_NONE;

Thanks iharob, helped a lot!

L.



来源:https://stackoverflow.com/questions/29058706/python-systemerror-null-result-without-error-in-pyobject-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!