ยปCore Development>Code coverage>Lib/urllib/response.py

Python code coverage for Lib/urllib/response.py

#countcontent
1n/a"""Response classes used by urllib.
2n/a
3n/aThe base class, addbase, defines a minimal file-like interface,
4n/aincluding read() and readline(). The typical response object is an
5n/aaddinfourl instance, which defines an info() method that returns
6n/aheaders and a geturl() method that returns the url.
7n/a"""
8n/a
9n/aimport tempfile
10n/a
11n/a__all__ = ['addbase', 'addclosehook', 'addinfo', 'addinfourl']
12n/a
13n/a
14n/aclass addbase(tempfile._TemporaryFileWrapper):
15n/a """Base class for addinfo and addclosehook. Is a good idea for garbage collection."""
16n/a
17n/a # XXX Add a method to expose the timeout on the underlying socket?
18n/a
19n/a def __init__(self, fp):
20n/a super(addbase, self).__init__(fp, '<urllib response>', delete=False)
21n/a # Keep reference around as this was part of the original API.
22n/a self.fp = fp
23n/a
24n/a def __repr__(self):
25n/a return '<%s at %r whose fp = %r>' % (self.__class__.__name__,
26n/a id(self), self.file)
27n/a
28n/a def __enter__(self):
29n/a if self.fp.closed:
30n/a raise ValueError("I/O operation on closed file")
31n/a return self
32n/a
33n/a def __exit__(self, type, value, traceback):
34n/a self.close()
35n/a
36n/a
37n/aclass addclosehook(addbase):
38n/a """Class to add a close hook to an open file."""
39n/a
40n/a def __init__(self, fp, closehook, *hookargs):
41n/a super(addclosehook, self).__init__(fp)
42n/a self.closehook = closehook
43n/a self.hookargs = hookargs
44n/a
45n/a def close(self):
46n/a try:
47n/a closehook = self.closehook
48n/a hookargs = self.hookargs
49n/a if closehook:
50n/a self.closehook = None
51n/a self.hookargs = None
52n/a closehook(*hookargs)
53n/a finally:
54n/a super(addclosehook, self).close()
55n/a
56n/a
57n/aclass addinfo(addbase):
58n/a """class to add an info() method to an open file."""
59n/a
60n/a def __init__(self, fp, headers):
61n/a super(addinfo, self).__init__(fp)
62n/a self.headers = headers
63n/a
64n/a def info(self):
65n/a return self.headers
66n/a
67n/a
68n/aclass addinfourl(addinfo):
69n/a """class to add info() and geturl() methods to an open file."""
70n/a
71n/a def __init__(self, fp, headers, url, code=None):
72n/a super(addinfourl, self).__init__(fp, headers)
73n/a self.url = url
74n/a self.code = code
75n/a
76n/a def getcode(self):
77n/a return self.code
78n/a
79n/a def geturl(self):
80n/a return self.url