ยปCore Development>Code coverage>Lib/test/test_exception_hierarchy.py

Python code coverage for Lib/test/test_exception_hierarchy.py

#countcontent
1n/aimport builtins
2n/aimport os
3n/aimport select
4n/aimport socket
5n/aimport unittest
6n/aimport errno
7n/afrom errno import EEXIST
8n/a
9n/a
10n/aclass SubOSError(OSError):
11n/a pass
12n/a
13n/aclass SubOSErrorWithInit(OSError):
14n/a def __init__(self, message, bar):
15n/a self.bar = bar
16n/a super().__init__(message)
17n/a
18n/aclass SubOSErrorWithNew(OSError):
19n/a def __new__(cls, message, baz):
20n/a self = super().__new__(cls, message)
21n/a self.baz = baz
22n/a return self
23n/a
24n/aclass SubOSErrorCombinedInitFirst(SubOSErrorWithInit, SubOSErrorWithNew):
25n/a pass
26n/a
27n/aclass SubOSErrorCombinedNewFirst(SubOSErrorWithNew, SubOSErrorWithInit):
28n/a pass
29n/a
30n/aclass SubOSErrorWithStandaloneInit(OSError):
31n/a def __init__(self):
32n/a pass
33n/a
34n/a
35n/aclass HierarchyTest(unittest.TestCase):
36n/a
37n/a def test_builtin_errors(self):
38n/a self.assertEqual(OSError.__name__, 'OSError')
39n/a self.assertIs(IOError, OSError)
40n/a self.assertIs(EnvironmentError, OSError)
41n/a
42n/a def test_socket_errors(self):
43n/a self.assertIs(socket.error, IOError)
44n/a self.assertIs(socket.gaierror.__base__, OSError)
45n/a self.assertIs(socket.herror.__base__, OSError)
46n/a self.assertIs(socket.timeout.__base__, OSError)
47n/a
48n/a def test_select_error(self):
49n/a self.assertIs(select.error, OSError)
50n/a
51n/a # mmap.error is tested in test_mmap
52n/a
53n/a _pep_map = """
54n/a +-- BlockingIOError EAGAIN, EALREADY, EWOULDBLOCK, EINPROGRESS
55n/a +-- ChildProcessError ECHILD
56n/a +-- ConnectionError
57n/a +-- BrokenPipeError EPIPE, ESHUTDOWN
58n/a +-- ConnectionAbortedError ECONNABORTED
59n/a +-- ConnectionRefusedError ECONNREFUSED
60n/a +-- ConnectionResetError ECONNRESET
61n/a +-- FileExistsError EEXIST
62n/a +-- FileNotFoundError ENOENT
63n/a +-- InterruptedError EINTR
64n/a +-- IsADirectoryError EISDIR
65n/a +-- NotADirectoryError ENOTDIR
66n/a +-- PermissionError EACCES, EPERM
67n/a +-- ProcessLookupError ESRCH
68n/a +-- TimeoutError ETIMEDOUT
69n/a """
70n/a def _make_map(s):
71n/a _map = {}
72n/a for line in s.splitlines():
73n/a line = line.strip('+- ')
74n/a if not line:
75n/a continue
76n/a excname, _, errnames = line.partition(' ')
77n/a for errname in filter(None, errnames.strip().split(', ')):
78n/a _map[getattr(errno, errname)] = getattr(builtins, excname)
79n/a return _map
80n/a _map = _make_map(_pep_map)
81n/a
82n/a def test_errno_mapping(self):
83n/a # The OSError constructor maps errnos to subclasses
84n/a # A sample test for the basic functionality
85n/a e = OSError(EEXIST, "Bad file descriptor")
86n/a self.assertIs(type(e), FileExistsError)
87n/a # Exhaustive testing
88n/a for errcode, exc in self._map.items():
89n/a e = OSError(errcode, "Some message")
90n/a self.assertIs(type(e), exc)
91n/a othercodes = set(errno.errorcode) - set(self._map)
92n/a for errcode in othercodes:
93n/a e = OSError(errcode, "Some message")
94n/a self.assertIs(type(e), OSError)
95n/a
96n/a def test_try_except(self):
97n/a filename = "some_hopefully_non_existing_file"
98n/a
99n/a # This checks that try .. except checks the concrete exception
100n/a # (FileNotFoundError) and not the base type specified when
101n/a # PyErr_SetFromErrnoWithFilenameObject was called.
102n/a # (it is therefore deliberate that it doesn't use assertRaises)
103n/a try:
104n/a open(filename)
105n/a except FileNotFoundError:
106n/a pass
107n/a else:
108n/a self.fail("should have raised a FileNotFoundError")
109n/a
110n/a # Another test for PyErr_SetExcFromWindowsErrWithFilenameObject()
111n/a self.assertFalse(os.path.exists(filename))
112n/a try:
113n/a os.unlink(filename)
114n/a except FileNotFoundError:
115n/a pass
116n/a else:
117n/a self.fail("should have raised a FileNotFoundError")
118n/a
119n/a
120n/aclass AttributesTest(unittest.TestCase):
121n/a
122n/a def test_windows_error(self):
123n/a if os.name == "nt":
124n/a self.assertIn('winerror', dir(OSError))
125n/a else:
126n/a self.assertNotIn('winerror', dir(OSError))
127n/a
128n/a def test_posix_error(self):
129n/a e = OSError(EEXIST, "File already exists", "foo.txt")
130n/a self.assertEqual(e.errno, EEXIST)
131n/a self.assertEqual(e.args[0], EEXIST)
132n/a self.assertEqual(e.strerror, "File already exists")
133n/a self.assertEqual(e.filename, "foo.txt")
134n/a if os.name == "nt":
135n/a self.assertEqual(e.winerror, None)
136n/a
137n/a @unittest.skipUnless(os.name == "nt", "Windows-specific test")
138n/a def test_errno_translation(self):
139n/a # ERROR_ALREADY_EXISTS (183) -> EEXIST
140n/a e = OSError(0, "File already exists", "foo.txt", 183)
141n/a self.assertEqual(e.winerror, 183)
142n/a self.assertEqual(e.errno, EEXIST)
143n/a self.assertEqual(e.args[0], EEXIST)
144n/a self.assertEqual(e.strerror, "File already exists")
145n/a self.assertEqual(e.filename, "foo.txt")
146n/a
147n/a def test_blockingioerror(self):
148n/a args = ("a", "b", "c", "d", "e")
149n/a for n in range(6):
150n/a e = BlockingIOError(*args[:n])
151n/a with self.assertRaises(AttributeError):
152n/a e.characters_written
153n/a e = BlockingIOError("a", "b", 3)
154n/a self.assertEqual(e.characters_written, 3)
155n/a e.characters_written = 5
156n/a self.assertEqual(e.characters_written, 5)
157n/a
158n/a
159n/aclass ExplicitSubclassingTest(unittest.TestCase):
160n/a
161n/a def test_errno_mapping(self):
162n/a # When constructing an OSError subclass, errno mapping isn't done
163n/a e = SubOSError(EEXIST, "Bad file descriptor")
164n/a self.assertIs(type(e), SubOSError)
165n/a
166n/a def test_init_overridden(self):
167n/a e = SubOSErrorWithInit("some message", "baz")
168n/a self.assertEqual(e.bar, "baz")
169n/a self.assertEqual(e.args, ("some message",))
170n/a
171n/a def test_init_kwdargs(self):
172n/a e = SubOSErrorWithInit("some message", bar="baz")
173n/a self.assertEqual(e.bar, "baz")
174n/a self.assertEqual(e.args, ("some message",))
175n/a
176n/a def test_new_overridden(self):
177n/a e = SubOSErrorWithNew("some message", "baz")
178n/a self.assertEqual(e.baz, "baz")
179n/a self.assertEqual(e.args, ("some message",))
180n/a
181n/a def test_new_kwdargs(self):
182n/a e = SubOSErrorWithNew("some message", baz="baz")
183n/a self.assertEqual(e.baz, "baz")
184n/a self.assertEqual(e.args, ("some message",))
185n/a
186n/a def test_init_new_overridden(self):
187n/a e = SubOSErrorCombinedInitFirst("some message", "baz")
188n/a self.assertEqual(e.bar, "baz")
189n/a self.assertEqual(e.baz, "baz")
190n/a self.assertEqual(e.args, ("some message",))
191n/a e = SubOSErrorCombinedNewFirst("some message", "baz")
192n/a self.assertEqual(e.bar, "baz")
193n/a self.assertEqual(e.baz, "baz")
194n/a self.assertEqual(e.args, ("some message",))
195n/a
196n/a def test_init_standalone(self):
197n/a # __init__ doesn't propagate to OSError.__init__ (see issue #15229)
198n/a e = SubOSErrorWithStandaloneInit()
199n/a self.assertEqual(e.args, ())
200n/a self.assertEqual(str(e), '')
201n/a
202n/a
203n/aif __name__ == "__main__":
204n/a unittest.main()