[Zope-Checkins] CVS: Zope/lib/python/ExtensionClass - _ExtensionClass.c:1.1.2.6 tests.py:1.1.2.5

Jim Fulton cvs-admin at zope.org
Sat Nov 15 07:11:17 EST 2003


Update of /cvs-repository/Zope/lib/python/ExtensionClass
In directory cvs.zope.org:/tmp/cvs-serv23725/lib/python/ExtensionClass

Modified Files:
      Tag: zodb33-devel-branch
	_ExtensionClass.c tests.py 
Log Message:
Changed inheritedAttribute to search the class mro rather than doing a
depth-first left-to-right search of the bases. The mro is actually
different than that.


=== Zope/lib/python/ExtensionClass/_ExtensionClass.c 1.1.2.5 => 1.1.2.6 ===
--- Zope/lib/python/ExtensionClass/_ExtensionClass.c:1.1.2.5	Mon Nov  3 10:59:58 2003
+++ Zope/lib/python/ExtensionClass/_ExtensionClass.c	Sat Nov 15 07:11:15 2003
@@ -408,15 +408,25 @@
 inheritedAttribute(PyTypeObject *self, PyObject *name)
 {
   int i;
-  PyObject *v;
+  PyObject *d, *cls;
 
-  for (i = 0; i < PyTuple_GET_SIZE(self->tp_bases); i++)
+  for (i = 1; i < PyTuple_GET_SIZE(self->tp_mro); i++)
     {
-      v = PyObject_GetAttr(PyTuple_GET_ITEM(self->tp_bases, i), name);
-      if (v)
-        return v;
-      PyErr_Clear();
+      cls = PyTuple_GET_ITEM(self->tp_mro, i);
+      if (PyType_Check(cls))
+        d = ((PyTypeObject *)cls)->tp_dict;
+      else if (PyClass_Check(cls))
+        d = ((PyClassObject *)cls)->cl_dict;
+      else
+        /* Unrecognized thing, punt */
+        d = NULL;
+      
+      if ((d == NULL) || (PyDict_GetItem(d, name) == NULL))
+        continue;
+                    
+      return PyObject_GetAttr(cls, name);
     }
+
   PyErr_SetObject(PyExc_AttributeError, name);
   return NULL;
 }


=== Zope/lib/python/ExtensionClass/tests.py 1.1.2.4 => 1.1.2.5 ===
--- Zope/lib/python/ExtensionClass/tests.py:1.1.2.4	Mon Nov  3 10:59:58 2003
+++ Zope/lib/python/ExtensionClass/tests.py	Sat Nov 15 07:11:15 2003
@@ -617,6 +617,27 @@
        """'ED', 'EB', 'EA', 'EC', 'ND', 'NB', 'NC', 'NA', 'Base', 'object']
     """
 
+def test_avoiding___init__decoy_w_inheritedAttribute():
+    """
+
+    >>> class Decoy(Base):
+    ...    pass
+
+    >>> class B(Base):
+    ...    def __init__(self, a, b):
+    ...       print '__init__', a, b
+
+    >>> class C(Decoy, B):
+    ...    def __init__(self):
+    ...       print 'C init'
+    ...       C.inheritedAttribute('__init__')(self, 1, 2)
+
+    >>> x = C()
+    C init
+    __init__ 1 2
+    
+    """
+
 from doctest import DocTestSuite
 import unittest
 




More information about the Zope-Checkins mailing list