summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2003-03-01 03:20:41 (GMT)
committerGuido van Rossum <guido@python.org>2003-03-01 03:20:41 (GMT)
commit46d3dc37e4a01e8d72b00ab9ad4e4a07022b7b64 (patch)
treed2c8e67f458cd38115f6865be8034150d9e7242c /Lib/test
parentd1a283be269c66eb813948febc3ce6c9405fb64f (diff)
downloadcpython-46d3dc37e4a01e8d72b00ab9ad4e4a07022b7b64.zip
cpython-46d3dc37e4a01e8d72b00ab9ad4e4a07022b7b64.tar.gz
cpython-46d3dc37e4a01e8d72b00ab9ad4e4a07022b7b64.tar.bz2
- New function sys.exc_clear() clears the current exception. This is
rarely needed, but can sometimes be useful to release objects referenced by the traceback held in sys.exc_info()[2]. (SF patch #693195.) Thanks to Kevin Jacobs!
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_sys.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py
index b18b0c7..2afac65 100644
--- a/Lib/test/test_sys.py
+++ b/Lib/test/test_sys.py
@@ -63,6 +63,50 @@ class SysModuleTest(unittest.TestCase):
# FIXME: testing the code for a lost or replaced excepthook in
# Python/pythonrun.c::PyErr_PrintEx() is tricky.
+ def test_exc_clear(self):
+ self.assertRaises(TypeError, sys.exc_clear, 42)
+
+ # Verify that exc_info is present and matches exc, then clear it, and
+ # check that it worked.
+ def clear_check(exc):
+ typ, value, traceback = sys.exc_info()
+ self.assert_(typ is not None)
+ self.assert_(value is exc)
+ self.assert_(traceback is not None)
+
+ sys.exc_clear()
+
+ typ, value, traceback = sys.exc_info()
+ self.assert_(typ is None)
+ self.assert_(value is None)
+ self.assert_(traceback is None)
+
+ def clear():
+ try:
+ raise ValueError, 42
+ except ValueError, exc:
+ clear_check(exc)
+
+ # Raise an exception and check that it can be cleared
+ clear()
+
+ # Verify that a frame currently handling an exception is
+ # unaffected by calling exc_clear in a nested frame.
+ try:
+ raise ValueError, 13
+ except ValueError, exc:
+ typ1, value1, traceback1 = sys.exc_info()
+ clear()
+ typ2, value2, traceback2 = sys.exc_info()
+
+ self.assert_(typ1 is typ2)
+ self.assert_(value1 is exc)
+ self.assert_(value1 is value2)
+ self.assert_(traceback1 is traceback2)
+
+ # Check that an exception can be cleared outside of an except block
+ clear_check(exc)
+
def test_exit(self):
self.assertRaises(TypeError, sys.exit, 42, 42)