Skip to content

RFC: restrict cosmology extension modules to CPython's Limited API - #18261

Merged
mhvk merged 1 commit into
astropy:mainfrom
neutrinoceros:cosmology/rfc/limited-api-compat
Jul 7, 2025
Merged

mhvk merged 1 commit into
astropy:mainfrom
neutrinoceros:cosmology/rfc/limited-api-compat

Conversation

@neutrinoceros

Copy link
Copy Markdown
Contributor

Description

Ref #18163
This should be the last (non-infrastructural) missing piece (!)

  • 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.

@github-actions

Copy link
Copy Markdown
Contributor

👋 Thank you for your draft pull request! Do you know that you can use [ci skip] or [skip ci] in your commit messages to skip running continuous integration tests until you are ready?

@neutrinoceros

Copy link
Copy Markdown
Contributor Author

Build seems completely broken on CI but somehow works on my machine, talk about a disapointment... let me see what's gone wrong

@neutrinoceros

Copy link
Copy Markdown
Contributor Author

Looks like I broke runtime when I fixed build time 🙈
Will come back to this tomorrow

Py_INCREF(self);
return self;
}
return PyMethod_New(self, obj);

@da-woods da-woods Jun 11, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It is possible to replace this. I'd use Cython's implementation as a starting point https://github.com/cython/cython/blob/dba606bc50e90dbaf37850779d1f84f1c0a22c7a/Cython/Utility/ObjectHandling.c#L2977

Essentially we look-up the method type in types.MethodType, cache it (for speed) and then call it as a Python object.


I think you do need to provide this code, however you really don't want this code to actually be called - it's very slow.

What you want to do (in addition) is to set the Py_TPFLAGS_METHOD_DESCRIPTOR type-flag on the type. This means that Python can usually avoid going through the __get__ code. I don't think you'll have to change your call code at all - just set the flag.


The Limited API code is probably bad enough here to be worth keeping the old version and guarding it with #if !defined(Py_LIMITED_API)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Cython does some opaque stuff in its utility code that I think you're struggling to see through. I can give more detailed instructions (i.e. the exact code where it needs to be) later today if that helps.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes, I was just about to ask actually. That would be tremendously helpful, thank you !

@nstarman nstarman 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.

I don't know enough C to meaningfully review this. But I trust y'all 😰 😆

Comment thread .github/workflows/ci_workflows.yml
@neutrinoceros
neutrinoceros force-pushed the cosmology/rfc/limited-api-compat branch 2 times, most recently from 0a9d98a to f1cb13b Compare June 12, 2025 08:55

@da-woods da-woods left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggestions aren't tested so there may be small typos, but hopefully should be very close to working.

Comment thread astropy/cosmology/_src/signature_deprecations.c Outdated
Comment thread astropy/cosmology/_src/signature_deprecations.c Outdated
Comment thread astropy/cosmology/_src/signature_deprecations.c Outdated
Comment thread astropy/cosmology/_src/signature_deprecations.c Outdated
Comment thread astropy/cosmology/_src/signature_deprecations.c Outdated
Comment thread astropy/cosmology/_src/signature_deprecations.c Outdated
Comment thread astropy/cosmology/_src/signature_deprecations.c Outdated
@neutrinoceros
neutrinoceros force-pushed the cosmology/rfc/limited-api-compat branch 2 times, most recently from e87f8c8 to 9e4da2d Compare June 12, 2025 18:14
@neutrinoceros

Copy link
Copy Markdown
Contributor Author

Fantastic, thanks a lot @da-woods, I couldn't have done this without your help !
I included all your suggestions as is (no typos whatsoever) and squashed my commits back, this time putting you as a co-author. It should be stable now but I'll wait for CI to go green before I actually undraft

@neutrinoceros
neutrinoceros marked this pull request as ready for review June 12, 2025 18:32
@neutrinoceros
neutrinoceros requested a review from a team as a code owner June 12, 2025 18:32
@neutrinoceros
neutrinoceros requested a review from nstarman June 12, 2025 18:32

@nstarman nstarman 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.

👍

@neutrinoceros

Copy link
Copy Markdown
Contributor Author

@nstarman do you want to formally approve this one, or would you like a second opinion ?

@nstarman

nstarman commented Jul 2, 2025

Copy link
Copy Markdown
Member

2nd opinion plz.

@neutrinoceros

Copy link
Copy Markdown
Contributor Author

Who can we ask a review from ? I think David has more expertise there than most (all ?) of us (combined ? 😅)

@da-woods da-woods left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

One change and a couple of things that might be fixed as follow ups. But it looks good to me. Although I'm not the most impartial on this.

Comment thread astropy/cosmology/_src/signature_deprecations.c

PyObject *since_unicode = PyObject_Str(self->since);
const char* since_utf8 = PyUnicode_AsUTF8(since_unicode);
const char* since_utf8 = PyUnicode_AsUTF8AndSize(since_unicode, NULL);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Propably something for a follow-up PR rather than to fix here, but I think that you could save a lot of this by using PyErr_WarnFormat later. (Unless I'm missing something which is completely possible...)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This module was my very first experience using Python's C-API and isn't really polished in any way. In any case it's also temporary code that we'll soon get rid of anyway, so it doesn't matter much if the slow path is sub-optimal.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Fair enough - that seems reasonable to leave as is then

{Py_tp_members, depr_kws_wrap_members},
{Py_tp_call, (ternaryfunc)depr_kws_wrap_call},
{Py_tp_descr_get, depr_kws_wrap_get},
{0, NULL},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Again, something for a follow-up PR since it's not a new problem, but this type looks like it needs GC.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Do you mean I should set Py_TPFLAGS_HAVE_GC (and abide the contract that comes with it) ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes I think so. It looks like the type has a dict and I think wrapped is an arbitrary Python callable, so I think it can participate in reference loops.

This doesn't change with the Limited API though, so personally I'd do it separately.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fine with me. Thanks for the tip !

@neutrinoceros
neutrinoceros force-pushed the cosmology/rfc/limited-api-compat branch from 3ea6406 to 2476a79 Compare July 2, 2025 19:41

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thought I might as well look at this one too - another possible wrong DECREF, but otherwise would seem to make sense.

@neutrinoceros

Copy link
Copy Markdown
Contributor Author

@mhvk I'm not seeing the specific comment you're alluding to. Did you forget to submit it ?

Comment thread astropy/cosmology/_src/signature_deprecations.c
@neutrinoceros
neutrinoceros force-pushed the cosmology/rfc/limited-api-compat branch from 2476a79 to cb04af7 Compare July 4, 2025 15:41

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approving since my one comment turned out to be based on a misunderstanding.

@github-project-automation github-project-automation Bot moved this from Triage to Reviewer approved in Cosmology, the Expansion Jul 7, 2025
@mhvk
mhvk merged commit 70d7f62 into astropy:main Jul 7, 2025
@github-project-automation github-project-automation Bot moved this from Reviewer approved to Done in Cosmology, the Expansion Jul 7, 2025
@neutrinoceros
neutrinoceros deleted the cosmology/rfc/limited-api-compat branch July 7, 2025 11:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants