summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2009-12-28 08:34:58 (GMT)
committerGeorg Brandl <georg@python.org>2009-12-28 08:34:58 (GMT)
commit740cdc3a9f9593ee1fe86d751c70b8505d6a19c9 (patch)
tree4c34f7bec409dfb040cba93c2064b37bb71b2b95 /Lib
parent02e7dfde639498064b137be7cb850c4229f0c8fb (diff)
downloadcpython-740cdc3a9f9593ee1fe86d751c70b8505d6a19c9.zip
cpython-740cdc3a9f9593ee1fe86d751c70b8505d6a19c9.tar.gz
cpython-740cdc3a9f9593ee1fe86d751c70b8505d6a19c9.tar.bz2
#7033: add new API function PyErr_NewExceptionWithDoc, for easily giving new exceptions a docstring.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_exceptions.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
index d5ce80b..a30f42b 100644
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -537,6 +537,45 @@ class TestSameStrAndUnicodeMsg(unittest.TestCase):
self.assertRaises(UnicodeEncodeError, str, e)
self.assertEqual(unicode(e), u'f\xf6\xf6')
+ def test_exception_with_doc(self):
+ import _testcapi
+ doc2 = "This is a test docstring."
+ doc4 = "This is another test docstring."
+
+ self.assertRaises(SystemError, _testcapi.make_exception_with_doc,
+ "error1")
+
+ # test basic usage of PyErr_NewException
+ error1 = _testcapi.make_exception_with_doc("_testcapi.error1")
+ self.assertIs(type(error1), type)
+ self.assertTrue(issubclass(error1, Exception))
+ self.assertIsNone(error1.__doc__)
+
+ # test with given docstring
+ error2 = _testcapi.make_exception_with_doc("_testcapi.error2", doc2)
+ self.assertEqual(error2.__doc__, doc2)
+
+ # test with explicit base (without docstring)
+ error3 = _testcapi.make_exception_with_doc("_testcapi.error3",
+ base=error2)
+ self.assertTrue(issubclass(error3, error2))
+
+ # test with explicit base tuple
+ class C(object):
+ pass
+ error4 = _testcapi.make_exception_with_doc("_testcapi.error4", doc4,
+ (error3, C))
+ self.assertTrue(issubclass(error4, error3))
+ self.assertTrue(issubclass(error4, C))
+ self.assertEqual(error4.__doc__, doc4)
+
+ # test with explicit dictionary
+ error5 = _testcapi.make_exception_with_doc("_testcapi.error5", "",
+ error4, {'a': 1})
+ self.assertTrue(issubclass(error5, error4))
+ self.assertEqual(error5.a, 1)
+ self.assertEqual(error5.__doc__, "")
+
def test_main():
run_unittest(ExceptionTests, TestSameStrAndUnicodeMsg)