使用继承且构造函数参数名错误时Cython出现段错误
Cython继承引发段错误的问题排查
我在Cython中遇到了一系列与继承相关的奇怪段错误,以下最小示例可复现该问题:
代码文件
buffer.pxd
from libc.stdint cimport uint16_t, uint32_t from cpython.mem cimport PyMem_RawCalloc,PyMem_RawFree cdef class PixelBase: pass cdef class PixelBuffer(PixelBase): cdef: uint16_t* value cdef void allocate(self, uint32_t len) cdef void deallocate(self)
buffer.pyx
cdef class PixelBase: def __cinit__(self, uint32_t buf_len = 100): pass def __init__(self, uint32_t buf_len = 100): pass cdef class PixelBuffer(PixelBase): def __cinit__(self, uint32_t buf_len = 100): self.allocate(buf_len) def __init__(self, uint32_t buf_len = 100): super().__init__(buf_len) def __dealloc__(self): self.deallocate() cdef void allocate(self, uint32_t buf_len): self.value = <uint16_t*> PyMem_RawCalloc(1, buf_len * sizeof(uint16_t)) cdef void deallocate(self): PyMem_RawFree(self.value)
编译命令
cythonize -i buffer.pyx
测试与问题现象
使用错误的参数名进行测试:
from src.buffer import PixelBuffer buffer = PixelBuffer(wrong_parameter_name=10)
此时程序会出现段错误(无异常抛出)。若移除继承关系(PixelBuffer不继承自PixelBase),则会正常抛出异常。
我还遇到过其他与继承和属性使用相关的段错误问题,现想确认我的继承实现是否存在根本错误。请问是否有人能指出我在PixelBuffer继承PixelBase的方式中存在的根本问题?
Python版本:3.11.4,Cython版本:3.0.2
内容的提问来源于stack exchange,提问作者fkh746351
相关产品推荐
相关产品推荐

