Skip to content

BLD: only use PyObject_HEAD if PyObject isn't opaque - #20492

Merged
astrofrog merged 1 commit into
astropy:mainfrom
neutrinoceros:bld/opaque-pyobject
Sep 28, 2026
Merged

astrofrog merged 1 commit into
astropy:mainfrom
neutrinoceros:bld/opaque-pyobject

Conversation

@neutrinoceros

@neutrinoceros neutrinoceros commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Description

close #19479
ref implementation: cython/cython#7412
I got started working on abi3t-compliance, and can confirm this patch alone resolves most existing build-time errors, but it isn't sufficient in itself so I'm not going to add this target to (extra) CI yet.

AI Disclosure

N/A

  • I certify that I am human and that I take full responsibility for this pull request including all interactions with reviewers.

Merge method

  • By checking this box, the PR author has requested that maintainers do NOT use the "Squash and Merge" button. Maintainers should respect this when possible; however, the final decision is at the discretion of the maintainer that merges the PR.

@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.

  • Do the proposed changes actually accomplish desired goals?
  • Do the proposed changes follow the Astropy coding guidelines?
  • Are tests added/updated as required? If so, do they follow the Astropy testing guidelines?
  • Are docs added/updated as required? If so, do they follow the Astropy documentation guidelines?
  • Is rebase and/or squash necessary? If so, please provide the author with appropriate instructions. Also see instructions for rebase and squash.
  • Did the CI pass? If no, are the failures related? If you need to run daily and weekly cron jobs as part of the PR, please apply the "Extra CI" label. Codestyle issues can be fixed by the bot.
  • Is a change log needed? If yes, did the change log check pass? If no, add the "no-changelog-entry-needed" label. If this is a manual backport, use the "skip-changelog-checks" label unless special changelog handling is necessary.
  • Is this a big PR that makes a "What's new?" entry worthwhile and if so, is (1) a "what's new" entry included in this PR and (2) the "whatsnew-needed" label applied?
  • At the time of adding the milestone, if the milestone set requires a backport to release branch(es), apply the appropriate "backport-X.Y.x" label(s) before merge.

@neutrinoceros neutrinoceros changed the title BLD: only use PyObject_HEAD if PyObject isn't opaque BLD: only use PyObject_HEAD if PyObject isn't opaque Sep 28, 2026
@neutrinoceros
neutrinoceros marked this pull request as ready for review September 28, 2026 16:30
@pllim
pllim requested a review from astrofrog September 28, 2026 16:59

@astrofrog astrofrog left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good to me!

@astrofrog
astrofrog merged commit 51a6654 into astropy:main Sep 28, 2026
47 of 48 checks passed
@da-woods

Copy link
Copy Markdown
Contributor

This doesn't feel like it would actually work if you tried to run it to me.

Just as an example:

self = (IterParser *)alloc_func(type, 0);

The cast here is dodgy. You've allocated a Python object so that object starts with the PyObject head. So the cast to an object without the head ignores that structure. The correct line would be:

PyObject *self_obj = alloc_func(...); // as before
self = (IterParser*)PyObject_GetTypeData(self_obj, type);

(Assuming no inheritance... With inheritance type needs to be exactly IterParserType.) You'd return self_obj.

And you should be very suspicious of functions taking IterParser* since they almost certainly need to take PyObject* and then lookup the type data from that.

.basicsize = sizeof(IterParser),

The size needs to be negative in the opaque case.

@da-woods

Copy link
Copy Markdown
Contributor

If the comments above aren't sufficiently obvious then I can make a PR with one file in a working state. The other files should be pretty straightforward once you've need the pattern. Or I can elaborate.

@neutrinoceros
neutrinoceros deleted the bld/opaque-pyobject branch September 29, 2026 07:04
@neutrinoceros

Copy link
Copy Markdown
Contributor Author

Thanks @da-woods for reviewing this. Indeed I did not attempt to validate the validity of my patch at runtime, as I only focused on getting past a build-time error, but this approach seems naive in retrospect. If you have time for it, I would indeed appreciate seeing a PR from you to fix a single file; I'd feel much more confident that we can get the rest of them right without assistance then :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RFC: (optionally) treat PyObject as an opaque struct using heap types for PEP 803 compliance

3 participants