Require implementations for warnings.showwarning() support the 'line' argument.
[python.git] / Lib / test / test_traceback.py
blob8034839499a3114177aaac100270d9815fafba0e
1 """Test cases for traceback module"""
3 from _testcapi import traceback_print
4 from StringIO import StringIO
5 import sys
6 import unittest
7 from test.test_support import run_unittest, is_jython, Error
9 import traceback
11 try:
12 raise KeyError
13 except KeyError:
14 type_, value, tb = sys.exc_info()
15 file_ = StringIO()
16 traceback_print(tb, file_)
17 example_traceback = file_.getvalue()
18 else:
19 raise Error("unable to create test traceback string")
22 class TracebackCases(unittest.TestCase):
23 # For now, a very minimal set of tests. I want to be sure that
24 # formatting of SyntaxErrors works based on changes for 2.1.
26 def get_exception_format(self, func, exc):
27 try:
28 func()
29 except exc, value:
30 return traceback.format_exception_only(exc, value)
31 else:
32 raise ValueError, "call did not raise exception"
34 def syntax_error_with_caret(self):
35 compile("def fact(x):\n\treturn x!\n", "?", "exec")
37 def syntax_error_without_caret(self):
38 # XXX why doesn't compile raise the same traceback?
39 import test.badsyntax_nocaret
41 def syntax_error_bad_indentation(self):
42 compile("def spam():\n print 1\n print 2", "?", "exec")
44 def test_caret(self):
45 err = self.get_exception_format(self.syntax_error_with_caret,
46 SyntaxError)
47 self.assert_(len(err) == 4)
48 self.assert_(err[1].strip() == "return x!")
49 self.assert_("^" in err[2]) # third line has caret
50 self.assert_(err[1].find("!") == err[2].find("^")) # in the right place
52 def test_nocaret(self):
53 if is_jython:
54 # jython adds a caret in this case (why shouldn't it?)
55 return
56 err = self.get_exception_format(self.syntax_error_without_caret,
57 SyntaxError)
58 self.assert_(len(err) == 3)
59 self.assert_(err[1].strip() == "[x for x in x] = x")
61 def test_bad_indentation(self):
62 err = self.get_exception_format(self.syntax_error_bad_indentation,
63 IndentationError)
64 self.assert_(len(err) == 4)
65 self.assert_(err[1].strip() == "print 2")
66 self.assert_("^" in err[2])
67 self.assert_(err[1].find("2") == err[2].find("^"))
69 def test_bug737473(self):
70 import sys, os, tempfile, time
72 savedpath = sys.path[:]
73 testdir = tempfile.mkdtemp()
74 try:
75 sys.path.insert(0, testdir)
76 testfile = os.path.join(testdir, 'test_bug737473.py')
77 print >> open(testfile, 'w'), """
78 def test():
79 raise ValueError"""
81 if 'test_bug737473' in sys.modules:
82 del sys.modules['test_bug737473']
83 import test_bug737473
85 try:
86 test_bug737473.test()
87 except ValueError:
88 # this loads source code to linecache
89 traceback.extract_tb(sys.exc_traceback)
91 # If this test runs too quickly, test_bug737473.py's mtime
92 # attribute will remain unchanged even if the file is rewritten.
93 # Consequently, the file would not reload. So, added a sleep()
94 # delay to assure that a new, distinct timestamp is written.
95 # Since WinME with FAT32 has multisecond resolution, more than
96 # three seconds are needed for this test to pass reliably :-(
97 time.sleep(4)
99 print >> open(testfile, 'w'), """
100 def test():
101 raise NotImplementedError"""
102 reload(test_bug737473)
103 try:
104 test_bug737473.test()
105 except NotImplementedError:
106 src = traceback.extract_tb(sys.exc_traceback)[-1][-1]
107 self.failUnlessEqual(src, 'raise NotImplementedError')
108 finally:
109 sys.path[:] = savedpath
110 for f in os.listdir(testdir):
111 os.unlink(os.path.join(testdir, f))
112 os.rmdir(testdir)
114 def test_members(self):
115 # Covers Python/structmember.c::listmembers()
116 try:
118 except:
119 import sys
120 sys.exc_traceback.__members__
122 def test_base_exception(self):
123 # Test that exceptions derived from BaseException are formatted right
124 e = KeyboardInterrupt()
125 lst = traceback.format_exception_only(e.__class__, e)
126 self.assertEqual(lst, ['KeyboardInterrupt\n'])
128 # String exceptions are deprecated, but legal. The quirky form with
129 # separate "type" and "value" tends to break things, because
130 # not isinstance(value, type)
131 # and a string cannot be the first argument to issubclass.
133 # Note that sys.last_type and sys.last_value do not get set if an
134 # exception is caught, so we sort of cheat and just emulate them.
136 # test_string_exception1 is equivalent to
138 # >>> raise "String Exception"
140 # test_string_exception2 is equivalent to
142 # >>> raise "String Exception", "String Value"
144 def test_string_exception1(self):
145 str_type = "String Exception"
146 err = traceback.format_exception_only(str_type, None)
147 self.assertEqual(len(err), 1)
148 self.assertEqual(err[0], str_type + '\n')
150 def test_string_exception2(self):
151 str_type = "String Exception"
152 str_value = "String Value"
153 err = traceback.format_exception_only(str_type, str_value)
154 self.assertEqual(len(err), 1)
155 self.assertEqual(err[0], str_type + ': ' + str_value + '\n')
157 def test_format_exception_only_bad__str__(self):
158 class X(Exception):
159 def __str__(self):
161 err = traceback.format_exception_only(X, X())
162 self.assertEqual(len(err), 1)
163 str_value = '<unprintable %s object>' % X.__name__
164 self.assertEqual(err[0], X.__name__ + ': ' + str_value + '\n')
166 def test_without_exception(self):
167 err = traceback.format_exception_only(None, None)
168 self.assertEqual(err, ['None\n'])
171 class TracebackFormatTests(unittest.TestCase):
173 def test_traceback_indentation(self):
174 # Make sure that the traceback is properly indented.
175 tb_lines = example_traceback.splitlines()
176 self.assertEquals(len(tb_lines), 3)
177 banner, location, source_line = tb_lines
178 self.assert_(banner.startswith('Traceback'))
179 self.assert_(location.startswith(' File'))
180 self.assert_(source_line.startswith(' raise'))
183 def test_main():
184 run_unittest(TracebackCases, TracebackFormatTests)
187 if __name__ == "__main__":
188 test_main()