Kaydet (Commit) 414f8b93 authored tarafından Benjamin Peterson's avatar Benjamin Peterson

add gc support to slice (closes #26659)

üst 16b83b1f
# tests for slice objects; in particular the indices method. # tests for slice objects; in particular the indices method.
import unittest import unittest
from test import test_support import weakref
from cPickle import loads, dumps from cPickle import loads, dumps
from test import test_support
import sys import sys
...@@ -128,6 +130,15 @@ class SliceTest(unittest.TestCase): ...@@ -128,6 +130,15 @@ class SliceTest(unittest.TestCase):
self.assertEqual(s.indices(15), t.indices(15)) self.assertEqual(s.indices(15), t.indices(15))
self.assertNotEqual(id(s), id(t)) 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())
def test_main(): def test_main():
test_support.run_unittest(SliceTest) test_support.run_unittest(SliceTest)
......
...@@ -10,6 +10,8 @@ What's New in Python 2.7.12? ...@@ -10,6 +10,8 @@ What's New in Python 2.7.12?
Core and Builtins 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. - Issue #26718: super.__init__ no longer leaks memory if called multiple times.
NOTE: A direct call of super.__init__ is not endorsed! NOTE: A direct call of super.__init__ is not endorsed!
......
...@@ -60,7 +60,7 @@ PyObject _Py_EllipsisObject = { ...@@ -60,7 +60,7 @@ PyObject _Py_EllipsisObject = {
PyObject * PyObject *
PySlice_New(PyObject *start, PyObject *stop, PyObject *step) PySlice_New(PyObject *start, PyObject *stop, PyObject *step)
{ {
PySliceObject *obj = PyObject_New(PySliceObject, &PySlice_Type); PySliceObject *obj = PyObject_GC_New(PySliceObject, &PySlice_Type);
if (obj == NULL) if (obj == NULL)
return NULL; return NULL;
...@@ -76,6 +76,7 @@ PySlice_New(PyObject *start, PyObject *stop, PyObject *step) ...@@ -76,6 +76,7 @@ PySlice_New(PyObject *start, PyObject *stop, PyObject *step)
obj->start = start; obj->start = start;
obj->stop = stop; obj->stop = stop;
_PyObject_GC_TRACK(obj);
return (PyObject *) obj; return (PyObject *) obj;
} }
...@@ -219,10 +220,11 @@ Create a slice object. This is used for extended slicing (e.g. a[0:10:2])."); ...@@ -219,10 +220,11 @@ Create a slice object. This is used for extended slicing (e.g. a[0:10:2]).");
static void static void
slice_dealloc(PySliceObject *r) slice_dealloc(PySliceObject *r)
{ {
_PyObject_GC_UNTRACK(r);
Py_DECREF(r->step); Py_DECREF(r->step);
Py_DECREF(r->start); Py_DECREF(r->start);
Py_DECREF(r->stop); Py_DECREF(r->stop);
PyObject_Del(r); PyObject_GC_Del(r);
} }
static PyObject * static PyObject *
...@@ -320,6 +322,15 @@ slice_hash(PySliceObject *v) ...@@ -320,6 +322,15 @@ slice_hash(PySliceObject *v)
return -1L; return -1L;
} }
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 = { PyTypeObject PySlice_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0) PyVarObject_HEAD_INIT(&PyType_Type, 0)
"slice", /* Name of this type */ "slice", /* Name of this type */
...@@ -340,9 +351,9 @@ PyTypeObject PySlice_Type = { ...@@ -340,9 +351,9 @@ PyTypeObject PySlice_Type = {
PyObject_GenericGetAttr, /* tp_getattro */ PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */ 0, /* tp_setattro */
0, /* tp_as_buffer */ 0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
slice_doc, /* tp_doc */ slice_doc, /* tp_doc */
0, /* tp_traverse */ (traverseproc)slice_traverse, /* tp_traverse */
0, /* tp_clear */ 0, /* tp_clear */
0, /* tp_richcompare */ 0, /* tp_richcompare */
0, /* tp_weaklistoffset */ 0, /* tp_weaklistoffset */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment