[Python-checkins] cpython (merge 3.5 -> default): merge 3.5 (#26659)

benjamin.peterson python-checkins at python.org
Sat Apr 16 17:54:37 EDT 2016


https://hg.python.org/cpython/rev/870fcc50f1bd
changeset:   101022:870fcc50f1bd
parent:      101017:3bb3e6316098
parent:      101021:9e2176d18965
user:        Benjamin Peterson <benjamin at python.org>
date:        Sat Apr 16 14:54:27 2016 -0700
summary:
  merge 3.5 (#26659)

files:
  Lib/test/test_slice.py |  17 ++++++++++++++---
  Misc/NEWS              |   2 ++
  Objects/sliceobject.c  |  19 +++++++++++++++----
  3 files changed, 31 insertions(+), 7 deletions(-)


diff --git a/Lib/test/test_slice.py b/Lib/test/test_slice.py
--- a/Lib/test/test_slice.py
+++ b/Lib/test/test_slice.py
@@ -1,11 +1,13 @@
 # tests for slice objects; in particular the indices method.
 
-import unittest
-from pickle import loads, dumps
-
 import itertools
 import operator
 import sys
+import unittest
+import weakref
+
+from pickle import loads, dumps
+from test import support
 
 
 def evaluate_slice_index(arg):
@@ -240,5 +242,14 @@
             self.assertEqual(s.indices(15), t.indices(15))
             self.assertNotEqual(id(s), id(t))
 
+    def test_cycle(self):
+        class myobj(): pass
+        o = myobj()
+        o.s = slice(o)
+        w = weakref.ref(o)
+        o = None
+        test_support.gc_collect()
+        self.assertIsNone(w())
+
 if __name__ == "__main__":
     unittest.main()
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,8 @@
 Core and Builtins
 -----------------
 
+- Issue #26659: Make the builtin slice type support cycle collection.
+
 - Issue #26718: super.__init__ no longer leaks memory if called multiple times.
   NOTE: A direct call of super.__init__ is not endorsed!
 
diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c
--- a/Objects/sliceobject.c
+++ b/Objects/sliceobject.c
@@ -119,7 +119,7 @@
         slice_cache = NULL;
         _Py_NewReference((PyObject *)obj);
     } else {
-        obj = PyObject_New(PySliceObject, &PySlice_Type);
+        obj = PyObject_GC_New(PySliceObject, &PySlice_Type);
         if (obj == NULL)
             return NULL;
     }
@@ -135,6 +135,7 @@
     obj->start = start;
     obj->stop = stop;
 
+    _PyObject_GC_TRACK(obj);
     return (PyObject *) obj;
 }
 
@@ -288,13 +289,14 @@
 static void
 slice_dealloc(PySliceObject *r)
 {
+    _PyObject_GC_UNTRACK(r);
     Py_DECREF(r->step);
     Py_DECREF(r->start);
     Py_DECREF(r->stop);
     if (slice_cache == NULL)
         slice_cache = r;
     else
-        PyObject_Del(r);
+        PyObject_GC_Del(r);
 }
 
 static PyObject *
@@ -586,6 +588,15 @@
     return res;
 }
 
+static int
+slice_traverse(PySliceObject *v, visitproc visit, void *arg)
+{
+    Py_VISIT(v->start);
+    Py_VISIT(v->stop);
+    Py_VISIT(v->step);
+    return 0;
+}
+
 PyTypeObject PySlice_Type = {
     PyVarObject_HEAD_INIT(&PyType_Type, 0)
     "slice",                    /* Name of this type */
@@ -606,9 +617,9 @@
     PyObject_GenericGetAttr,                    /* tp_getattro */
     0,                                          /* tp_setattro */
     0,                                          /* tp_as_buffer */
-    Py_TPFLAGS_DEFAULT,                         /* tp_flags */
+    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,    /* tp_flags */
     slice_doc,                                  /* tp_doc */
-    0,                                          /* tp_traverse */
+    (traverseproc)slice_traverse,               /* tp_traverse */
     0,                                          /* tp_clear */
     slice_richcompare,                          /* tp_richcompare */
     0,                                          /* tp_weaklistoffset */

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list