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