[Python-checkins] bpo-37126: Allow structseq objects to be tracked by the GC (GH-13729)

Pablo Galindo webhook-mailer at python.org
Sun Jun 2 10:45:19 EDT 2019


https://github.com/python/cpython/commit/7ffcf848df214135abeea7f6c6faa4135fd0928f
commit: 7ffcf848df214135abeea7f6c6faa4135fd0928f
branch: master
author: Pablo Galindo <Pablogsal at gmail.com>
committer: GitHub <noreply at github.com>
date: 2019-06-02T15:45:13+01:00
summary:

bpo-37126: Allow structseq objects to be tracked by the GC (GH-13729)

files:
A Misc/NEWS.d/next/Core and Builtins/2019-06-01-20-03-13.bpo-37126.tP6lL4.rst
M Objects/structseq.c

diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-06-01-20-03-13.bpo-37126.tP6lL4.rst b/Misc/NEWS.d/next/Core and Builtins/2019-06-01-20-03-13.bpo-37126.tP6lL4.rst
new file mode 100644
index 000000000000..069b064dfa61
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2019-06-01-20-03-13.bpo-37126.tP6lL4.rst	
@@ -0,0 +1,2 @@
+All structseq objects are now tracked by the garbage collector. Patch by
+Pablo Galindo.
diff --git a/Objects/structseq.c b/Objects/structseq.c
index a5046c42cbc3..3d857f734be8 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 */



More information about the Python-checkins mailing list