]> granicus.if.org Git - python/commitdiff
Issue #9756: When calling a method descriptor or a slot wrapper descriptor, the
authorVictor Stinner <victor.stinner@haypocalc.com>
Sun, 1 May 2011 21:19:15 +0000 (23:19 +0200)
committerVictor Stinner <victor.stinner@haypocalc.com>
Sun, 1 May 2011 21:19:15 +0000 (23:19 +0200)
check of the object type doesn't read the __class__ attribute anymore.  Fix a
crash if a class override its __class__ attribute (e.g. a proxy of the str
type).

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

index bd88f4533774e42cb8c90b56a59b896d6840d92b..aafe428bd0c9afe0ab9b2a2381f6e92bbcf18c3a 100644 (file)
@@ -4235,6 +4235,22 @@ order (MRO) for bases """
         with self.assertRaises(AttributeError):
             del X.__abstractmethods__
 
+    def test_proxy_call(self):
+        class FakeStr:
+            __class__ = str
+
+        fake_str = FakeStr()
+        # isinstance() reads __class__
+        self.assertTrue(isinstance(fake_str, str))
+
+        # call a method descriptor
+        with self.assertRaises(TypeError):
+            str.split(fake_str)
+
+        # call a slot wrapper descriptor
+        with self.assertRaises(TypeError):
+            str.__add__(fake_str, "abc")
+
 
 class DictProxyTests(unittest.TestCase):
     def setUp(self):
index b4ba3b67cb0d021e2de6e354f54bf657506bf13b..41bda2a6a78c193d02ac219a6749fe8813f22243 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,11 @@ What's New in Python 3.1.4?
 Core and Builtins
 -----------------
 
+- Issue #9756: When calling a method descriptor or a slot wrapper descriptor,
+  the check of the object type doesn't read the __class__ attribute anymore.
+  Fix a crash if a class override its __class__ attribute (e.g. a proxy of the
+  str type).
+
 - Issue #6780: fix starts/endswith error message to mention that tuples are
   accepted too.
 
index 5f118ce09ed4788f47a8f799d075cb608576c170..9eda4bdd7da7755c790388015f1fb1b858f8c715 100644 (file)
@@ -226,7 +226,8 @@ methoddescr_call(PyMethodDescrObject *descr, PyObject *args, PyObject *kwds)
         return NULL;
     }
     self = PyTuple_GET_ITEM(args, 0);
-    if (!PyObject_IsInstance(self, (PyObject *)(descr->d_type))) {
+    if (!_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self),
+                                  (PyObject *)(descr->d_type))) {
         PyErr_Format(PyExc_TypeError,
                      "descriptor '%V' "
                      "requires a '%.100s' object "
@@ -284,7 +285,8 @@ wrapperdescr_call(PyWrapperDescrObject *descr, PyObject *args, PyObject *kwds)
         return NULL;
     }
     self = PyTuple_GET_ITEM(args, 0);
-    if (!PyObject_IsInstance(self, (PyObject *)(descr->d_type))) {
+    if (!_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self),
+                                  (PyObject *)(descr->d_type))) {
         PyErr_Format(PyExc_TypeError,
                      "descriptor '%V' "
                      "requires a '%.100s' object "
@@ -1065,7 +1067,8 @@ PyWrapper_New(PyObject *d, PyObject *self)
 
     assert(PyObject_TypeCheck(d, &PyWrapperDescr_Type));
     descr = (PyWrapperDescrObject *)d;
-    assert(PyObject_IsInstance(self, (PyObject *)(descr->d_type)));
+    assert(_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self),
+                                    (PyObject *)(descr->d_type)));
 
     wp = PyObject_GC_New(wrapperobject, &wrappertype);
     if (wp != NULL) {