Skip to content

Check nb::inst_ready() in tp_clear examples and tests - #1463

Open
hawkinsp wants to merge 1 commit into
wjakob:masterfrom
hawkinsp:tp_clear
Open

hawkinsp wants to merge 1 commit into
wjakob:masterfrom
hawkinsp:tp_clear

Conversation

@hawkinsp

@hawkinsp hawkinsp commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

If an object is created with new without running init, its C++ constructor has not run yet. If that object is reachable from a reference cycle (for example, a list that contains itself), Python's garbage collector can still call tp_clear on it. Touching the C++ object before its constructor has run is undefined behavior.

Add nb::inst_ready(self) checks to the tp_clear example in the docs and to the tp_clear (and test_ndarray tp_traverse) callbacks in the tests.

If an object is created with __new__ without running __init__, its C++ constructor has not run yet. If that object is reachable from a reference cycle (for example, a list that contains itself), Python's garbage collector can still call tp_clear on it. Touching the C++ object before its constructor has run is undefined behavior.

Add nb::inst_ready(self) checks to the tp_clear example in the docs and to the tp_clear (and test_ndarray tp_traverse) callbacks in the tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant