8000 bpo-44263: Py_TPFLAGS_HAVE_GC requires tp_traverse by vstinner · Pull Request #26463 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-44263: Py_TPFLAGS_HAVE_GC requires tp_traverse #26463

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

Merged
merged 1 commit into from
Jun 1, 2021
Merged
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
7 changes: 6 additions & 1 deletion Doc/whatsnew/3.11.rst
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ New Features
Porting to Python 3.11
----------------------

* The :c:func:`PyType_Ready` function now raises an error if a type is defined
with the :const:`Py_TPFLAGS_HAVE_GC` flag set but has no traverse function
(:c:member:`PyTypeObject.tp_traverse`).
(Contributed by Victor Stinner in :issue:`44263`.)

Deprecated
----------

Expand Down Expand Up @@ -180,4 +185,4 @@ Removed
parameter of functions :func:`~gettext.translation` and
:func:`~gettext.install` are also removed, since they are only used for
the ``l*gettext()`` functions.
(Contributed by Dong-hee Na and Serhiy Storchaka in :issue:`44235`.)
(Contributed by Dong-hee Na and Serhiy Storchaka in :issue:`44235`.)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why this change? :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Ask my text editor.

Copy link
Member Author

Choose a reason for hiding this comment

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

My text editor (vim) likes to add newline at the end of a file.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
The :c:func:`PyType_Ready` function now raises an error if a type is defined
with the :const:`Py_TPFLAGS_HAVE_GC` flag set but has no traverse function
(:c:member:`PyTypeObject.tp_traverse`).
Patch by Victor Stinner.
32 changes: 30 additions & 2 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ _PyType_CheckConsistency(PyTypeObject *type)
CHECK(!(type->tp_flags & Py_TPFLAGS_READYING));
CHECK(type->tp_dict != NULL);

if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
// bpo-44263: tp_traverse is required if Py_TPFLAGS_HAVE_GC is set.
// Note: tp_clear is optional.
CHECK(type->tp_traverse != NULL);
}

if (type->tp_flags & Py_TPFLAGS_DISALLOW_INSTANTIATION) {
CHECK(type->tp_new == NULL);
CHECK(_PyDict_ContainsId(type->tp_dict, &PyId___new__) == 0);
Expand Down Expand Up @@ -3607,6 +3613,7 @@ PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases)
}
}

assert(_PyType_CheckConsistency(type));
return (PyObject*)res;

fail:
Expand Down Expand Up @@ -5944,7 +5951,7 @@ static int add_tp_new_wrapper(PyTypeObject *type);
#define COLLECTION_FLAGS (Py_TPFLAGS_SEQUENCE | Py_TPFLAGS_MAPPING)

static int
type_ready_checks(PyTypeObject *type)
type_ready_pre_checks(PyTypeObject *type)
{
/* Consistency checks for PEP 590:
* - Py_TPFLAGS_METHOD_DESCRIPTOR requires tp_descr_get
Expand Down Expand Up @@ -6305,10 +6312,28 @@ type_ready_set_new(PyTypeObject *type)
}


static int
type_ready_post_checks(PyTypeObject *type)
{
// bpo-44263: tp_traverse is required if Py_TPFLAGS_HAVE_GC is set.
// Note: tp_clear is optional.
if (type->tp_flags & Py_TPFLAGS_HAVE_GC
&& type->tp_traverse == NULL)
{
PyErr_Format(PyExc_SystemError,
"type %s has the Py_TPFLAGS_HAVE_GC flag "
"but has no traverse function",
type->tp_name);
return -1;
}
return 0;
}


static int
type_ready(PyTypeObject *type)
{
if (type_ready_checks(type) < 0) {
if (type_ready_pre_checks(type) < 0) {
return -1;
}

Expand Down Expand Up @@ -6346,6 +6371,9 @@ type_ready(PyTypeObject *type)
if (type_ready_add_subclasses(type) < 0) {
return -1;
}
if (type_ready_post_checks(type) < 0) {
return -1;
}
return 0;
}

Expand Down
0