diff options
author | Benjamin Peterson <benjamin@python.org> | 2008-06-11 15:59:43 +0000 |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2008-06-11 15:59:43 +0000 |
commit | eec3d7137929611b98dd593cd2f122cd91b723b2 (patch) | |
tree | 42721419d4fe3f53961ecfd7c1dea3224188ae40 /Lib/test/test_exceptions.py | |
parent | e8465f2b413174084fcc2dc4cd7a53122c62ce4b (diff) | |
download | cpython-git-eec3d7137929611b98dd593cd2f122cd91b723b2.tar.gz |
#3021: Antoine Pitrou's Lexical exception handlers
Diffstat (limited to 'Lib/test/test_exceptions.py')
-rw-r--r-- | Lib/test/test_exceptions.py | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py index 41b9413f41..906855403e 100644 --- a/Lib/test/test_exceptions.py +++ b/Lib/test/test_exceptions.py @@ -427,6 +427,7 @@ class ExceptionTests(unittest.TestCase): local_ref = obj raise MyException(obj) + # Qualified "except" with "as" obj = MyObj() wr = weakref.ref(obj) try: @@ -437,6 +438,113 @@ class ExceptionTests(unittest.TestCase): obj = wr() self.failUnless(obj is None, "%s" % obj) + # Qualified "except" without "as" + obj = MyObj() + wr = weakref.ref(obj) + try: + inner_raising_func() + except MyException: + pass + obj = None + obj = wr() + self.failUnless(obj is None, "%s" % obj) + + # Bare "except" + obj = MyObj() + wr = weakref.ref(obj) + try: + inner_raising_func() + except: + pass + obj = None + obj = wr() + self.failUnless(obj is None, "%s" % obj) + + # "except" with premature block leave + obj = MyObj() + wr = weakref.ref(obj) + for i in [0]: + try: + inner_raising_func() + except: + break + obj = None + obj = wr() + self.failUnless(obj is None, "%s" % obj) + + # "except" block raising another exception + obj = MyObj() + wr = weakref.ref(obj) + try: + try: + inner_raising_func() + except: + raise KeyError + except KeyError: + obj = None + obj = wr() + self.failUnless(obj is None, "%s" % obj) + + # Some complicated construct + obj = MyObj() + wr = weakref.ref(obj) + try: + inner_raising_func() + except MyException: + try: + try: + raise + finally: + raise + except MyException: + pass + obj = None + obj = wr() + self.failUnless(obj is None, "%s" % obj) + + # Inside an exception-silencing "with" block + class Context: + def __enter__(self): + return self + def __exit__ (self, exc_type, exc_value, exc_tb): + return True + obj = MyObj() + wr = weakref.ref(obj) + with Context(): + inner_raising_func() + obj = None + obj = wr() + self.failUnless(obj is None, "%s" % obj) + + def test_generator_leaking(self): + # Test that generator exception state doesn't leak into the calling + # frame + def yield_raise(): + try: + raise KeyError("caught") + except KeyError: + yield sys.exc_info()[0] + yield sys.exc_info()[0] + yield sys.exc_info()[0] + g = yield_raise() + self.assertEquals(next(g), KeyError) + self.assertEquals(sys.exc_info()[0], None) + self.assertEquals(next(g), KeyError) + self.assertEquals(sys.exc_info()[0], None) + self.assertEquals(next(g), None) + + # Same test, but inside an exception handler + try: + raise TypeError("foo") + except TypeError: + g = yield_raise() + self.assertEquals(next(g), KeyError) + self.assertEquals(sys.exc_info()[0], TypeError) + self.assertEquals(next(g), KeyError) + self.assertEquals(sys.exc_info()[0], TypeError) + self.assertEquals(next(g), TypeError) + del g + self.assertEquals(sys.exc_info()[0], TypeError) def test_main(): run_unittest(ExceptionTests) |