[Python-checkins] cpython (merge 3.2 -> default): merge 3.2 (#3787e896dbe9)

benjamin.peterson python-checkins at python.org
Thu Mar 8 01:53:09 CET 2012


http://hg.python.org/cpython/rev/b595e1ad5722
changeset: 75484:b595e1ad5722
parent: 75481:c8d1df9ac987
parent: 75482:3787e896dbe9
user: Benjamin Peterson <benjamin at python.org>
date: Wed Mar 07 18:52:52 2012 -0600
summary:
 merge 3.2 (#3787e896dbe9)
files:
 Lib/test/test_descr.py | 19 +++++++++++++++++--
 Misc/NEWS | 3 +++
 Objects/typeobject.c | 9 +++++++--
 3 files changed, 27 insertions(+), 4 deletions(-)
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -1,8 +1,10 @@
 import builtins
+import gc
 import sys
 import types
 import math
 import unittest
+import weakref
 
 from copy import deepcopy
 from test import support
@@ -1186,7 +1188,6 @@
 self.assertEqual(Counted.counter, 0)
 
 # Test lookup leaks [SF bug 572567]
- import gc
 if hasattr(gc, 'get_objects'):
 class G(object):
 def __eq__(self, other):
@@ -4387,7 +4388,6 @@
 self.assertRaises(AttributeError, getattr, C(), "attr")
 self.assertEqual(descr.counter, 4)
 
- import gc
 class EvilGetattribute(object):
 # This used to segfault
 def __getattr__(self, name):
@@ -4484,6 +4484,21 @@
 ns = {'__qualname__': 1}
 self.assertRaises(TypeError, type, 'Foo', (), ns)
 
+ def test_cycle_through_dict(self):
+ # See bug #1469629
+ class X(dict):
+ def __init__(self):
+ dict.__init__(self)
+ self.__dict__ = self
+ x = X()
+ x.attr = 42
+ wr = weakref.ref(x)
+ del x
+ support.gc_collect()
+ self.assertIsNone(wr())
+ for o in gc.get_objects():
+ self.assertIsNot(type(o), X)
+
 
 class DictProxyTests(unittest.TestCase):
 def setUp(self):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 Core and Builtins
 -----------------
 
+- Issue #1469629: Allow cycles through an object's __dict__ slot to be
+ collected. (For example if ``x.__dict__ is x``).
+
 - Issue #14205: dict lookup raises a RuntimeError if the dict is modified
 during a lookup.
 
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -862,8 +862,13 @@
 assert(base);
 }
 
- /* There's no need to clear the instance dict (if any);
- the collector will call its tp_clear handler. */
+ /* Clear the instance dict (if any), to break cycles involving only
+ __dict__ slots (as in the case 'self.__dict__ is self'). */
+ if (type->tp_dictoffset != base->tp_dictoffset) {
+ PyObject **dictptr = _PyObject_GetDictPtr(self);
+ if (dictptr && *dictptr)
+ Py_CLEAR(*dictptr);
+ }
 
 if (baseclear)
 return baseclear(self);
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /