【问题标题】:Adding parameters to cpdef func definition in derived cython class在派生的 cython 类中将参数添加到 cpdef func 定义
【发布时间】:2014-04-26 02:37:22
【问题描述】:

我正在尝试更改派生类中 cpdef 方法的方法签名,但不断出错。

在 *.pxd 中:

from libc.stdint cimport uint64_t

cdef struct _ft_device_list_info_node_os:
        uint64_t ftHandle
ctypedef _ft_device_list_info_node_os FT_DEVICE_LIST_INFO_NODE_OS

cdef class BaseClass(object):
    cpdef object do_something(BaseClass self)

cdef class DerivedClass(BaseClass):
    cdef FT_DEVICE_LIST_INFO_NODE_OS cheese

在 *.pyx 中:

cdef class BaseClass(object):

    cpdef object do_something(BaseClass self):
        pass

cdef class DerivedClass(BaseClass):

    cpdef object do_something(DerivedClass self, int val=10):
        cdef FT_DEVICE_LIST_INFO_NODE_OS node
        self.cheese = node

编译时出现以下错误:

cythoning test.pyx to pybarst\test.cpp

Error compiling Cython file:
------------------------------------------------------------
...

cdef class DerivedClass(BaseClass):

    cpdef object do_something(DerivedClass self, int val=10):
        cdef FT_DEVICE_LIST_INFO_NODE_OS node
        self.cheese = node
                         ^
------------------------------------------------------------

test.pyx:10:26: Cannot convert 'FT_DEVICE_LIST_INFO_NODE_OS' to Python object

如果结构仅使用整数或更简单的数据类型定义,则似乎不会发生此错误,即如果我这样做int ftHandle,则不会发生错误。

它似乎在派生类的do_something 方法中感到困惑,它认为它是基类,因此找不到cheese

是否有在派生类中添加参数的正确方法。

【问题讨论】:

  • 您确定 Cython 正在正确读取您的 .pxd 文件吗? `FT_DEVICE_LIST_INFO_NODE_OS` 定义在哪里?使用正确的 .h 和 .pxd 定义它对我有用(即我得到了bla.pyx:10:26: local variable 'node' referenced before assignment
  • 是的,该结构需要具有某些类型才能发生这种情况。即使您直接在 cython 中定义它也会发生。查看我的编辑。
  • 在你问之前,我会from libc.stdint cimport uint64_t :)
  • 我想我需要一个 SSCCE。我无法重现该问题。
  • 我不知道该说什么。是的,在他们使用其他代码之前,但是当我将它移动到他们自己的文件中时仍然会发生错误。我编辑了帖子以反映这一点,但我没有添加任何新信息。

标签: python cython


【解决方案1】:

原来这是他们用this 修复的错误。见here

【讨论】:

    猜你喜欢
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    相关资源
    最近更新 更多