diff options
Diffstat (limited to 'Lib/test/test_exceptions.py')
-rw-r--r-- | Lib/test/test_exceptions.py | 102 |
1 files changed, 72 insertions, 30 deletions
diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py index dcdd885deb..a19c82d066 100644 --- a/Lib/test/test_exceptions.py +++ b/Lib/test/test_exceptions.py @@ -6,7 +6,7 @@ import unittest import pickle, cPickle from test.test_support import (TESTFN, unlink, run_unittest, captured_output, - check_warnings) + check_warnings, cpython_only) from test.test_pep352 import ignore_deprecation_warnings # XXX This is not really enough, each *operation* should be tested! @@ -32,8 +32,8 @@ class ExceptionTests(unittest.TestCase): raise exc("spam") except exc, err: buf2 = str(err) - self.assertEquals(buf1, buf2) - self.assertEquals(exc.__name__, excname) + self.assertEqual(buf1, buf2) + self.assertEqual(exc.__name__, excname) def testRaising(self): self.raise_catch(AttributeError, "AttributeError") @@ -147,6 +147,7 @@ class ExceptionTests(unittest.TestCase): ckmsg(s, "'continue' not properly in loop") ckmsg("continue\n", "'continue' not properly in loop") + @cpython_only def testSettingException(self): # test that setting an exception at the C level works even if the # exception object can't be constructed. @@ -162,8 +163,8 @@ class ExceptionTests(unittest.TestCase): except TypeError, err: exc, err, tb = sys.exc_info() co = tb.tb_frame.f_code - self.assertEquals(co.co_name, "test_capi1") - self.assert_(co.co_filename.endswith('test_exceptions'+os.extsep+'py')) + self.assertEqual(co.co_name, "test_capi1") + self.assertTrue(co.co_filename.endswith('test_exceptions'+os.extsep+'py')) else: self.fail("Expected exception") @@ -174,10 +175,10 @@ class ExceptionTests(unittest.TestCase): except RuntimeError, err: exc, err, tb = sys.exc_info() co = tb.tb_frame.f_code - self.assertEquals(co.co_name, "__init__") - self.assert_(co.co_filename.endswith('test_exceptions'+os.extsep+'py')) + self.assertEqual(co.co_name, "__init__") + self.assertTrue(co.co_filename.endswith('test_exceptions'+os.extsep+'py')) co2 = tb.tb_frame.f_back.f_code - self.assertEquals(co2.co_name, "test_capi2") + self.assertEqual(co2.co_name, "test_capi2") else: self.fail("Expected exception") @@ -191,12 +192,12 @@ class ExceptionTests(unittest.TestCase): except NameError: pass else: - self.failUnlessEqual(str(WindowsError(1001)), + self.assertEqual(str(WindowsError(1001)), "1001") - self.failUnlessEqual(str(WindowsError(1001, "message")), + self.assertEqual(str(WindowsError(1001, "message")), "[Error 1001] message") - self.failUnlessEqual(WindowsError(1001, "message").errno, 22) - self.failUnlessEqual(WindowsError(1001, "message").winerror, 1001) + self.assertEqual(WindowsError(1001, "message").errno, 22) + self.assertEqual(WindowsError(1001, "message").winerror, 1001) @ignore_deprecation_warnings def testAttributes(self): @@ -283,14 +284,14 @@ class ExceptionTests(unittest.TestCase): if type(e) is not exc: raise # Verify module name - self.assertEquals(type(e).__module__, 'exceptions') + self.assertEqual(type(e).__module__, 'exceptions') # Verify no ref leaks in Exc_str() s = str(e) for checkArgName in expected: - self.assertEquals(repr(getattr(e, checkArgName)), - repr(expected[checkArgName]), - 'exception "%s", attribute "%s"' % - (repr(e), checkArgName)) + self.assertEqual(repr(getattr(e, checkArgName)), + repr(expected[checkArgName]), + 'exception "%s", attribute "%s"' % + (repr(e), checkArgName)) # test for pickling support for p in pickle, cPickle: @@ -299,9 +300,9 @@ class ExceptionTests(unittest.TestCase): for checkArgName in expected: got = repr(getattr(new, checkArgName)) want = repr(expected[checkArgName]) - self.assertEquals(got, want, - 'pickled "%r", attribute "%s"' % - (e, checkArgName)) + self.assertEqual(got, want, + 'pickled "%r", attribute "%s"' % + (e, checkArgName)) def testDeprecatedMessageAttribute(self): @@ -323,7 +324,8 @@ class ExceptionTests(unittest.TestCase): self.assertEqual(len(w.warnings), 0) # Deleting the message is supported, too. del exc.message - self.assertRaises(AttributeError, getattr, exc, "message") + with self.assertRaises(AttributeError): + exc.message @ignore_deprecation_warnings def testPickleMessageAttribute(self): @@ -343,7 +345,7 @@ class ExceptionTests(unittest.TestCase): # going through the 'args' attribute. args = (1, 2, 3) exc = BaseException(*args) - self.failUnlessEqual(exc[:], args) + self.assertEqual(exc[:], args) self.assertEqual(exc.args[:], args) def testKeywordArgs(self): @@ -357,7 +359,7 @@ class ExceptionTests(unittest.TestCase): self.fancy_arg = fancy_arg x = DerivedException(fancy_arg=42) - self.assertEquals(x.fancy_arg, 42) + self.assertEqual(x.fancy_arg, 42) def testInfiniteRecursion(self): def f(): @@ -386,11 +388,11 @@ class ExceptionTests(unittest.TestCase): def testUnicodeStrUsage(self): # Make sure both instances and classes have a str and unicode # representation. - self.failUnless(str(Exception)) - self.failUnless(unicode(Exception)) - self.failUnless(str(Exception('a'))) - self.failUnless(unicode(Exception(u'a'))) - self.failUnless(unicode(Exception(u'\xe1'))) + self.assertTrue(str(Exception)) + self.assertTrue(unicode(Exception)) + self.assertTrue(str(Exception('a'))) + self.assertTrue(unicode(Exception(u'a'))) + self.assertTrue(unicode(Exception(u'\xe1'))) def testUnicodeChangeAttributes(self): # See issue 7309. This was a crasher. @@ -459,8 +461,8 @@ class ExceptionTests(unittest.TestCase): except RuntimeError: return sys.exc_info() e, v, tb = g() - self.assert_(e is RuntimeError, e) - self.assert_("maximum recursion depth exceeded" in str(v), v) + self.assertTrue(e is RuntimeError, e) + self.assertIn("maximum recursion depth exceeded", str(v)) @@ -570,6 +572,46 @@ class TestSameStrAndUnicodeMsg(unittest.TestCase): self.assertRaises(UnicodeEncodeError, str, e) self.assertEqual(unicode(e), u'f\xf6\xf6') + @cpython_only + 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) |