Debian Codegraph @

test/test_opener.doctest

raw
1 >>> import io
2 >>> from mechanize import _opener, HTTPError
3
4 Normal case. Response goes through hook function and is returned.
5
6 >>> def urlopen(fullurl, data=None, timeout=None):
7 ... print("fullurl %r" % fullurl)
8 ... print("data %r" % data)
9 ... return "response"
10 >>> def response_hook(response):
11 ... print("response %r" % response)
12 ... return "processed response"
13 >>> _opener.wrapped_open(urlopen, response_hook,
14 ... "http://example.com", "data"
15 ... )
16 fullurl 'http://example.com'
17 data 'data'
18 response 'response'
19 'processed response'
20
21
22 Raised HTTPError exceptions still go through the response hook but
23 the result is raised rather than returned.
24
25 >>> def urlopen(fullurl, data=None, timeout=None):
26 ... print("fullurl %r" % fullurl)
27 ... print("data %r" % data)
28 ... raise HTTPError(
29 ... "http://example.com", 200, "OK", {}, io.BytesIO())
30 >>> def response_hook(response):
31 ... print("response class %s" % response.__class__.__name__)
32 ... return Exception("processed response")
33 >>> try:
34 ... _opener.wrapped_open(urlopen, response_hook,
35 ... "http://example.com", "data"
36 ... )
37 ... except Exception as exc:
38 ... print(exc)
39 fullurl 'http://example.com'
40 data 'data'
41 response class HTTPError
42 processed response
43
44 Other exceptions get ignored, since they're not response objects.
45
46 >>> def urlopen(fullurl, data=None, timeout=None):
47 ... print("fullurl %r" % fullurl)
48 ... print("data %r" % data)
49 ... raise Exception("not caught")
50 >>> try:
51 ... _opener.wrapped_open(urlopen, response_hook,
52 ... "http://example.com", "data"
53 ... )
54 ... except Exception as exc:
55 ... print(exc)
56 fullurl 'http://example.com'
57 data 'data'
58 not caught