-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
gh-75459: Doc: C API: Improve object life cycle documentation #125962
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
base: main
Are you sure you want to change the base?
Conversation
I'm not familiar enough with CPython's internals to be super confident about these changes. I would appreciate it if a GC expert would carefully review this. Thanks! |
Hmm, I'm not sure if we can require graphviz for the docs. We'd have to consider installing it on the main docs server in addition to Read the Docs, and also make sure the docs can still build without it, for downstream redistributors who might only want to build with "vanilla" Sphinx and no extra extensions. Plus other developers would need an easy way to build the docs on their machines. cc @AA-Turner |
be22691
to
d804c6c
Compare
Maybe I should just commit the generated |
* Add "cyclic isolate" to the glossary. * Add a new "Object Life Cycle" page. * Illustrate the order of life cycle functions. * Document `PyObject_CallFinalizer` and `PyObject_CallFinalizerFromDealloc`. * `PyObject_Init` does not call `tp_init`. * `PyObject_New`: * also initializes the memory * does not call `tp_alloc`, `tp_new`, or `tp_init` * should not be used for GC-enabled objects * memory must be freed by `PyObject_Free` * `PyObject_GC_New` memory must be freed by `PyObject_GC_Del`. * Warn that garbage collector functions can be called from any thread. * `tp_finalize` and `tp_clear`: * Only called when there's a cyclic isolate. * Only one object in the cyclic isolate is finalized/cleared at a time. * Clearly warn that they might not be called. * They can optionally be manually called from `tp_dealloc` (via `PyObject_CallFinalizerFromDealloc` in the case of `tp_finalize`). * `tp_finalize`: * Reference `object.__del__`. * The finalizer can resurrect the object. * Suggest `PyErr_GetRaisedException` and `PyErr_SetRaisedException` instead of the deprecated `PyErr_Fetch` and `PyErr_Restore` functions. * Add links to `PyErr_GetRaisedException` and `PyErr_SetRaisedException`. * Suggest using `PyErr_WriteUnraisable` if an exception is raised during finalization. * Rename the example function from `local_finalize` to `foo_finalize` for consistency with the `tp_dealloc` documentation and as a hint that the name isn't special. * Minor wording and sylistic tweaks. * Warn that `tp_finalize` can be called during shutdown.
I committed the generated |
I think that requiring I would want to include a NEWS entry to say that A |
My main concern with documenting nitty-gritty details of the lifecycle is that we're technically documenting implementation details, which are subject to change (and we've been bad at updating these kind of things from version-to-version in past). I suggest the SVG go into the InternalDocs folder instead. It's also worth noting here that |
This reverts commit 361eaca.
I don't want to document any implementation details here, so I'm happy to remove what isn't necessary. It's hard to tell what is and isn't necessary because the end of an object's life is especially fraught with peril. I think that it is better to err on the side of over-documenting this topic than under-documenting. I wrote this PR because there were several things that I needed to know that the existing documentation didn't make clear:
If I understand correctly,
I thought that was already sufficiently explained, even before this PR. Can you explain what you think is lacking? |
First, thanks for doing this!
I don't, that limits our ability to modify the lifecycle in the future (especially because there's no good way to deprecate things here). I'll point this out when doing a more in-depth review though, I don't see anything particularly bad right now.
You're right, it's not, but I don't think we should limit ourselves to that in the future. It might be possible someday to automatically do this for untracked types as well. We should just document that all types, even GC, require
Basically, it's not documented which types need to have a Also, I don't think it should be documented that A few other notes:
|
I did a major rewrite to hopefully address all of the feedback (thanks for reviewing!). Please take another look. |
I'll start the second pass today, but it will definitely take a few days for me to finish the whole review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay.
I've not been able to go through the entire PR, but I'm sending the comments I already wrote. I hope they make sense.
I'll make another pass later.
Doc/c-api/lifecycle.rst
Outdated
*resurrected*, preventing its pending destruction. (Only | ||
:c:member:`!tp_finalize` is allowed to resurrect an object; | ||
:c:member:`~PyTypeObject.tp_clear` and | ||
:c:member:`~PyTypeObject.tp_dealloc` cannot.) Resurrecting an object may |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would make the text contradictory: if tp_finalize
may resurrect an object but tp_dealloc
may not, then tp_dealloc
may call PyObject_CallFinalizerFromDealloc
(which calls tp_finalize
).
AFAIK, any of these may resurrect the object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think tp_clear
can resurrect, can it? tp_clear
and tp_dealloc
should be resurrecting via the finalizer, but if tp_clear
is called, then tp_finalize
should have been called by the GC anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for dropping the ball on my part--we're getting to a point where I'm willing to put the first checkmark on it!
I've noticed that a lot of information is repeated. It's good to be clear, but redudancy can be a bit frustrating on our end, because we need to update more places if we want to change something.
@rhansen Are you still planning on working on this? If not, I'm happy to take over. |
Updating to resolve a conflict with GH-129850. IMO, the warnings about @rhansen, do you have any work in progress on this? |
Co-authored-by: Petr Viktorin <[email protected]>
Co-authored-by: Petr Viktorin <[email protected]>
I'm going to be taking this over. I've addressed my own comments and most of Petr's. Other reviews would be appreciated! |
Thank you! I definitely want to take another look -- after Beta 1, next week or so. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall, this is an improvement. Thank you for the work!
Let's do one more pass, merge, and leave the rest for a future PR?
Co-authored-by: Petr Viktorin <[email protected]>
Sounds good to me. |
PyObject_CallFinalizer
andPyObject_CallFinalizerFromDealloc
.PyObject_Init
does not calltp_init
.PyObject_New
:tp_alloc
,tp_new
, ortp_init
PyObject_Free
PyObject_GC_New
memory must be freed byPyObject_GC_Del
.tp_finalize
andtp_clear
:tp_dealloc
(viaPyObject_CallFinalizerFromDealloc
in the case oftp_finalize
).tp_finalize
:object.__del__
.PyErr_GetRaisedException
andPyErr_SetRaisedException
instead of the deprecatedPyErr_Fetch
andPyErr_Restore
functions.PyErr_GetRaisedException
andPyErr_SetRaisedException
.PyErr_WriteUnraisable
if an exception is raised during finalization.local_finalize
tofoo_finalize
for consistency with thetp_dealloc
documentation and as a hint that the name isn't special.tp_finalize
can be called during shutdown.📚 Documentation preview 📚: https://cpython-previews--125962.org.readthedocs.build/