Skip to content

gh-145921: Clarify return value of tp_traverse - #160455

Open
encukou wants to merge 3 commits into
python:mainfrom
encukou:tp_traverse-retval
Open

encukou wants to merge 3 commits into
python:mainfrom
encukou:tp_traverse-retval

Conversation

@encukou

@encukou encukou commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

@encukou
encukou requested a review from nascheme October 23, 2026 17:49
@bedevere-app bedevere-app Bot added awaiting core review type-feature A feature request or enhancement labels Oct 23, 2026
@bedevere-app bedevere-app Bot added docs Documentation in the Doc dir skip news labels Oct 23, 2026
@github-project-automation github-project-automation Bot moved this to Todo in Docs PRs Oct 23, 2026
@read-the-docs-community

read-the-docs-community Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

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

LTGM aside from the "unreachable" wording.

Comment thread Doc/c-api/gcsupport.rst Outdated

If an error occurs during traversal, ignore it and return 0.
If ``tp_traverse`` fails to visit some strong references, objects
may become unreachable and waste memory, but correctness won't be affected.

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 would say something like "garbage reference cycles will not be broken and the program may waste memory". If you want to use to word "unreachable", I would say something like "the GC will not reliability determine which objects are unreachable and are only alive due to circular references"

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.

Or maybe more precisely "garbage reference cycles involving the skipped strong references will not be reliably broken". I think that's accurate. The GC will still be able to break up cyclic garbage involving the working tp_traverse methods (i.e. one failing tp_traverse doesn't prevent the cyclic GC from functioning entirely, which could be wrongly inferred). OTOH, maybe we want to be careful about promising too much. As the GC is now, it's true that other garbage reference cycles can be broken, even in the face of some tp_traverse methods not revealing all strong references. But maybe that's too strong of a promise.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think we can guarantee that. If one incorrect tp_traverse could gum up the entire garbage collector, we'd have a big problem.
Also, “involving” isn't a very precise term...

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

Labels

awaiting merge docs Documentation in the Doc dir skip news type-feature A feature request or enhancement

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

2 participants