python运行无结果,Python的 - 关于SystemError:无的PyObject调用错误NULL结果

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值