diff options
author | Pablo Galindo <Pablogsal@gmail.com> | 2019-06-02 14:45:13 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-06-02 14:45:13 (GMT) |
commit | 7ffcf848df214135abeea7f6c6faa4135fd0928f (patch) | |
tree | c96c376ca6ba875dcc4aab40b0555b8ea52e4243 /Objects/structseq.c | |
parent | 13ed07998ad93dbdd94991ba0451b9b559f07972 (diff) | |
download | cpython-7ffcf848df214135abeea7f6c6faa4135fd0928f.zip cpython-7ffcf848df214135abeea7f6c6faa4135fd0928f.tar.gz cpython-7ffcf848df214135abeea7f6c6faa4135fd0928f.tar.bz2 |
bpo-37126: Allow structseq objects to be tracked by the GC (GH-13729)
Diffstat (limited to 'Objects/structseq.c')
-rw-r--r-- | Objects/structseq.c | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/Objects/structseq.c b/Objects/structseq.c index a5046c4..3d857f7 100644 --- a/Objects/structseq.c +++ b/Objects/structseq.c @@ -3,6 +3,7 @@ #include "Python.h" #include "pycore_tupleobject.h" +#include "pycore_object.h" #include "structmember.h" static const char visible_length_key[] = "n_sequence_fields"; @@ -59,6 +60,18 @@ PyStructSequence_GetItem(PyObject* op, Py_ssize_t i) return PyStructSequence_GET_ITEM(op, i); } + +static int +structseq_traverse(PyStructSequence *obj, visitproc visit, void *arg) +{ + Py_ssize_t i, size; + size = REAL_SIZE(obj); + for (i = 0; i < size; ++i) { + Py_VISIT(obj->ob_item[i]); + } + return 0; +} + static void structseq_dealloc(PyStructSequence *obj) { @@ -166,6 +179,7 @@ structseq_new_impl(PyTypeObject *type, PyObject *arg, PyObject *dict) } Py_DECREF(arg); + _PyObject_GC_TRACK(res); return (PyObject*) res; } @@ -388,6 +402,7 @@ PyStructSequence_InitType2(PyTypeObject *type, PyStructSequence_Desc *desc) type->tp_methods = structseq_methods; type->tp_new = structseq_new; type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC; + type->tp_traverse = (traverseproc) structseq_traverse; n_members = count_members(desc, &n_unnamed_members); members = PyMem_NEW(PyMemberDef, n_members - n_unnamed_members + 1); @@ -426,7 +441,7 @@ PyStructSequence_NewType(PyStructSequence_Desc *desc) PyMemberDef *members; PyObject *bases; PyTypeObject *type; - PyType_Slot slots[7]; + PyType_Slot slots[8]; PyType_Spec spec; Py_ssize_t n_members, n_unnamed_members; @@ -446,7 +461,8 @@ PyStructSequence_NewType(PyStructSequence_Desc *desc) slots[3] = (PyType_Slot){Py_tp_methods, structseq_methods}; slots[4] = (PyType_Slot){Py_tp_new, structseq_new}; slots[5] = (PyType_Slot){Py_tp_members, members}; - slots[6] = (PyType_Slot){0, 0}; + slots[6] = (PyType_Slot){Py_tp_traverse, (traverseproc)structseq_traverse}; + slots[7] = (PyType_Slot){0, 0}; /* Initialize Spec */ /* The name in this PyType_Spec is statically allocated so it is */ |