Cython 中结构的错误 return 值?

Error return values for struct in Cython?

我们可以在 Cython 中执行此操作,以便每当 return 值为 -1 时,Cython 都会生成对 PyErr_Occurred():

的调用
cdef int spam() except?-1:

但是,如果 return 类型是一个结构呢?

ctypedef struct A:
    double x
    double y

cdef A spam() except ?????

比如说,我想将错误值定义为:A(x=1, y=-1)。可以吗?

根据文档,这可能是不可能的。

可以在 cython 的文档

Error return values 部分末尾找到此说明

Exception values can only declared for functions returning an integer, enum, float or pointer type, and the value must be a constant expression. Void functions can only use the except * form.

你不能指定一个错误值,但是你可以通过写

让错误传播
cdef A spam() except *:
    [code that may raise exceptions]

这将隐式使用 PyErr_Occurred 宏而不是 return 值来确定是否发生错误。