Python - 系统错误 : NULL result without error in PyObject call

标签 python c arrays numpy extending

故事:我正在尝试从 C 接口(interface)到 Python,以便对现有 Python 代码使用 C 的更快计算速度。我已经取得了一些成功,也通过了 NumPy 数组 - 但现在似乎有一个问题,我无法解决它。这是代码:

#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;
    }

现在它并没有真正做任何事情,我只是想检查我是否能够传递这些数组。正如标题所说,我收到以下错误消息:

SystemError: NULL result without error in PyObject call

这很有趣,因为我已经传递了一些数组一次(以相同的方式做..)并且通常这些数组可能是 100x100 甚至是 100x100。通常人们提示非常大的阵列..

我在 64 位机器上使用 Ubuntu 14.04,Python V2.7.6 和 Numpy 1.8.2

如果你们中有人能帮助我,那就太棒了——我不知道这里出了什么问题..

编辑:我还没有弄清楚这个问题,但有时它有效,有时它因上面的错误而崩溃。我完全不知道这可能是什么 - 有人吗?

最佳答案

最近有人在另一个帖子里给我看了答案:

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;

感谢 iharob,帮助很大!

L.

关于Python - 系统错误 : NULL result without error in PyObject call,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29058706/

相关文章:

c - EVA插件: How to check the add value in "temp = (volatile unsigned short*) add " through the temp variable

php - 根据有序数组对多维数组进行排序

c# - IEnumerable<T>.ToArray() 是如何工作的?

Python - 我读取了一个文件,但它显示了错误的值?

python - Pandas 数据帧与多个 groupby 相加

python - 使用 Beautifulsoup-Python 进行抓取

java - 使用JNA将 native C函数映射到Java接口(interface)时出现指针问题

c - 在 EOF 处终止 scanf 并忽略字符串

javascript - 带有 $http 请求的复选框 ng-click 上的 Angular JS 无限循环

python - 当代码抛出 ImportError 时如何进行单元测试?