This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: [patch] assert tp_traverse in PyType_GenericAlloc()
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.2, Python 3.3, Python 2.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: ajaksu2, bsilverthorn, iritkatriel, pitrou, vstinner
Priority: normal Keywords: patch

Created on 2007-12-19 17:22 by bsilverthorn, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit
bcs_assert_tp_traverse_r72055.patch bsilverthorn, 2009-04-28 01:24
Messages (9)
msg58811 - (view) Author: Bryan Silverthorn (bsilverthorn) Date: 2007-12-19 17:22
Attached is a very short patch against r59568 which asserts tp_traverse
on (the types of) objects allocated in PyType_GenericAlloc(). As far as
I'm aware, tp_traverse should always be set at this point. Catching that
error early, even if only in debug builds, would help to prevent bugs
like .
msg86708 - (view) Author: Daniel Diniz (ajaksu2) * (Python triager) Date: 2009-04-27 22:57
Bryan: can you provide a test case without external dependencies? If
not, confirming this is still valid in 2.6 would also help.
msg86712 - (view) Author: Bryan Silverthorn (bsilverthorn) Date: 2009-04-28 01:24
Well, there's no Python bug per se, hence no test case; this patch just
adds a single additional assert that might catch a particular extension
implementation mistake. It was prompted by tracking down the bug in
pygtk mentioned above.

I've attached an updated patch against r72055. It's a trivial change,
but I would suggest that someone more familiar with the Python core sign
off on it regardless.
msg86899 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2009-05-01 20:03
It would be probably be better to put this check in PyType_Ready() instead.
msg392243 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-04-28 18:19
Are you sure this assertion is correct? tp_traverse is optional.
msg392277 - (view) Author: Bryan Silverthorn (bsilverthorn) Date: 2021-04-29 03:44
I submitted this patch 14 years ago and am sure of nothing. :)
msg393639 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-05-14 11:10
tp_traverse is optional, so we should not add this assertion.
msg396689 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-06-29 01:30
This issue was fixed differently in bpo-44263, by adding a test in PyType_Ready():

    // 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)
                     "type %s has the Py_TPFLAGS_HAVE_GC flag "
                     "but has no traverse function",
        return -1;

commit ee7637596d8de25f54261bbeabc602d31e74f482
Author: Victor Stinner <>
Date:   Tue Jun 1 23:37:12 2021 +0200

    bpo-44263: Py_TPFLAGS_HAVE_GC requires tp_traverse (GH-26463)
    The PyType_Ready() function now raises an error if a type is defined
    with the Py_TPFLAGS_HAVE_GC flag set but has no traverse function
msg396690 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-06-29 01:32
> tp_traverse is optional, so we should not add this assertion.

It is required by types implementing the GC protocol. This assumption is non-obvious and was not documented. The documentation was completed in bpo-44263 by commit 8b55bc3f93a655bc803bff79725d5fe3f124e2f0.
Date User Action Args
2022-04-11 14:56:29adminsetgithub: 46003
2021-06-29 01:32:27vstinnersetmessages: + msg396690
2021-06-29 01:30:14vstinnersetresolution: rejected -> fixed

messages: + msg396689
nosy: + vstinner
2021-05-14 11:10:56iritkatrielsetstatus: open -> closed
resolution: rejected
messages: + msg393639

stage: test needed -> resolved
2021-04-29 03:44:39bsilverthornsetmessages: + msg392277
2021-04-28 18:19:36iritkatrielsetnosy: + iritkatriel
messages: + msg392243
2011-06-26 19:56:47terry.reedysetversions: + Python 3.2, Python 3.3, - Python 3.1
2009-05-01 20:03:52pitrousetnosy: + pitrou

messages: + msg86899
versions: + Python 3.1, Python 2.7, - Python 2.6
2009-04-28 01:24:43bsilverthornsetfiles: - bcs_typeobject_assert.patch
2009-04-28 01:24:35bsilverthornsetfiles: + bcs_assert_tp_traverse_r72055.patch

messages: + msg86712
2009-04-27 22:57:28ajaksu2setnosy: + ajaksu2

messages: + msg86708
stage: test needed
2007-12-19 17:45:59christian.heimessetpriority: normal
keywords: + patch
2007-12-19 17:22:01bsilverthorncreate