summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_exceptions.py
diff options
context:
space:
mode:
authorCollin Winter <collinw@gmail.com>2007-08-31 00:04:24 (GMT)
committerCollin Winter <collinw@gmail.com>2007-08-31 00:04:24 (GMT)
commit828f04ac3f0dd3b68b4dbf42a79ebb846d1de568 (patch)
tree21e25d3d969ce636c32539e4d4b5255dc4c85702 /Lib/test/test_exceptions.py
parent150b7d7d02eca6970d792f3e6887f957a36b6ca2 (diff)
downloadcpython-828f04ac3f0dd3b68b4dbf42a79ebb846d1de568.zip
cpython-828f04ac3f0dd3b68b4dbf42a79ebb846d1de568.tar.gz
cpython-828f04ac3f0dd3b68b4dbf42a79ebb846d1de568.tar.bz2
Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Diffstat (limited to 'Lib/test/test_exceptions.py')
-rw-r--r--Lib/test/test_exceptions.py60
1 files changed, 58 insertions, 2 deletions
diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
index 90f6ae7..d2a2191 100644
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -13,7 +13,7 @@ class ExceptionTests(unittest.TestCase):
def raise_catch(self, exc, excname):
try:
- raise exc, "spam"
+ raise exc("spam")
except exc as err:
buf1 = str(err)
try:
@@ -141,7 +141,7 @@ class ExceptionTests(unittest.TestCase):
class BadException(Exception):
def __init__(self_):
- raise RuntimeError, "can't instantiate BadException"
+ raise RuntimeError("can't instantiate BadException")
class InvalidException:
pass
@@ -305,6 +305,62 @@ class ExceptionTests(unittest.TestCase):
'pickled "%r", attribute "%s' %
(e, checkArgName))
+ def testWithTraceback(self):
+ try:
+ raise IndexError(4)
+ except:
+ tb = sys.exc_info()[2]
+
+ e = BaseException().with_traceback(tb)
+ self.failUnless(isinstance(e, BaseException))
+ self.assertEqual(e.__traceback__, tb)
+
+ e = IndexError(5).with_traceback(tb)
+ self.failUnless(isinstance(e, IndexError))
+ self.assertEqual(e.__traceback__, tb)
+
+ class MyException(Exception):
+ pass
+
+ e = MyException().with_traceback(tb)
+ self.failUnless(isinstance(e, MyException))
+ self.assertEqual(e.__traceback__, tb)
+
+ def testInvalidTraceback(self):
+ try:
+ Exception().__traceback__ = 5
+ except TypeError as e:
+ self.failUnless("__traceback__ must be a traceback" in str(e))
+ else:
+ self.fail("No exception raised")
+
+ def testNoneClearsTracebackAttr(self):
+ try:
+ raise IndexError(4)
+ except:
+ tb = sys.exc_info()[2]
+
+ e = Exception()
+ e.__traceback__ = tb
+ e.__traceback__ = None
+ self.assertEqual(e.__traceback__, None)
+
+ def testChainingAttrs(self):
+ e = Exception()
+ self.assertEqual(e.__context__, None)
+ self.assertEqual(e.__cause__, None)
+
+ e = TypeError()
+ self.assertEqual(e.__context__, None)
+ self.assertEqual(e.__cause__, None)
+
+ class MyException(EnvironmentError):
+ pass
+
+ e = MyException()
+ self.assertEqual(e.__context__, None)
+ self.assertEqual(e.__cause__, None)
+
def testKeywordArgs(self):
# test that builtin exception don't take keyword args,
# but user-defined subclasses can if they want