[Zope-Checkins] CVS: Zope/lib/python/SearchIndex - UnIndex.py:1.34

Jim Fulton cvs-admin at zope.org
Fri Nov 28 11:46:13 EST 2003


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

Modified Files:
	UnIndex.py 
Log Message:
With new-style classes, is is no longer possible to override slots
(special '__' methods) on an instance-by instance basis. Changed the
handling of the length so that the __len__ method looks for a __len__
dictionary key. It would be better not to use a __len__ attribute name
for the BTree length object, but such a change would be problematic
for old instances.


=== Zope/lib/python/SearchIndex/UnIndex.py 1.33 => 1.34 ===
--- Zope/lib/python/SearchIndex/UnIndex.py:1.33	Fri May  9 16:11:55 2003
+++ Zope/lib/python/SearchIndex/UnIndex.py	Fri Nov 28 11:46:12 2003
@@ -75,9 +75,22 @@
         self.ignore_ex=ignore_ex        # currently unimplimented
         self.call_methods=call_methods
 
-        self.__len__=BTrees.Length.Length() # see __len__ method docstring
+        # Note that it was unfortunate to use __len__ as the attribute
+        # name here. New-style classes cache slot methods in C slot
+        # pointers. The result is that instances can't override slots.
+        # This is not easy to change on account of old objects with
+        # __len__ attr.
+
+        self.__len__=BTrees.Length.Length()
         self.clear()
 
+    def __len__(self):
+        try:
+            return self.__dict__['__len__']()
+        except KeyError:
+            # Fallback for really old indexes
+            return len(self._unindex)
+
     def clear(self):
         # inplace opportunistic conversion from old-style to new style BTrees
         try: self.__len__.set(0)
@@ -116,15 +129,6 @@
 
     def __nonzero__(self):
         return not not self._unindex
-
-    def __len__(self):
-        """Return the number of objects indexed.
-
-        This method is only called for indexes which have "old" BTrees,
-        and the *only* reason that UnIndexes maintain a __len__ is for
-        the searching code in the catalog during sorting.
-        """
-        return len(self._unindex)
 
     def histogram(self):
         """Return a mapping which provides a histogram of the number of




More information about the Zope-Checkins mailing list