>>> import urllib2, StringIO
>>> from mechanize import _opener

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 urllib2.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
