CPython API - Py_BuildValue() 错误(退出代码 -1073741819)

CPython API - Error (exit code -1073741819) with Py_BuildValue()

我一直在尝试使用 CPython API 创建一个计算贝塞尔曲线的函数。但是,当我尝试 运行 程序时出现以下错误(而是退出代码)。一切都正确编译,这是我的代码:

static PyObject* BezierCurve_raw_bezier_curve(PyObject* self, PyObject* args){
    unsigned long long size;
    double t;
    PyObject *py_control_points, *temp1, *x, *y;

    if (!PyArg_ParseTuple(args, "dO", &t, &py_control_points))
        return NULL;

    size = PyLong_AsUnsignedLongLong(PyLong_FromSsize_t(PyList_GET_SIZE(py_control_points)));

    long long *control_points = (long long*)malloc(size * sizeof(long long) * 2);

    for (unsigned long long i = 0; i < size; i+=2){

        temp1 = PyList_GetItem(py_control_points, i);

        if (temp1 == NULL)
            return NULL;

        x = PyList_GetItem(temp1, 0);
        y = PyList_GetItem(temp1, 1);

        if (PyNumber_Check(x) != 1 || PyNumber_Check(y) != 1){
            PyErr_SetString(PyExc_TypeError, "Control Points Argument is Non-Numeric");
            return NULL;}

        control_points[i] = PyLong_AsLongLong(x);
        control_points[i + 1] = PyLong_AsLongLong(y);

        Py_DECREF(x);
        Py_DECREF(y);
        Py_DECREF(temp1);

        if (PyErr_Occurred())
            return NULL;
    }

    Py_DECREF(py_control_points);

    struct DoubleTuple2 point = raw_bezier_curve(t, size, control_points);
    printf("x: %lf, y: %lf", point.x, point.y);
    return Py_BuildValue("[dd]", point.x, point.y);
}
struct DoubleTuple2 {
    double x, y;
};

我打印了 point 结构的值,输出结果如下:

x: 185.424000, y: 167.1840000.23040000000000005

Process finished with exit code -1073741819 (0xC0000005)

为什么会出现这个错误? raw_bezier_curve 函数也返回一个 double 值。

struct DoubleTuple2 raw_bezier_curve(long double t, unsigned long long size, long long control_points[]) {...}

我怎样才能让它发挥作用?有没有办法获得更多信息的错误消息(以供将来调试)?

终于!我让它工作了。事实证明,问题很简单——我漏掉了一个逗号。为了修复它,我更改了:

return Py_BuildValue("[dd]", point.x, point.y);

return Py_BuildValue("[d, d]", point.x, point.y);