【发布时间】:2021-03-11 18:15:43
【问题描述】:
我正在尝试在 CPython 中创建一个自定义类型,该类型继承自 Python 中已定义的类型对象。到目前为止,我的方法是使用PyImport_ImportModule,然后访问PyTypeObject 并将其设置为PyTypeObject 中的tp_base 属性。
进口:
PyTypeObject typeObj = {...} // Previously defined PyTypeObject for a fallback
int init() {
PyObject* obj = PyImport_ImportModule("<absolute_path_to_python_module>");
if (obj && PyObject_HasString(obj, "<python_type_object>")) {
PyTypeObject* type_ptr = (PyTypeObject*) PyObject_GetAttrString(obj, "<python_type_object>");
typeObj = *type_ptr;
}
if (PyType_Ready(&typeObj) < 0) return -1;
... // Other initialization stuff
}
继承:
PyTypeObject CustomType = {
... // Initialization stuff
.tp_base = &typeObj;
};
自定义类型能够继承函数,但在isInstance(CustomType(), TypeObj) 上失败。当我尝试访问自定义类型的__bases__ 属性时,会引发分段错误。
【问题讨论】:
-
非堆类型(大多数 C 类)并非旨在从堆类型(用 Python 编写的类型,加上一些奇怪的 C 类)继承。
-
另外,
typeObj = *type_ptr;正在做一些完全不安全和不受支持的事情——你不能像那样复制 any Python 对象。 -
所以我想在堆上分配内存,即使用
malloc?
标签: python c inheritance cpython