Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PyHeap_GetType() to limited C API to get the value of ht_type from heap types using PyObject pointer. #128351

Closed
AraHaan opened this issue Dec 30, 2024 · 7 comments

Comments

@AraHaan
Copy link
Contributor

AraHaan commented Dec 30, 2024

Feature or enhancement

Proposal:

Currently on the Limited C API one can use PyType_FromSpec() using a spec for a type to obtain a special heap type object. However, in order to then call PyModule_AddObject to bind the resulting type object created from this to the module one must first access the PyHeapTypeObject structure which is sadly not defined at all, nor any of it's members when using the limited C API.

The only workaround for this would be a new PyHeap_GetType() to obtain the value (PyTypeObject *) from the input PyHeapTypeObject as a PyObject * without first casting the result from PyType_FromSpec to PyHeapTypeObject. The result is then better usage in C extensions wanting to use the limited API but cannot due to this limitation.

Alternatives as well:

  • A copy of PyType_FromSpec for the limited API that takes in the module object (PyObject *) and calls the PyModule_AddObject function for the user so they never need to worry about heap type object stuffs.

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

I seen discussions about allowing PyHeapTypeObject in C extensions on GitHub in the past, but no idea how far the progress is currently on that. I think having PyHeap_GetType or whatever name of a function it could be and/or a function that internally calls PyType_FromSpec AND PyModule_AddObject for C extension developers exposed to the C API could work as well.

See also: #105970 (there could be more)

@AraHaan AraHaan added the type-feature A feature request or enhancement label Dec 30, 2024
@AraHaan AraHaan changed the title Add PyHeap_GetType() for limited C API to get the value of ht_type from heap types using PyObject pointer. Add PyHeap_GetType() to limited C API to get the value of ht_type from heap types using PyObject pointer. Dec 30, 2024
@godlygeek
Copy link
Contributor

in order to then call PyModule_AddObject to bind the resulting type object created from this to the module one must first access the PyHeapTypeObject structure

There's no need for that. You can use the return value from PyType_FromSpec directly as the value argument for PyModule_AddObject. The return value from PyType_FromSpec is the newly created class that you want to add to the module's namespace.

@AraHaan
Copy link
Contributor Author

AraHaan commented Dec 31, 2024

in order to then call PyModule_AddObject to bind the resulting type object created from this to the module one must first access the PyHeapTypeObject structure

There's no need for that. You can use the return value from PyType_FromSpec directly as the value argument for PyModule_AddObject. The return value from PyType_FromSpec is the newly created class that you want to add to the module's namespace.

When building and testing my updated code, I normally use PyVectorcall_Function to then test if the callable supports vectorcall and then use PyVectorcall_Call if it does, otherwise I fallback to using PyObject_Call both for calling normal functions, and for constructing instances of said heap types.

What I discovered is that somehow my code caused the interpreter to crash within the inline function for PyVectorcall_Function that checks if the vectorcall flag is provided on the type.

@picnixz
Copy link
Contributor

picnixz commented Dec 31, 2024

What I discovered is that somehow my code caused the interpreter to crash within the inline function for PyVectorcall_Function that checks if the vectorcall flag is provided on the type.

Can you show us your code?

@picnixz picnixz added the pending The issue will be closed if no feedback is provided label Dec 31, 2024
@AraHaan
Copy link
Contributor Author

AraHaan commented Jan 3, 2025

I can share it privately if possible.

@picnixz
Copy link
Contributor

picnixz commented Jan 3, 2025

You can invite me to a private repo where you have a reproducer. Otherwise, can you give us a reproducer which is like your private code but without the private parts (namely just show us how you declared your types and registered them). I don't see a connection with vector call functions. Are you sure the crash doesn't happen due to other reasons (like a dereferenced pointer being NULL)?

I don't think we need to expose such API and I would still think we need a more specific use case and example of usage (and/or how a simple cuatom type implementing vectorcalls would crash).

@picnixz
Copy link
Contributor

picnixz commented Jan 3, 2025

cc @ZeroIntensity @encukou @vstinner

@erlend-aasland
Copy link
Contributor

in order to then call PyModule_AddObject to bind the resulting type object created from this to the module one must first access the PyHeapTypeObject structure

There's no need for that. You can use the return value from PyType_FromSpec directly as the value argument for PyModule_AddObject. The return value from PyType_FromSpec is the newly created class that you want to add to the module's namespace.

+1. Closing as not-a-bug. If you need help with the C APIs, please see the docs or create a topic on Discourse.

@erlend-aasland erlend-aasland closed this as not planned Won't fix, can't repro, duplicate, stale Jan 3, 2025
@picnixz picnixz removed type-feature A feature request or enhancement pending The issue will be closed if no feedback is provided labels Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants