diff options
author | Benjamin Peterson <benjamin@python.org> | 2008-06-11 15:59:43 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2008-06-11 15:59:43 (GMT) |
commit | eec3d7137929611b98dd593cd2f122cd91b723b2 (patch) | |
tree | 42721419d4fe3f53961ecfd7c1dea3224188ae40 /Lib/test/test_raise.py | |
parent | e8465f2b413174084fcc2dc4cd7a53122c62ce4b (diff) | |
download | cpython-eec3d7137929611b98dd593cd2f122cd91b723b2.zip cpython-eec3d7137929611b98dd593cd2f122cd91b723b2.tar.gz cpython-eec3d7137929611b98dd593cd2f122cd91b723b2.tar.bz2 |
#3021: Antoine Pitrou's Lexical exception handlers
Diffstat (limited to 'Lib/test/test_raise.py')
-rw-r--r-- | Lib/test/test_raise.py | 73 |
1 files changed, 72 insertions, 1 deletions
diff --git a/Lib/test/test_raise.py b/Lib/test/test_raise.py index 89e2190..5f0070e 100644 --- a/Lib/test/test_raise.py +++ b/Lib/test/test_raise.py @@ -16,6 +16,13 @@ def get_tb(): return sys.exc_info()[2] +class Context: + def __enter__(self): + return self + def __exit__(self, exc_type, exc_value, exc_tb): + return True + + class TestRaise(unittest.TestCase): def test_invalid_reraise(self): try: @@ -37,6 +44,71 @@ class TestRaise(unittest.TestCase): else: self.fail("No exception raised") + def test_except_reraise(self): + def reraise(): + try: + raise TypeError("foo") + except: + try: + raise KeyError("caught") + except KeyError: + pass + raise + self.assertRaises(TypeError, reraise) + + def test_finally_reraise(self): + def reraise(): + try: + raise TypeError("foo") + except: + try: + raise KeyError("caught") + finally: + raise + self.assertRaises(KeyError, reraise) + + def test_nested_reraise(self): + def nested_reraise(): + raise + def reraise(): + try: + raise TypeError("foo") + except: + nested_reraise() + self.assertRaises(TypeError, reraise) + + def test_with_reraise1(self): + def reraise(): + try: + raise TypeError("foo") + except: + with Context(): + pass + raise + self.assertRaises(TypeError, reraise) + + def test_with_reraise2(self): + def reraise(): + try: + raise TypeError("foo") + except: + with Context(): + raise KeyError("caught") + raise + self.assertRaises(TypeError, reraise) + + def test_yield_reraise(self): + def reraise(): + try: + raise TypeError("foo") + except: + yield 1 + raise + g = reraise() + next(g) + self.assertRaises(TypeError, lambda: next(g)) + self.assertRaises(StopIteration, lambda: next(g)) + def test_erroneous_exception(self): class MyException(Exception): def __init__(self): @@ -158,6 +230,5 @@ class TestRemovedFunctionality(unittest.TestCase): def test_main(): support.run_unittest(__name__) - if __name__ == "__main__": unittest.main() |