blob: 20a1278f38a165cad0822d857583e30ae8e98fbb [file] [log] [blame]
>>> import StringIO
>>> from mechanize import _opener, HTTPError
Normal case. Response goes through hook function and is returned.
>>> def urlopen(fullurl, data=None, timeout=None):
... print "fullurl %r" % fullurl
... print "data %r" % data
... return "response"
>>> def response_hook(response):
... print "response %r" % response
... return "processed response"
>>> _opener.wrapped_open(urlopen, response_hook,
... "http://example.com", "data"
... )
fullurl 'http://example.com'
data 'data'
response 'response'
'processed response'
Raised HTTPError exceptions still go through the response hook but
the result is raised rather than returned.
>>> def urlopen(fullurl, data=None, timeout=None):
... print "fullurl %r" % fullurl
... print "data %r" % data
... raise HTTPError(
... "http://example.com", 200, "OK", {}, StringIO.StringIO())
>>> def response_hook(response):
... print "response class", response.__class__.__name__
... return Exception("processed response")
>>> try:
... _opener.wrapped_open(urlopen, response_hook,
... "http://example.com", "data"
... )
... except Exception, exc:
... print exc
fullurl 'http://example.com'
data 'data'
response class HTTPError
processed response
Other exceptions get ignored, since they're not response objects.
>>> def urlopen(fullurl, data=None, timeout=None):
... print "fullurl %r" % fullurl
... print "data %r" % data
... raise Exception("not caught")
>>> try:
... _opener.wrapped_open(urlopen, response_hook,
... "http://example.com", "data"
... )
... except Exception, exc:
... print exc
fullurl 'http://example.com'
data 'data'
not caught