]> granicus.if.org Git - python/commitdiff
Issue #2115: __slot__ attributes setting was 10x slower.
authorAmaury Forgeot d'Arc <amauryfa@gmail.com>
Fri, 15 Feb 2008 21:22:45 +0000 (21:22 +0000)
committerAmaury Forgeot d'Arc <amauryfa@gmail.com>
Fri, 15 Feb 2008 21:22:45 +0000 (21:22 +0000)
Also correct a possible crash using ABCs.

This change is exactly the same as an optimisation
done 5 years ago, but on slot *access*:
http://svn.python.org/view?view=rev&rev=28297

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

index 755a967f45d593568930605e73523959fbc07626..3c607f70cc940a4e64202a8afbe1af1b522087a3 100644 (file)
@@ -1182,6 +1182,24 @@ order (MRO) for bases """
         a.foo = 42
         self.assertEqual(a.__dict__, {"foo": 42})
 
+    def test_slots_descriptor(self):
+        # Issue2115: slot descriptors did not correctly check
+        # the type of the given object
+        import abc
+        class MyABC:
+            __metaclass__ = abc.ABCMeta
+            __slots__ = "a"
+
+        class Unrelated(object):
+            pass
+        MyABC.register(Unrelated)
+
+        u = Unrelated()
+        self.assert_(isinstance(u, MyABC))
+
+        # This used to crash
+        self.assertRaises(TypeError, MyABC.a.__set__, u, 3)
+
     def test_dynamics(self):
         # Testing class attribute propagation...
         class D(object):
index fdc81c1a539ae16871d6dd4b13530dead6435c3a..74466d69143dbc793d1fec20bca4daed6186f211 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,10 @@ What's New in Python 2.6 alpha 1?
 Core and builtins
 -----------------
 
+- Issue #2115: Important speedup in setting __slot__ attributes.  Also 
+  prevent a possible crash: an Abstract Base Class would try to access a slot 
+  on a registered virtual subclass.
+
 - Fixed repr() and str() of complex numbers with infinity or nan as real or
   imaginary part.
 
index 4b599ec2c88f7d305ec462008f8d1c3b06f059ba..9823b7436c3210df4807ebdf1a23a6424afdbcca 100644 (file)
@@ -166,7 +166,7 @@ descr_setcheck(PyDescrObject *descr, PyObject *obj, PyObject *value,
               int *pres)
 {
        assert(obj != NULL);
-       if (!PyObject_IsInstance(obj, (PyObject *)(descr->d_type))) {
+       if (!PyObject_TypeCheck(obj, descr->d_type)) {
                PyErr_Format(PyExc_TypeError,
                             "descriptor '%.200s' for '%.100s' objects "
                             "doesn't apply to '%.100s' object",