summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2008-06-14 20:20:24 (GMT)
committerGuido van Rossum <guido@python.org>2008-06-14 20:20:24 (GMT)
commitb4fb6e4d27b56c2119bd1dad83afce874e34a72a (patch)
tree8d3d70b49164329da4a47c9d08364f18018b16df /Lib
parent973124fd70687f6032a1d6e3a0b9e640d2691133 (diff)
downloadcpython-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')
-rw-r--r--Lib/test/test_exceptions.py7
-rw-r--r--Lib/test/test_raise.py122
2 files changed, 102 insertions, 27 deletions
diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
index 9068554..55a57ba 100644
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -480,7 +480,12 @@ class ExceptionTests(unittest.TestCase):
inner_raising_func()
except:
raise KeyError
- except KeyError:
+ except KeyError as e:
+ # We want to test that the except block above got rid of
+ # the exception raised in inner_raising_func(), but it
+ # also ends up in the __context__ of the KeyError, so we
+ # must clear the latter manually for our test to succeed.
+ e.__context__ = None
obj = None
obj = wr()
self.failUnless(obj is None, "%s" % obj)
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):