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

Python code coverage for Lib/urllib/error.py

#countcontent
1n/a"""Exception classes raised by urllib.
2n/a
3n/aThe base exception class is URLError, which inherits from OSError. It
4n/adoesn't define any behavior of its own, but is the base class for all
5n/aexceptions defined in this package.
6n/a
7n/aHTTPError is an exception class that is also a valid HTTP response
8n/ainstance. It behaves this way because HTTP protocol errors are valid
9n/aresponses, with a status code, headers, and a body. In some contexts,
10n/aan application may want to handle an exception like a regular
11n/aresponse.
12n/a"""
13n/a
14n/aimport urllib.response
15n/a
16n/a__all__ = ['URLError', 'HTTPError', 'ContentTooShortError']
17n/a
18n/a
19n/a# do these error classes make sense?
20n/a# make sure all of the OSError stuff is overridden. we just want to be
21n/a# subtypes.
22n/a
23n/aclass URLError(OSError):
24n/a # URLError is a sub-type of OSError, but it doesn't share any of
25n/a # the implementation. need to override __init__ and __str__.
26n/a # It sets self.args for compatibility with other EnvironmentError
27n/a # subclasses, but args doesn't have the typical format with errno in
28n/a # slot 0 and strerror in slot 1. This may be better than nothing.
29n/a def __init__(self, reason, filename=None):
30n/a self.args = reason,
31n/a self.reason = reason
32n/a if filename is not None:
33n/a self.filename = filename
34n/a
35n/a def __str__(self):
36n/a return '<urlopen error %s>' % self.reason
37n/a
38n/a
39n/aclass HTTPError(URLError, urllib.response.addinfourl):
40n/a """Raised when HTTP error occurs, but also acts like non-error return"""
41n/a __super_init = urllib.response.addinfourl.__init__
42n/a
43n/a def __init__(self, url, code, msg, hdrs, fp):
44n/a self.code = code
45n/a self.msg = msg
46n/a self.hdrs = hdrs
47n/a self.fp = fp
48n/a self.filename = url
49n/a # The addinfourl classes depend on fp being a valid file
50n/a # object. In some cases, the HTTPError may not have a valid
51n/a # file object. If this happens, the simplest workaround is to
52n/a # not initialize the base classes.
53n/a if fp is not None:
54n/a self.__super_init(fp, hdrs, url, code)
55n/a
56n/a def __str__(self):
57n/a return 'HTTP Error %s: %s' % (self.code, self.msg)
58n/a
59n/a def __repr__(self):
60n/a return '<HTTPError %s: %r>' % (self.code, self.msg)
61n/a
62n/a # since URLError specifies a .reason attribute, HTTPError should also
63n/a # provide this attribute. See issue13211 for discussion.
64n/a @property
65n/a def reason(self):
66n/a return self.msg
67n/a
68n/a @property
69n/a def headers(self):
70n/a return self.hdrs
71n/a
72n/a @headers.setter
73n/a def headers(self, headers):
74n/a self.hdrs = headers
75n/a
76n/a
77n/aclass ContentTooShortError(URLError):
78n/a """Exception raised when downloaded size does not match content-length."""
79n/a def __init__(self, message, content):
80n/a URLError.__init__(self, message)
81n/a self.content = content