Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - SystemError: NULL result without error in PyObject call

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?

like image 587
rammelmueller Avatar asked Mar 15 '15 08:03

rammelmueller


1 Answers

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.

like image 53
rammelmueller Avatar answered Nov 18 '22 01:11

rammelmueller