Repository navigation
Make iterparse.c work with opaque Python objects - #20504
Conversation
This is mainly a case of: * introducing a cast macro to either do the call to PyObject_GetTypeData or do the C cast depending on the mode. * changing the non-internal functions to accept `PyObjects*` rather than the data struct. * -ve size on the type spec. I've added the `ASTROPY_OPAQUE_OBJECTS` macros which can be defined to force it to compile opaque objects mode. This is optional (i.e. you could take it out of the PR) but convenient for development. Note that you could just switch to doing the opaque object unconditionally since it'd be supported from Python 3.12 upwards. That trades a small performance benefit for a single code-path.
|
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.
|
|
When doing the rest of it That (and the other |
| * requested (as occurs with transparent decompression of the input | ||
| * stream), and for the initial allocation to combine the logic in one place. | ||
| */ | ||
| static int queue_realloc(IterParser *self, Py_ssize_t req_size) |
There was a problem hiding this comment.
These are internal functions that never treat self as a PyObject * so it's easier to pass the data struct and not a PyObject*. Hence they're unchanged.
| {Py_tp_dealloc, (destructor)IterParser_dealloc}, | ||
| {Py_tp_dealloc, IterParser_dealloc}, | ||
| {Py_tp_doc, "IterParser objects"}, | ||
| {Py_tp_traverse, (traverseproc)IterParser_traverse}, |
There was a problem hiding this comment.
Casts are no longer needed because the signature with PyObject * matches (and because they were only really needed on the static type version anyway)
But are you a human? 😆 j/k j/k Thanks! xref #20492 |
|
pre-commit.ci autofix |
|
Generic comment on how to approach this:
|
There was a problem hiding this comment.
Since there are no plans to use ASTROPY_OPAQUE_OBJECTS yet, I think it's best to omit it for now.
Otherwise, and for the most part, this seems pretty clear to me. I only have one question I want to elucidate before merge.
it's all pretty mechanical so AI would probably do pretty well at the follow up PRs
I object to AI use in any shape or form. In this particular instance I think using AI would deprive us from the opportunity to learn about these details.
While my position isn't that of the project as a whole, I'll make a point not to use any LLM in continuing this work.
| .basicsize = sizeof(IterParser), | ||
| .basicsize = | ||
| #if defined(Py_TARGET_ABI3T) || defined(ASTROPY_OPAQUE_OBJECTS) | ||
| -(Py_ssize_t) // negative size indicates opaque PyObject |
There was a problem hiding this comment.
did you mean PY_SSIZE_T_MAX here ?
I don't understand how Py_ssize_t, which is a type, would be interpreted as a value.
There was a problem hiding this comment.
No - it's casting the line afterwards (to avoid a c compile warning). Maybe that isn't written super-clearly but it's it evaluates as
.basic size = -(Py_ssize_t)sizeof(IterParser),
It might be clearer to have a bit more duplication and not put the ifdef guards in the middle of a line...
There was a problem hiding this comment.
oh, I see. This is the "review before coffee" effect at work :)
#ifdef are never particularly easy to read in my opinion so I'm actually happy to keep it as is. Do feel free to push a different version if you find one that feels easier to understand though.
Just to clarify the intent of this: it's so you can force this module to be compiled with opaque objects to test it when developing it. So the intention is that it never gets used in a release (which will ultimately be governed by Completely happy to remove it though. |
I understand, and I actually like the idea, though we'd need to document it in some way and I'm not sure it's worth the effort yet as it might get no use at all if I'm the only one working on this and I forget about it (which is pretty likely). I might re-introduce it later if I change my mind though. |
Description
This is mainly a case of:
PyObjects*rather than the data struct.I've added the
ASTROPY_OPAQUE_OBJECTSmacros which can be defined to force it to compile opaque objects mode. This is optional (i.e. you could take it out of the PR)but convenient for development. Note that you could just switch to doing the opaque object unconditionally since it'd be supported from Python 3.12 upwards. That trades a small performance benefit for a single code-path.
Part of #19479
AI Disclosure
No AI. Just a note that it's all pretty mechanical so AI would probably do pretty well at the follow up PRsns with reviewers.
Merge method