[Python-checkins] cpython (merge 3.1 -> 3.2): (Merge 3.1) Issue #9756: When calling a method descriptor or a slot wrapper

victor.stinner python-checkins at python.org
Sun May 1 23:33:20 CEST 2011


http://hg.python.org/cpython/rev/4fc04f6a0731
changeset:   69742:4fc04f6a0731
branch:      3.2
parent:      69738:13878873ccc7
parent:      69741:c5e6f997730e
user:        Victor Stinner <victor.stinner at haypocalc.com>
date:        Sun May 01 23:31:36 2011 +0200
summary:
  (Merge 3.1) 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).

files:
  Lib/test/test_descr.py |  16 ++++++++++++++++
  Misc/NEWS              |   5 +++++
  Objects/descrobject.c  |   9 ++++++---
  3 files changed, 27 insertions(+), 3 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
@@ -4235,6 +4235,22 @@
         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):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,11 @@
 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 #10914: Initialize correctly the filesystem codec when creating a new
   subinterpreter to fix a bootstrap issue with codecs implemented in Python, as
   the ISO-8859-15 codec.
diff --git a/Objects/descrobject.c b/Objects/descrobject.c
--- a/Objects/descrobject.c
+++ b/Objects/descrobject.c
@@ -226,7 +226,8 @@
         return NULL;
     }
     self = PyTuple_GET_ITEM(args, 0);
-    if (!PyObject_IsInstance(self, (PyObject *)PyDescr_TYPE(descr))) {
+    if (!_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self),
+                                  (PyObject *)PyDescr_TYPE(descr))) {
         PyErr_Format(PyExc_TypeError,
                      "descriptor '%V' "
                      "requires a '%.100s' object "
@@ -284,7 +285,8 @@
         return NULL;
     }
     self = PyTuple_GET_ITEM(args, 0);
-    if (!PyObject_IsInstance(self, (PyObject *)PyDescr_TYPE(descr))) {
+    if (!_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self),
+                                  (PyObject *)PyDescr_TYPE(descr))) {
         PyErr_Format(PyExc_TypeError,
                      "descriptor '%V' "
                      "requires a '%.100s' object "
@@ -1065,7 +1067,8 @@
 
     assert(PyObject_TypeCheck(d, &PyWrapperDescr_Type));
     descr = (PyWrapperDescrObject *)d;
-    assert(PyObject_IsInstance(self, (PyObject *)PyDescr_TYPE(descr)));
+    assert(_PyObject_RealIsSubclass((PyObject *)Py_TYPE(self),
+                                    (PyObject *)PyDescr_TYPE(descr)));
 
     wp = PyObject_GC_New(wrapperobject, &wrappertype);
     if (wp != NULL) {

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list