[Python-checkins] r60841 - in python/trunk: Lib/test/test_descr.py Misc/NEWS Objects/descrobject.c

amaury.forgeotdarc python-checkins at python.org
Fri Feb 15 22:22:46 CET 2008


Author: amaury.forgeotdarc
Date: Fri Feb 15 22:22:45 2008
New Revision: 60841
Modified:
 python/trunk/Lib/test/test_descr.py
 python/trunk/Misc/NEWS
 python/trunk/Objects/descrobject.c
Log:
Issue #2115: __slot__ attributes setting was 10x slower.
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
Modified: python/trunk/Lib/test/test_descr.py
==============================================================================
--- python/trunk/Lib/test/test_descr.py	(original)
+++ python/trunk/Lib/test/test_descr.py	Fri Feb 15 22:22:45 2008
@@ -1182,6 +1182,24 @@
 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):
Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Fri Feb 15 22:22:45 2008
@@ -12,6 +12,10 @@
 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.
 
Modified: python/trunk/Objects/descrobject.c
==============================================================================
--- python/trunk/Objects/descrobject.c	(original)
+++ python/trunk/Objects/descrobject.c	Fri Feb 15 22:22:45 2008
@@ -166,7 +166,7 @@
 	 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",


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /