[wwwsearch-commits] r36044 - in wwwsearch/mechanize/trunk: mechanize test
jjlee at codespeak.net
jjlee at codespeak.net
Fri Dec 29 21:40:08 CET 2006
Author: jjlee
Date: Fri Dec 29 21:40:06 2006
New Revision: 36044
Modified:
wwwsearch/mechanize/trunk/mechanize/_html.py
wwwsearch/mechanize/trunk/test/test_forms.doctest
Log:
Fix non-idempotent behaviour of Factory.forms() / .links() by making Factory not assume restarting behaviour of e.g. FormsFactory.forms() / LinksFactory.links() iterator functions. Previously, if for example you got a ParseError during execution of .forms(), you could call it again and have it not raise an exception, because it started out where it left off.
Modified: wwwsearch/mechanize/trunk/mechanize/_html.py
==============================================================================
--- wwwsearch/mechanize/trunk/mechanize/_html.py (original)
+++ wwwsearch/mechanize/trunk/mechanize/_html.py Fri Dec 29 21:40:06 2006
@@ -509,8 +509,12 @@
# this implementation sets .global_form as a side-effect, for benefit
# of __getattr__ impl
if self._forms_genf is None:
- self._forms_genf = CachingGeneratorFunction(
- self._forms_factory.forms())
+ try:
+ self._forms_genf = CachingGeneratorFunction(
+ self._forms_factory.forms())
+ except:
+ self.set_response(self._response)
+ raise
self.global_form = getattr(
self._forms_factory, "global_form", None)
return self._forms_genf()
@@ -518,8 +522,12 @@
def links(self):
"""Return iterable over mechanize.Link-like objects."""
if self._links_genf is None:
- self._links_genf = CachingGeneratorFunction(
- self._links_factory.links())
+ try:
+ self._links_genf = CachingGeneratorFunction(
+ self._links_factory.links())
+ except:
+ self.set_response(self._response)
+ raise
return self._links_genf()
class DefaultFactory(Factory):
Modified: wwwsearch/mechanize/trunk/test/test_forms.doctest
==============================================================================
--- wwwsearch/mechanize/trunk/test/test_forms.doctest (original)
+++ wwwsearch/mechanize/trunk/test/test_forms.doctest Fri Dec 29 21:40:06 2006
@@ -33,3 +33,27 @@
>>> br.find_control(type="isindex").value = "blah"
>>> br.click(type="isindex").get_full_url()
'http://example.com/?blah'
+
+
+If something (e.g. calling .forms() triggers parsing, and parsing
+fails, the next attempt should not succeed! This used to happen
+because the response held by LinksFactory etc was stale, since it had
+already been .read(). Fixed by calling Factory.set_response() on
+error.
+
+>>> import mechanize, sgmllib
+>>> br = mechanize.Browser()
+>>> r = mechanize._response.test_html_response("""\
+... <form>
+... <input type="text" name="foo" value="a"></input><!!!>
+... <input type="text" name="bar" value="b"></input>
+... </form>
+... """)
+>>> br.set_response(r)
+>>> try:
+... br.select_form(nr=0)
+... except sgmllib.SGMLParseError:
+... pass
+>>> br.select_form(nr=0) # doctest: +IGNORE_EXCEPTION_DETAIL
+Traceback (most recent call last):
+SGMLParseError: expected name token
More information about the wwwsearch-commits
mailing list