gpt4 book ai didi

python - 在多个模块中使用函数类型的错误签名错误

转载 作者:行者123 更新时间:2023-12-04 16:49:51 25 4
gpt4 key购买 nike

我有以下 Cython 模块:

compmech    
integrate
integratev.pxd
integratev.pyx
conecyl
main.pyx

integratev.pxd 中我声明:

ctypedef void (*f_type)(int npts, double *xs, double *ts, double *out,
double *alphas, double *betas, void *args) nogil

cdef int trapz2d(f_type f, int fdim, np.ndarray[cDOUBLE, ndim=1] final_out,
double xmin, double xmax, int m,
double ymin, double ymax, int n,
void *args, int num_cores)

我从main.pyx调用trapz2d,传递给trapz2d的函数在main.pyx中声明>,例如:

from compmech.integrate.integratev cimport trapz2d

cdef void cfk0L(int npts, double *xs, double *ts, double *out,
double *alphas, double *betas, void *args) nogil:
...

trapz2d(<f_type>cfk0L, fdim, k0Lv, xa, xb, nx, ta, tb, nt, &args, num_cores)

它编译得很好,但是当我运行时出现错误:

TypeError: C function compmech.integrate.integratev.trapz2d has wrong signature       
(expected int (__pyx_t_8compmech_9integrate_10integratev_f_type, int, PyArrayObject *,
double, double, int, double, double, int, void *, int),
got int (__pyx_t_10integratev_f_type, int, PyArrayObject *,
double, double, int, double, double, int, void *, int))

这对我来说似乎是一个错误,但也许我在这里遗漏了一些重要的东西......


注意:当我将所有内容放入 main.pyx 而不是使用多个模块时,它会起作用。

最佳答案

这确实看起来像是一个错误,自动尝试合并 filename.pxdfilename.pyx 中的类型失败。使用 void* 是一种解决方法,但如果您想避免转换,您可以创建第三个文件 _util.pxd,其中包含函数指针的共享定义。

# _util.pxd

ctypedef void (*f_type)(int npts, double *xs, double *ts, double *out,
double *alphas, double *betas, void *args) nogil

然后您可以将共享的 typedef 导入到 integrate.pxdintegrate.pyx 中,这保证了类型相同。

关于python - 在多个模块中使用函数类型的错误签名错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22845057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com