Skip to content

RFC: treat PyTypeObject as an opaque struct for Limited API compliance#157

Merged
brandon-rhodes merged 1 commit intobrandon-rhodes:masterfrom
neutrinoceros:rfc/tp_alloc
Apr 30, 2026
Merged

RFC: treat PyTypeObject as an opaque struct for Limited API compliance#157
brandon-rhodes merged 1 commit intobrandon-rhodes:masterfrom
neutrinoceros:rfc/tp_alloc

Conversation

@neutrinoceros
Copy link
Copy Markdown
Contributor

@neutrinoceros neutrinoceros commented Mar 11, 2026

needed for #151
based off #153

This one doesn't compile yet, but the good news is that the errors are identical wether I force abi3 or not.

extension/wrapper.cpp:124:15: error: cannot initialize a variable of type 'allocfunc' (aka '_object *(*)(_typeobject *, long)') with an rvalue of type 'void *'
  124 |     allocfunc alloc_func = PyType_GetSlot(cls, Py_tp_alloc);
      |               ^            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
extension/wrapper.cpp:514:15: error: cannot initialize a variable of type 'allocfunc' (aka '_object *(*)(_typeobject *, long)') with an rvalue of type 'void *'
  514 |     allocfunc alloc_func = PyType_GetSlot(type, Py_tp_alloc);
      |               ^            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 errors generated.

@neutrinoceros
Copy link
Copy Markdown
Contributor Author

The only difference I can see between this broken code and the known-working examples I have in astropy is that this module is C++ while the ones I can compile are C... if that's the meaningful difference I may be out of my depth, as I cannot think of a single reason why that would matter.

@neutrinoceros
Copy link
Copy Markdown
Contributor Author

found inspiration from this stack overflow thread. It compiles okay if I use type casting. Maybe C compilers just do the exact same thing implicitly and I never knew it was needed ?

Copy link
Copy Markdown
Owner

@brandon-rhodes brandon-rhodes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I only spotted one naming tweak, then we can get this merged!

Comment thread extension/wrapper.cpp Outdated
@brandon-rhodes brandon-rhodes merged commit b38ccb5 into brandon-rhodes:master Apr 30, 2026
21 checks passed
@neutrinoceros neutrinoceros deleted the rfc/tp_alloc branch April 30, 2026 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants