Skip to content
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

gh-110812: Isolating Extension Modules HOWTO: List GC-related gotchas #111504

Merged
merged 6 commits into from
Nov 16, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions Doc/howto/isolating-extensions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -438,9 +438,20 @@ GC-tracked objects need to be allocated using GC-aware functions.

If you use use :c:func:`PyObject_New` or :c:func:`PyObject_NewVar`:

- Get and call type's :c:member:`~PyTypeObject.tp_alloc` slot , if possible.
- If not possible (e.g. inside a custom ``tp_alloc``),
call :c:func:`PyObject_GC_New` or :c:func:`PyObject_GC_NewVar`.
- Get and call type's :c:member:`~PyTypeObject.tp_alloc` slot, if possible.
That is, replace ``TYPE *o = PyObject_New(TYPE, typeobj)`` by::
encukou marked this conversation as resolved.
Show resolved Hide resolved

TYPE *o = typeobj->tp_alloc(typeobj, 0);
ericsnowcurrently marked this conversation as resolved.
Show resolved Hide resolved

Replace ``o = PyObject_NewVar(TYPE, typeobj, size)`` with the same,
but use size instead of the 0.

- If the above is not possible (e.g. inside a custom ``tp_alloc``),
call :c:func:`PyObject_GC_New` or :c:func:`PyObject_GC_NewVar`::

TYPE *o = PyObject_GC_New(TYPE, typeobj);

TYPE *o = PyObject_GC_NewVar(TYPE, typeobj, size);


Module State Access from Classes
Expand Down
Loading