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

Python code coverage for Lib/test/test_raise.py

#countcontent
1n/a# Copyright 2007 Google, Inc. All Rights Reserved.
2n/a# Licensed to PSF under a Contributor Agreement.
3n/a
4n/a"""Tests for the raise statement."""
5n/a
6n/afrom test import support
7n/aimport sys
8n/aimport types
9n/aimport unittest
10n/a
11n/a
12n/adef get_tb():
13n/a try:
14n/a raise OSError()
15n/a except:
16n/a return sys.exc_info()[2]
17n/a
18n/a
19n/aclass Context:
20n/a def __enter__(self):
21n/a return self
22n/a def __exit__(self, exc_type, exc_value, exc_tb):
23n/a return True
24n/a
25n/a
26n/aclass TestRaise(unittest.TestCase):
27n/a def test_invalid_reraise(self):
28n/a try:
29n/a raise
30n/a except RuntimeError as e:
31n/a self.assertIn("No active exception", str(e))
32n/a else:
33n/a self.fail("No exception raised")
34n/a
35n/a def test_reraise(self):
36n/a try:
37n/a try:
38n/a raise IndexError()
39n/a except IndexError as e:
40n/a exc1 = e
41n/a raise
42n/a except IndexError as exc2:
43n/a self.assertTrue(exc1 is exc2)
44n/a else:
45n/a self.fail("No exception raised")
46n/a
47n/a def test_except_reraise(self):
48n/a def reraise():
49n/a try:
50n/a raise TypeError("foo")
51n/a except:
52n/a try:
53n/a raise KeyError("caught")
54n/a except KeyError:
55n/a pass
56n/a raise
57n/a self.assertRaises(TypeError, reraise)
58n/a
59n/a def test_finally_reraise(self):
60n/a def reraise():
61n/a try:
62n/a raise TypeError("foo")
63n/a except:
64n/a try:
65n/a raise KeyError("caught")
66n/a finally:
67n/a raise
68n/a self.assertRaises(KeyError, reraise)
69n/a
70n/a def test_nested_reraise(self):
71n/a def nested_reraise():
72n/a raise
73n/a def reraise():
74n/a try:
75n/a raise TypeError("foo")
76n/a except:
77n/a nested_reraise()
78n/a self.assertRaises(TypeError, reraise)
79n/a
80n/a def test_raise_from_None(self):
81n/a try:
82n/a try:
83n/a raise TypeError("foo")
84n/a except:
85n/a raise ValueError() from None
86n/a except ValueError as e:
87n/a self.assertTrue(isinstance(e.__context__, TypeError))
88n/a self.assertIsNone(e.__cause__)
89n/a
90n/a def test_with_reraise1(self):
91n/a def reraise():
92n/a try:
93n/a raise TypeError("foo")
94n/a except:
95n/a with Context():
96n/a pass
97n/a raise
98n/a self.assertRaises(TypeError, reraise)
99n/a
100n/a def test_with_reraise2(self):
101n/a def reraise():
102n/a try:
103n/a raise TypeError("foo")
104n/a except:
105n/a with Context():
106n/a raise KeyError("caught")
107n/a raise
108n/a self.assertRaises(TypeError, reraise)
109n/a
110n/a def test_yield_reraise(self):
111n/a def reraise():
112n/a try:
113n/a raise TypeError("foo")
114n/a except:
115n/a yield 1
116n/a raise
117n/a g = reraise()
118n/a next(g)
119n/a self.assertRaises(TypeError, lambda: next(g))
120n/a self.assertRaises(StopIteration, lambda: next(g))
121n/a
122n/a def test_erroneous_exception(self):
123n/a class MyException(Exception):
124n/a def __init__(self):
125n/a raise RuntimeError()
126n/a
127n/a try:
128n/a raise MyException
129n/a except RuntimeError:
130n/a pass
131n/a else:
132n/a self.fail("No exception raised")
133n/a
134n/a def test_new_returns_invalid_instance(self):
135n/a # See issue #11627.
136n/a class MyException(Exception):
137n/a def __new__(cls, *args):
138n/a return object()
139n/a
140n/a with self.assertRaises(TypeError):
141n/a raise MyException
142n/a
143n/a def test_assert_with_tuple_arg(self):
144n/a try:
145n/a assert False, (3,)
146n/a except AssertionError as e:
147n/a self.assertEqual(str(e), "(3,)")
148n/a
149n/a
150n/a
151n/aclass TestCause(unittest.TestCase):
152n/a
153n/a def testCauseSyntax(self):
154n/a try:
155n/a try:
156n/a try:
157n/a raise TypeError
158n/a except Exception:
159n/a raise ValueError from None
160n/a except ValueError as exc:
161n/a self.assertIsNone(exc.__cause__)
162n/a self.assertTrue(exc.__suppress_context__)
163n/a exc.__suppress_context__ = False
164n/a raise exc
165n/a except ValueError as exc:
166n/a e = exc
167n/a
168n/a self.assertIsNone(e.__cause__)
169n/a self.assertFalse(e.__suppress_context__)
170n/a self.assertIsInstance(e.__context__, TypeError)
171n/a
172n/a def test_invalid_cause(self):
173n/a try:
174n/a raise IndexError from 5
175n/a except TypeError as e:
176n/a self.assertIn("exception cause", str(e))
177n/a else:
178n/a self.fail("No exception raised")
179n/a
180n/a def test_class_cause(self):
181n/a try:
182n/a raise IndexError from KeyError
183n/a except IndexError as e:
184n/a self.assertIsInstance(e.__cause__, KeyError)
185n/a else:
186n/a self.fail("No exception raised")
187n/a
188n/a def test_instance_cause(self):
189n/a cause = KeyError()
190n/a try:
191n/a raise IndexError from cause
192n/a except IndexError as e:
193n/a self.assertTrue(e.__cause__ is cause)
194n/a else:
195n/a self.fail("No exception raised")
196n/a
197n/a def test_erroneous_cause(self):
198n/a class MyException(Exception):
199n/a def __init__(self):
200n/a raise RuntimeError()
201n/a
202n/a try:
203n/a raise IndexError from MyException
204n/a except RuntimeError:
205n/a pass
206n/a else:
207n/a self.fail("No exception raised")
208n/a
209n/a
210n/aclass TestTraceback(unittest.TestCase):
211n/a
212n/a def test_sets_traceback(self):
213n/a try:
214n/a raise IndexError()
215n/a except IndexError as e:
216n/a self.assertIsInstance(e.__traceback__, types.TracebackType)
217n/a else:
218n/a self.fail("No exception raised")
219n/a
220n/a def test_accepts_traceback(self):
221n/a tb = get_tb()
222n/a try:
223n/a raise IndexError().with_traceback(tb)
224n/a except IndexError as e:
225n/a self.assertNotEqual(e.__traceback__, tb)
226n/a self.assertEqual(e.__traceback__.tb_next, tb)
227n/a else:
228n/a self.fail("No exception raised")
229n/a
230n/a
231n/aclass TestContext(unittest.TestCase):
232n/a def test_instance_context_instance_raise(self):
233n/a context = IndexError()
234n/a try:
235n/a try:
236n/a raise context
237n/a except:
238n/a raise OSError()
239n/a except OSError as e:
240n/a self.assertEqual(e.__context__, context)
241n/a else:
242n/a self.fail("No exception raised")
243n/a
244n/a def test_class_context_instance_raise(self):
245n/a context = IndexError
246n/a try:
247n/a try:
248n/a raise context
249n/a except:
250n/a raise OSError()
251n/a except OSError as e:
252n/a self.assertNotEqual(e.__context__, context)
253n/a self.assertIsInstance(e.__context__, context)
254n/a else:
255n/a self.fail("No exception raised")
256n/a
257n/a def test_class_context_class_raise(self):
258n/a context = IndexError
259n/a try:
260n/a try:
261n/a raise context
262n/a except:
263n/a raise OSError
264n/a except OSError as e:
265n/a self.assertNotEqual(e.__context__, context)
266n/a self.assertIsInstance(e.__context__, context)
267n/a else:
268n/a self.fail("No exception raised")
269n/a
270n/a def test_c_exception_context(self):
271n/a try:
272n/a try:
273n/a 1/0
274n/a except:
275n/a raise OSError
276n/a except OSError as e:
277n/a self.assertIsInstance(e.__context__, ZeroDivisionError)
278n/a else:
279n/a self.fail("No exception raised")
280n/a
281n/a def test_c_exception_raise(self):
282n/a try:
283n/a try:
284n/a 1/0
285n/a except:
286n/a xyzzy
287n/a except NameError as e:
288n/a self.assertIsInstance(e.__context__, ZeroDivisionError)
289n/a else:
290n/a self.fail("No exception raised")
291n/a
292n/a def test_noraise_finally(self):
293n/a try:
294n/a try:
295n/a pass
296n/a finally:
297n/a raise OSError
298n/a except OSError as e:
299n/a self.assertTrue(e.__context__ is None)
300n/a else:
301n/a self.fail("No exception raised")
302n/a
303n/a def test_raise_finally(self):
304n/a try:
305n/a try:
306n/a 1/0
307n/a finally:
308n/a raise OSError
309n/a except OSError as e:
310n/a self.assertIsInstance(e.__context__, ZeroDivisionError)
311n/a else:
312n/a self.fail("No exception raised")
313n/a
314n/a def test_context_manager(self):
315n/a class ContextManager:
316n/a def __enter__(self):
317n/a pass
318n/a def __exit__(self, t, v, tb):
319n/a xyzzy
320n/a try:
321n/a with ContextManager():
322n/a 1/0
323n/a except NameError as e:
324n/a self.assertIsInstance(e.__context__, ZeroDivisionError)
325n/a else:
326n/a self.fail("No exception raised")
327n/a
328n/a def test_cycle_broken(self):
329n/a # Self-cycles (when re-raising a caught exception) are broken
330n/a try:
331n/a try:
332n/a 1/0
333n/a except ZeroDivisionError as e:
334n/a raise e
335n/a except ZeroDivisionError as e:
336n/a self.assertTrue(e.__context__ is None, e.__context__)
337n/a
338n/a def test_reraise_cycle_broken(self):
339n/a # Non-trivial context cycles (through re-raising a previous exception)
340n/a # are broken too.
341n/a try:
342n/a try:
343n/a xyzzy
344n/a except NameError as a:
345n/a try:
346n/a 1/0
347n/a except ZeroDivisionError:
348n/a raise a
349n/a except NameError as e:
350n/a self.assertTrue(e.__context__.__context__ is None)
351n/a
352n/a def test_3118(self):
353n/a # deleting the generator caused the __context__ to be cleared
354n/a def gen():
355n/a try:
356n/a yield 1
357n/a finally:
358n/a pass
359n/a
360n/a def f():
361n/a g = gen()
362n/a next(g)
363n/a try:
364n/a try:
365n/a raise ValueError
366n/a except:
367n/a del g
368n/a raise KeyError
369n/a except Exception as e:
370n/a self.assertIsInstance(e.__context__, ValueError)
371n/a
372n/a f()
373n/a
374n/a def test_3611(self):
375n/a # A re-raised exception in a __del__ caused the __context__
376n/a # to be cleared
377n/a class C:
378n/a def __del__(self):
379n/a try:
380n/a 1/0
381n/a except:
382n/a raise
383n/a
384n/a def f():
385n/a x = C()
386n/a try:
387n/a try:
388n/a x.x
389n/a except AttributeError:
390n/a del x
391n/a raise TypeError
392n/a except Exception as e:
393n/a self.assertNotEqual(e.__context__, None)
394n/a self.assertIsInstance(e.__context__, AttributeError)
395n/a
396n/a with support.captured_output("stderr"):
397n/a f()
398n/a
399n/aclass TestRemovedFunctionality(unittest.TestCase):
400n/a def test_tuples(self):
401n/a try:
402n/a raise (IndexError, KeyError) # This should be a tuple!
403n/a except TypeError:
404n/a pass
405n/a else:
406n/a self.fail("No exception raised")
407n/a
408n/a def test_strings(self):
409n/a try:
410n/a raise "foo"
411n/a except TypeError:
412n/a pass
413n/a else:
414n/a self.fail("No exception raised")
415n/a
416n/a
417n/aif __name__ == "__main__":
418n/a unittest.main()