[Zope3-checkins] SVN: Zope3/branches/benji-integrate-new-mechanize/src/mechanize/_html.py remove a hack to work-around a bug in Mechanize and add a real fix (with tests)

Benji York cvs-admin at zope.org
Tue Jun 20 11:56:41 EDT 2006


Log message for revision 68774:
  remove a hack to work-around a bug in Mechanize and add a real fix (with tests)
  

Changed:
  U   Zope3/branches/benji-integrate-new-mechanize/src/mechanize/_html.py

-=-
Modified: Zope3/branches/benji-integrate-new-mechanize/src/mechanize/_html.py
===================================================================
--- Zope3/branches/benji-integrate-new-mechanize/src/mechanize/_html.py	2006-06-20 14:51:27 UTC (rev 68773)
+++ Zope3/branches/benji-integrate-new-mechanize/src/mechanize/_html.py	2006-06-20 15:56:37 UTC (rev 68774)
@@ -35,15 +35,65 @@
 DEFAULT_ENCODING = "latin-1"
 
 class CachingGeneratorFunction(object):
-    """Caching wrapper around a no-arguments iterable."""
+    """Caching wrapper around a no-arguments iterable.
+
+    >>> i = [1]
+    >>> func = CachingGeneratorFunction(i)
+    >>> list(func())
+    [1]
+    >>> list(func())
+    [1]
+
+    >>> i = [1, 2, 3]
+    >>> func = CachingGeneratorFunction(i)
+    >>> list(func())
+    [1, 2, 3]
+
+    >>> i = func()
+    >>> i.next()
+    1
+    >>> i.next()
+    2
+    >>> i.next()
+    3
+
+    >>> i = func()
+    >>> j = func()
+    >>> i.next()
+    1
+    >>> j.next()
+    1
+    >>> i.next()
+    2
+    >>> j.next()
+    2
+    >>> j.next()
+    3
+    >>> i.next()
+    3
+    >>> i.next()
+    Traceback (most recent call last):
+    ...
+    StopIteration
+    >>> j.next()
+    Traceback (most recent call last):
+    ...
+    StopIteration
+    """
     def __init__(self, iterable):
-        self._iterable = iterable
+        def make_gen():
+            for item in iterable:
+                yield item
+
         self._cache = []
+        self._generator = make_gen()
+
     def __call__(self):
         cache = self._cache
+
         for item in cache:
             yield item
-        for item in self._iterable:
+        for item in self._generator:
             cache.append(item)
             yield item
 
@@ -476,8 +526,7 @@
 
         """
         self._response = response
-#        self._forms_genf = self._links_genf = None
-        self._forms = self._links_genf = None
+        self._forms_genf = self._links_genf = None
         self._get_title = None
         for name in ["encoding", "is_html", "title"]:
             try:
@@ -507,9 +556,10 @@
 
     def forms(self):
         """Return iterable over ClientForm.HTMLForm-like objects."""
-        if self._forms is None:
-            self._forms = self._forms_factory.forms()
-        return self._forms
+        if self._forms_genf is None:
+            self._forms_genf = CachingGeneratorFunction(
+                self._forms_factory.forms())
+        return self._forms_genf()
 
     def links(self):
         """Return iterable over mechanize.Link-like objects."""



More information about the Zope3-Checkins mailing list