我正在尝试将现有的C Python扩展程序移植以使用 py_limited_api
,这样我们就不必为我们支持的所有Python版本构建扩展程序。
我面临的问题之一是,该库提供了一种类型,该类型属于Python float
类型。
最后的官方文档中带有示例()没有提及有限的API,我面临的问题是, pyfloatObject
不是属于稳定的ABI,这是创建基本结构的必要
typedef struct {
PyFloatObject base;
int state;
} SubclassedFloat;
条件:使用 py_limited_api
,尤其是 float
,还是不可能?
I am trying to port an existing C Python extension to use the Py_LIMITED_API
, so that we don't have to build the extension for all the python versions we support.
One of the issues I'm facing is, that this library provides a type that subclasses the python float
type.
The last official documentation with examples ( https://docs.python.org/3.5/extending/newtypes.html#subclassing-other-types) does not mention the limited API, and I'm facing the problem, that PyFloatObject
is not part of the stable ABI, which would be necessary to create the base struct:
typedef struct {
PyFloatObject base;
int state;
} SubclassedFloat;
Is there a way to subclass a builtin type when using Py_LIMITED_API
, especially float
, or is this impossible?
发布评论
评论(1)
这在python c extensions Cornord上得到了回答:
基本上,您必须从
pyfloat_type
在运行时获取__基础化__
键入属性,以声明您的对象大小:每当您访问对象时,您都必须偏移
pyobject*<
pyobject*<<<<<<<<< /代码>此大小的指针:
This was answered on the Python c-extensions discord:
https://discord.com/channels/267624335836053506/728390945384431688/981133468652154920
Basically, you have to fetch the
__basicsize__
type attribute fromPyFloat_Type
at runtime to declare your object size:Whenever you access your object, you'll have to offset the
PyObject*
pointer for this size: