Patch #1515343: Fix printing of deprecated string exceptions with a

value in the traceback module.
This commit is contained in:
Georg Brandl 2006-07-24 14:09:56 +00:00
parent 844f7ddcdc
commit c13c34c39d
3 changed files with 93 additions and 46 deletions

View file

@ -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,36 @@ 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.assert_(len(err) == 1)
self.assert_(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.assert_(len(err) == 1)
self.assert_(err[0] == str_type + ': ' + str_value + '\n')
def test_main():
run_unittest(TracebackCases)