(original) (raw)

changeset: 76520:e3eda2d91e93 branch: 2.7 parent: 76515:6a60359556f9 user: Benjamin Peterson benjamin@python.org date: Tue Apr 24 11:06:25 2012 -0400 files: Lib/test/test_descr.py Misc/NEWS Objects/typeobject.c description: don't use a slot wrapper from a different special method (closes #14658) This also alters the fix to #11603. Specifically, setting __repr__ to object.__str__ now raises a recursion RuntimeError when str() or repr() is called instead of silently bypassing the recursion. I believe this behavior is more correct. diff -r 6a60359556f9 -r e3eda2d91e93 Lib/test/test_descr.py --- a/Lib/test/test_descr.py Mon Apr 23 21:26:58 2012 -0700 +++ b/Lib/test/test_descr.py Tue Apr 24 11:06:25 2012 -0400 @@ -4591,7 +4591,15 @@ pass Foo.__repr__ = Foo.__str__ foo = Foo() - str(foo) + self.assertRaises(RuntimeError, str, foo) + self.assertRaises(RuntimeError, repr, foo) + + def test_mixing_slot_wrappers(self): + class X(dict): + __setattr__ = dict.__setitem__ + x = X() + x.y = 42 + self.assertEqual(x["y"], 42) def test_cycle_through_dict(self): # See bug #1469629 diff -r 6a60359556f9 -r e3eda2d91e93 Misc/NEWS --- a/Misc/NEWS Mon Apr 23 21:26:58 2012 -0700 +++ b/Misc/NEWS Tue Apr 24 11:06:25 2012 -0400 @@ -9,6 +9,12 @@ Core and Builtins ----------------- +- Issue #11603 (again): Setting __repr__ to __str__ now raises a RuntimeError + when repr() or str() is called on such an object. + +- Issue #14658: Fix binding a special method to a builtin implementation of a + special method with a different name. + - Issue #14612: Fix jumping around with blocks by setting f_lineno. - Issue #13889: Check and (if necessary) set FPU control word before calling diff -r 6a60359556f9 -r e3eda2d91e93 Objects/typeobject.c --- a/Objects/typeobject.c Mon Apr 23 21:26:58 2012 -0700 +++ b/Objects/typeobject.c Tue Apr 24 11:06:25 2012 -0400 @@ -2996,7 +2996,7 @@ unaryfunc f; f = Py_TYPE(self)->tp_repr; - if (f == NULL || f == object_str) + if (f == NULL) f = object_repr; return f(self); } @@ -6143,7 +6143,8 @@ } continue; } - if (Py_TYPE(descr) == &PyWrapperDescr_Type) { + if (Py_TYPE(descr) == &PyWrapperDescr_Type && + ((PyWrapperDescrObject *)descr)->d_base->name_strobj == p->name_strobj) { void **tptr = resolve_slotdups(type, p->name_strobj); if (tptr == NULL || tptr == ptr) generic = p->function; /benjamin@python.org