@@ -537,6 +537,45 @@ def test_many_args_with_overridden___str__(self):
537537 self .assertRaises (UnicodeEncodeError , str , e )
538538 self .assertEqual (unicode (e ), u'f\xf6 \xf6 ' )
539539
540+ def test_exception_with_doc (self ):
541+ import _testcapi
542+ doc2 = "This is a test docstring."
543+ doc4 = "This is another test docstring."
544+
545+ self .assertRaises (SystemError , _testcapi .make_exception_with_doc ,
546+ "error1" )
547+
548+ # test basic usage of PyErr_NewException
549+ error1 = _testcapi .make_exception_with_doc ("_testcapi.error1" )
550+ self .assertIs (type (error1 ), type )
551+ self .assertTrue (issubclass (error1 , Exception ))
552+ self .assertIsNone (error1 .__doc__ )
553+
554+ # test with given docstring
555+ error2 = _testcapi .make_exception_with_doc ("_testcapi.error2" , doc2 )
556+ self .assertEqual (error2 .__doc__ , doc2 )
557+
558+ # test with explicit base (without docstring)
559+ error3 = _testcapi .make_exception_with_doc ("_testcapi.error3" ,
560+ base = error2 )
561+ self .assertTrue (issubclass (error3 , error2 ))
562+
563+ # test with explicit base tuple
564+ class C (object ):
565+ pass
566+ error4 = _testcapi .make_exception_with_doc ("_testcapi.error4" , doc4 ,
567+ (error3 , C ))
568+ self .assertTrue (issubclass (error4 , error3 ))
569+ self .assertTrue (issubclass (error4 , C ))
570+ self .assertEqual (error4 .__doc__ , doc4 )
571+
572+ # test with explicit dictionary
573+ error5 = _testcapi .make_exception_with_doc ("_testcapi.error5" , "" ,
574+ error4 , {'a' : 1 })
575+ self .assertTrue (issubclass (error5 , error4 ))
576+ self .assertEqual (error5 .a , 1 )
577+ self .assertEqual (error5 .__doc__ , "" )
578+
540579
541580def test_main ():
542581 run_unittest (ExceptionTests , TestSameStrAndUnicodeMsg )
0 commit comments