]> granicus.if.org Git - python/commitdiff
(Merge 3.1) Issue #9756: When calling a method descriptor or a slot wrapper
authorVictor Stinner <victor.stinner@haypocalc.com>
Sun, 1 May 2011 21:43:37 +0000 (23:43 +0200)
committerVictor Stinner <victor.stinner@haypocalc.com>
Sun, 1 May 2011 21:43:37 +0000 (23:43 +0200)
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).

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

index b5e86fb778cc2c57837661c4c34a9d919f35435e..1fe0de582a32733ba27974bfb4f4f658071dc54a 100644 (file)
@@ -4564,6 +4564,22 @@ order (MRO) for bases """
         with self.assertRaises(AttributeError):
             del X.__abstractmethods__
 
+    def test_proxy_call(self):
+        class FakeStr(object):
+            __class__ = str
+
+        fake_str = FakeStr()
+        # isinstance() reads __class__ on new style classes
+        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 bb6f5ef362d724aab7b52574f3a2a31878e283af..6674c0b00e0e01899472857ac0a4e3dbd75334c1 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -9,6 +9,11 @@ What's New in Python 2.7.2?
 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 #10517: After fork(), reinitialize the TLS used by the PyGILState_*
   APIs, to avoid a crash with the pthread implementation in RHEL 5.  Patch
   by Charles-François Natali.
index 9d735a2640b9e936874262366d5d5ba05c66efa5..135b6d2a9aa7284d206dd9337f1e90037883b7fc 100644 (file)
@@ -224,7 +224,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 '%.200s' "
                      "requires a '%.100s' object "
@@ -282,7 +283,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 '%.200s' "
                      "requires a '%.100s' object "
@@ -1046,7 +1048,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) {