diff options
author | Guido van Rossum <guido@python.org> | 2008-06-14 20:20:24 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2008-06-14 20:20:24 (GMT) |
commit | b4fb6e4d27b56c2119bd1dad83afce874e34a72a (patch) | |
tree | 8d3d70b49164329da4a47c9d08364f18018b16df /Lib/test/test_raise.py | |
parent | 973124fd70687f6032a1d6e3a0b9e640d2691133 (diff) | |
download | cpython-b4fb6e4d27b56c2119bd1dad83afce874e34a72a.zip cpython-b4fb6e4d27b56c2119bd1dad83afce874e34a72a.tar.gz cpython-b4fb6e4d27b56c2119bd1dad83afce874e34a72a.tar.bz2 |
Implicit exception chaining via __context__ (PEP 3134).
Patch 3108 by Antooine Pitrou.
Diffstat (limited to 'Lib/test/test_raise.py')
-rw-r--r-- | Lib/test/test_raise.py | 122 |
1 files changed, 96 insertions, 26 deletions
diff --git a/Lib/test/test_raise.py b/Lib/test/test_raise.py index 5f0070e..3072c14 100644 --- a/Lib/test/test_raise.py +++ b/Lib/test/test_raise.py @@ -181,32 +181,102 @@ class TestTraceback(unittest.TestCase): self.fail("No exception raised") -# Disabled until context is implemented -# class TestContext(object): -# def test_instance_context_bare_raise(self): -# context = IndexError() -# try: -# try: -# raise context -# except: -# raise OSError() -# except OSError as e: -# self.assertEqual(e.__context__, context) -# else: -# self.fail("No exception raised") -# -# def test_class_context_bare_raise(self): -# context = IndexError -# try: -# try: -# raise context -# except: -# raise OSError() -# except OSError as e: -# self.assertNotEqual(e.__context__, context) -# self.failUnless(isinstance(e.__context__, context)) -# else: -# self.fail("No exception raised") +class TestContext(unittest.TestCase): + def test_instance_context_instance_raise(self): + context = IndexError() + try: + try: + raise context + except: + raise OSError() + except OSError as e: + self.assertEqual(e.__context__, context) + else: + self.fail("No exception raised") + + def test_class_context_instance_raise(self): + context = IndexError + try: + try: + raise context + except: + raise OSError() + except OSError as e: + self.assertNotEqual(e.__context__, context) + self.failUnless(isinstance(e.__context__, context)) + else: + self.fail("No exception raised") + + def test_class_context_class_raise(self): + context = IndexError + try: + try: + raise context + except: + raise OSError + except OSError as e: + self.assertNotEqual(e.__context__, context) + self.failUnless(isinstance(e.__context__, context)) + else: + self.fail("No exception raised") + + def test_c_exception_context(self): + try: + try: + 1/0 + except: + raise OSError + except OSError as e: + self.failUnless(isinstance(e.__context__, ZeroDivisionError)) + else: + self.fail("No exception raised") + + def test_c_exception_raise(self): + try: + try: + 1/0 + except: + xyzzy + except NameError as e: + self.failUnless(isinstance(e.__context__, ZeroDivisionError)) + else: + self.fail("No exception raised") + + def test_noraise_finally(self): + try: + try: + pass + finally: + raise OSError + except OSError as e: + self.failUnless(e.__context__ is None) + else: + self.fail("No exception raised") + + def test_raise_finally(self): + try: + try: + 1/0 + finally: + raise OSError + except OSError as e: + self.failUnless(isinstance(e.__context__, ZeroDivisionError)) + else: + self.fail("No exception raised") + + def test_context_manager(self): + class ContextManager: + def __enter__(self): + pass + def __exit__(self, t, v, tb): + xyzzy + try: + with ContextManager(): + 1/0 + except NameError as e: + self.failUnless(isinstance(e.__context__, ZeroDivisionError)) + else: + self.fail("No exception raised") class TestRemovedFunctionality(unittest.TestCase): |