在多个模块中使用函数类型时出现错误签名错误

2024-10-08 18:29:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下Cython模块:

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

integratev.pxd中,我声明:

^{pr2}$

我从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中而不是使用多个模块时,它是有效的。在


Tags: 模块声明maintypeargsintdoubleintegrate
1条回答
网友
1楼 · 发布于 2024-10-08 18:29:03

解决方案是将所有内容作为void *传递,并在函数实际执行之前,在trapz2d()内部将其强制转换为<f_type>。代码的最终布局是:

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

cdef int trapz2d(void *fin, 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)
    cdef f_type f
    f = <f_type>fin
    ...

在另一个代码中:

^{pr2}$

相关问题 更多 >

    热门问题