On 24. 05. 22 15:43, Stepan Sindelar wrote:
Hello,
coincidentally, this is similar to the design that HPy chose. Everything is opaque by design in HPy and so in "pure" HPy mode tp_basicsize is treated as "I need this much native space attached to objects of this type". Internally it does not even have to be laid out in memory after the object, it can be elsewhere, which can have some advantages for alternative Python implementations. There is also API to access the memory: void* HPy_AsStruct(hpy_context, object_handle).
Regarding:
- a new
void* PyObject_GetTypeData(PyObject *obj, PyTypeObject *cls)
returns the pointer to data specific tocls
.We haven't decided yet in HPy how to deal with multiple user defined base types with different struct attached to each of them. I think that passing the class object is reasonable, but note that getting the required PyTypeObject may then require loading it from module state, which means some extra API call and also the need to pass extra argument(s) around. Alternative could be also some other "token" that can be safely shared between subinterpreters (some ID, address of the corresponding PyType_Spec, ...), because that is all the implementation will need: just distinguish which class' data the caller wants. Open question is also how important this use case is. So far in our porting experiments (kiwi, matplotlib, ultrajson, subset of numpy) we did not need that.
I hope that with PyCMethod's defining_class, getting the class should be easy in sufficiently many cases. Definitely not all of them, of course.
Note that we also need to deal with alignments if the structs are not embedded in each other, but we want to lay them next to each other in the memory. So far HPy is using a union for this: https://github.com/hpyproject/hpy/blob/master/hpy/devel/include/hpy/runtime/..., but that does not scale.
Yeah, that's definitely a detail with a devil inside :)
Since CPython is on C11 now, we can use a compiler-provided version of
that union, max_align_t
.
I think that from the HPy point of view it is a worthwhile idea and something that HPy could piggy back on instead of hacking around the current API.
Stepan