Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-41036: Implement object_traverse() #20983

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Provide a default tp_traverse implementation for the base object type for
heap types which have no tp_traverse function. The traverse function visits
the type if the type is a heap type.
18 changes: 16 additions & 2 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1139,8 +1139,10 @@ subtype_traverse(PyObject *self, visitproc visit, void *arg)
Py_VISIT(type);
}

if (basetraverse)
// Don't call object_traverse() to avoid visiting the type twice
if (basetraverse && base != &PyBaseObject_Type) {
return basetraverse(self, visit, arg);
}
return 0;
}

Expand Down Expand Up @@ -4906,6 +4908,18 @@ object___dir___impl(PyObject *self)
return result;
}

static int
object_traverse(PyObject *op, visitproc visit, void *arg)
{
PyTypeObject *type = Py_TYPE(op);
if (_PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE)) {
// For instance of heap types, tp_type is a strong reference
// to the type.
Py_VISIT(type);
}
return 0;
}

static PyMethodDef object_methods[] = {
OBJECT___REDUCE_EX___METHODDEF
OBJECT___REDUCE___METHODDEF
Expand Down Expand Up @@ -4947,7 +4961,7 @@ PyTypeObject PyBaseObject_Type = {
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
object_doc, /* tp_doc */
0, /* tp_traverse */
object_traverse, /* tp_traverse */
0, /* tp_clear */
object_richcompare, /* tp_richcompare */
0, /* tp_weaklistoffset */
Expand Down