diff options
Diffstat (limited to 'Lib/test/test_traceback.py')
-rw-r--r-- | Lib/test/test_traceback.py | 45 |
1 files changed, 43 insertions, 2 deletions
diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 1b59f98..b3c5a50 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -31,8 +31,9 @@ class TracebackCases(unittest.TestCase): err = self.get_exception_format(self.syntax_error_with_caret, SyntaxError) self.assert_(len(err) == 4) - self.assert_("^" in err[2]) # third line has caret self.assert_(err[1].strip() == "return x!") + self.assert_("^" in err[2]) # third line has caret + self.assert_(err[1].find("!") == err[2].find("^")) # in the right place def test_nocaret(self): if is_jython: @@ -47,8 +48,9 @@ class TracebackCases(unittest.TestCase): err = self.get_exception_format(self.syntax_error_bad_indentation, IndentationError) self.assert_(len(err) == 4) - self.assert_("^" in err[2]) self.assert_(err[1].strip() == "print 2") + self.assert_("^" in err[2]) + self.assert_(err[1].find("2") == err[2].find("^")) def test_bug737473(self): import sys, os, tempfile, time @@ -109,6 +111,45 @@ def test(): lst = traceback.format_exception_only(e.__class__, e) self.assertEqual(lst, ['KeyboardInterrupt\n']) + # String exceptions are deprecated, but legal. The quirky form with + # separate "type" and "value" tends to break things, because + # not isinstance(value, type) + # and a string cannot be the first argument to issubclass. + # + # Note that sys.last_type and sys.last_value do not get set if an + # exception is caught, so we sort of cheat and just emulate them. + # + # test_string_exception1 is equivalent to + # + # >>> raise "String Exception" + # + # test_string_exception2 is equivalent to + # + # >>> raise "String Exception", "String Value" + # + def test_string_exception1(self): + str_type = "String Exception" + err = traceback.format_exception_only(str_type, None) + self.assertEqual(len(err), 1) + self.assertEqual(err[0], str_type + '\n') + + def test_string_exception2(self): + str_type = "String Exception" + str_value = "String Value" + err = traceback.format_exception_only(str_type, str_value) + self.assertEqual(len(err), 1) + self.assertEqual(err[0], str_type + ': ' + str_value + '\n') + + def test_format_exception_only_bad__str__(self): + class X(Exception): + def __str__(self): + 1/0 + err = traceback.format_exception_only(X, X()) + self.assertEqual(len(err), 1) + str_value = '<unprintable %s object>' % X.__name__ + self.assertEqual(err[0], X.__name__ + ': ' + str_value + '\n') + + def test_main(): run_unittest(TracebackCases) |