Python的 - 关于SystemError:无的PyObject调用错误NULL结果 [英] Python - SystemError: NULL result without error in PyObject call

查看:3907
本文介绍了Python的 - 关于SystemError:无的PyObject调用错误NULL结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

故事:我想从C到Python,以便使用C的更快的运算速度为现有的Python code接口。我已经取得了一些成功,还通过numpy的阵列 - 但现在似乎有一个问题,我解决不了它。这是code:

 的#define FORMAT_VALUE_TD
烧焦format_buffer [32];typedef结构
    {
        PyObject_HEAD
        PyArrayObject * invmat;
        无符号秩序;
        value_t重量,*缓冲区;
    }挪威;    双的typedef value_t;    typedef结构
    {
        PyObject_HEAD
        DET DET *;
        value_t *行* covs,比,星;
    } DetAppendMove;    静态INT append_init(DetAppendMove *自我,*的PyObject ARGS,*的PyObject kwds)
    {
        value_t星,*温度;
        PyArrayObject *行*关口;
        的PyObject *结果= Py_BuildValue((一),1);
        DET * DETE;        的snprintf(format_buffer,sizeof的(format_buffer),%s%S,O O O!!!,FORMAT_VALUE_T);
        如果(PyArg_ParseTuple(参数,format_buffer,&安培; DetType,&安培; DETE,&安培; PyArray_Type,&安培;排,和放大器; PyArray_Type,&安培;山坳,&安培;星))
        {
            自> DET = DETE;
            TEMP =(value_t *)自> det->缓冲区;
        }
        其他
        {
            结果= Py_BuildValue((一), - 1);
        }
        返回结果;
    }

这不是真的现在做什么,我只是想检查我是否能够通过这些arrays.As标题说,我得到了以下错误消息:


  

关于SystemError:NULL结果没有错误的PyObject通话


这是有趣的,因为我已经通过某些阵列一次(做了同样的方式。),通常这些阵列是100x100的可能,如果连。通常人们抱怨非常大的阵列。

我是一个64位的机器,巨蟒V2.7.6和numpy的1.8.2上使用Ubuntu 14.04

这将是真棒,如果你们中的一个能帮助我 - 我不知道发生了什么乱子这里..

编辑:?我没弄明白这个问题还没有,但有时它的工作原理,有时它与错误崩溃从上面。我绝对没有什么线索,这可能是 - 任何人


解决方案

最近有人给我在另一篇文章的回答:


  

当您从曝光的C函数返回NULL到Python则必须设置
  之前的错误信息,因为返回NULL表示错误
  发生了。


  
  

如果一个错误发生了,你是因为在返回NULL,那么,
  使用PyErr_SetString(),如果没有发生错误,然后用

  Py_RETURN_NONE;


由于iharob,帮助了很多!

升。

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?

解决方案

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.

这篇关于Python的 - 关于SystemError:无的PyObject调用错误NULL结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆