[Zope-Checkins] SVN: Zope/trunk/ Forward ported c118863 from 2.13 branch

Hanno Schlichting hannosch at hannosch.eu
Tue Dec 14 12:04:53 EST 2010


Log message for revision 118872:
  Forward ported c118863 from 2.13 branch
  

Changed:
  U   Zope/trunk/doc/CHANGES.rst
  U   Zope/trunk/src/Products/ZCatalog/Lazy.py
  U   Zope/trunk/src/Products/ZCatalog/tests/test_lazy.py

-=-
Modified: Zope/trunk/doc/CHANGES.rst
===================================================================
--- Zope/trunk/doc/CHANGES.rst	2010-12-14 16:36:29 UTC (rev 118871)
+++ Zope/trunk/doc/CHANGES.rst	2010-12-14 17:04:52 UTC (rev 118872)
@@ -11,6 +11,8 @@
 Bugs Fixed
 ++++++++++
 
+- Fix `LazyMap` to avoid unnecessary function calls.
+
 - LP 686664: WebDAV Lock Manager ZMI view wasn't accessible.
 
 - Fixed argument parsing for entrypoint based zopectl commands.

Modified: Zope/trunk/src/Products/ZCatalog/Lazy.py
===================================================================
--- Zope/trunk/src/Products/ZCatalog/Lazy.py	2010-12-14 16:36:29 UTC (rev 118871)
+++ Zope/trunk/src/Products/ZCatalog/Lazy.py	2010-12-14 17:04:52 UTC (rev 118872)
@@ -145,44 +145,28 @@
     # Don't access data until necessary
 
     def __init__(self, func, seq, length=None):
-        self._seq = seq
-        self._data = []
-        self._func = func
+        self._seq=seq
+        self._func=func
         if length is not None:
-            self._len = length
+            self._len=length
         else:
             self._len = len(seq)
+        self._marker = object()
+        self._data = [self._marker] * self._len
 
-    def __getitem__(self, index):
-        data = self._data
+    def __getitem__(self,index):
+        data=self._data
         try:
-            s = self._seq
+            s=self._seq
         except AttributeError:
             return data[index]
 
-        i = index
-        if i < 0:
-            i = len(self) + i
-        if i < 0:
-            raise IndexError(index)
+        value = data[index]
+        if value is self._marker:
+            value = data[index] = self._func(s[index])
+        return value
 
-        ind = len(data)
-        if i < ind:
-            return data[i]
-        ind = ind - 1
 
-        func = self._func
-        while i > ind:
-            try:
-                ind = ind + 1
-                data.append(func(s[ind]))
-            except IndexError:
-                del self._func
-                del self._seq
-                raise IndexError(index)
-        return data[i]
-
-
 class LazyFilter(Lazy):
     # Act like a sequence, but get data from a filtering process.
     # Don't access data until necessary. Only data for which test(data)

Modified: Zope/trunk/src/Products/ZCatalog/tests/test_lazy.py
===================================================================
--- Zope/trunk/src/Products/ZCatalog/tests/test_lazy.py	2010-12-14 16:36:29 UTC (rev 118871)
+++ Zope/trunk/src/Products/ZCatalog/tests/test_lazy.py	2010-12-14 17:04:52 UTC (rev 118872)
@@ -113,7 +113,17 @@
         lmap = self._createLMap(filter, seq1, seq2, seq3)
         self._compare(lmap, [str(x).lower() for x in (seq1 + seq2 + seq3)])
 
+    def testMapFuncIsOnlyCalledAsNecessary(self):
+        seq = range(10)
+        count = [0]     # closure only works with list, and `nonlocal` in py3
+        def func(x):
+            count[0] += 1
+            return x
+        lmap = self._createLMap(func, seq)
+        self.assertEqual(lmap[5], 5)
+        self.assertEqual(count[0], 1)
 
+
 class TestLazyFilter(TestLazyCat):
 
     def _createLSeq(self, *seq):



More information about the Zope-Checkins mailing list