]> granicus.if.org Git - python/commitdiff
don't use a slot wrapper from a different special method (closes #14658)
authorBenjamin Peterson <benjamin@python.org>
Tue, 24 Apr 2012 15:06:25 +0000 (11:06 -0400)
committerBenjamin Peterson <benjamin@python.org>
Tue, 24 Apr 2012 15:06:25 +0000 (11:06 -0400)
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.

Lib/test/test_descr.py
Misc/NEWS
Objects/typeobject.c

index 141d7918ddf712796d2d92f434f4dd8dd7f0f875..2289f6e577686bdd6e3db8442724beadcedf5e17 100644 (file)
@@ -4430,7 +4430,15 @@ order (MRO) for bases """
             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
index 6feb4dac3c0f001cf1ee5533402a2e702914da05..2d58cb618e6cd14446fa457cee62721b3c2678d3 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,12 @@ What's New in Python 3.2.4
 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 #14630: Fix a memory access bug for instances of a subclass of int
   with value 0.
 
index 8cfa8894b0c08bc590c20fc6138a4fd39197042b..13a20f7905f040c0018384543d459c7a3145f15e 100644 (file)
@@ -2928,7 +2928,7 @@ object_str(PyObject *self)
     unaryfunc f;
 
     f = Py_TYPE(self)->tp_repr;
-    if (f == NULL || f == object_str)
+    if (f == NULL)
         f = object_repr;
     return f(self);
 }
@@ -5757,7 +5757,8 @@ update_one_slot(PyTypeObject *type, slotdef *p)
             }
             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;