Replaced my dumb way of calculating seconds to midnight with Tim Peters' much more...
[python.git] / Lib / test / test_traceback.py
blob22c04567a7232f2fa1901e14d0cabf150a901517
1 """Test cases for traceback module"""
3 import unittest
4 from test.test_support import run_unittest, is_jython
6 import traceback
8 class TracebackCases(unittest.TestCase):
9 # For now, a very minimal set of tests. I want to be sure that
10 # formatting of SyntaxErrors works based on changes for 2.1.
12 def get_exception_format(self, func, exc):
13 try:
14 func()
15 except exc, value:
16 return traceback.format_exception_only(exc, value)
17 else:
18 raise ValueError, "call did not raise exception"
20 def syntax_error_with_caret(self):
21 compile("def fact(x):\n\treturn x!\n", "?", "exec")
23 def syntax_error_without_caret(self):
24 # XXX why doesn't compile raise the same traceback?
25 import test.badsyntax_nocaret
27 def syntax_error_bad_indentation(self):
28 compile("def spam():\n print 1\n print 2", "?", "exec")
30 def test_caret(self):
31 err = self.get_exception_format(self.syntax_error_with_caret,
32 SyntaxError)
33 self.assert_(len(err) == 4)
34 self.assert_("^" in err[2]) # third line has caret
35 self.assert_(err[1].strip() == "return x!")
37 def test_nocaret(self):
38 if is_jython:
39 # jython adds a caret in this case (why shouldn't it?)
40 return
41 err = self.get_exception_format(self.syntax_error_without_caret,
42 SyntaxError)
43 self.assert_(len(err) == 3)
44 self.assert_(err[1].strip() == "[x for x in x] = x")
46 def test_bad_indentation(self):
47 err = self.get_exception_format(self.syntax_error_bad_indentation,
48 IndentationError)
49 self.assert_(len(err) == 4)
50 self.assert_("^" in err[2])
51 self.assert_(err[1].strip() == "print 2")
53 def test_bug737473(self):
54 import sys, os, tempfile, time
56 savedpath = sys.path[:]
57 testdir = tempfile.mkdtemp()
58 try:
59 sys.path.insert(0, testdir)
60 testfile = os.path.join(testdir, 'test_bug737473.py')
61 print >> open(testfile, 'w'), """
62 def test():
63 raise ValueError"""
65 if 'test_bug737473' in sys.modules:
66 del sys.modules['test_bug737473']
67 import test_bug737473
69 try:
70 test_bug737473.test()
71 except ValueError:
72 # this loads source code to linecache
73 traceback.extract_tb(sys.exc_traceback)
75 # If this test runs too quickly, test_bug737473.py's mtime
76 # attribute will remain unchanged even if the file is rewritten.
77 # Consequently, the file would not reload. So, added a sleep()
78 # delay to assure that a new, distinct timestamp is written.
79 # Since WinME with FAT32 has multisecond resolution, more than
80 # three seconds are needed for this test to pass reliably :-(
81 time.sleep(4)
83 print >> open(testfile, 'w'), """
84 def test():
85 raise NotImplementedError"""
86 reload(test_bug737473)
87 try:
88 test_bug737473.test()
89 except NotImplementedError:
90 src = traceback.extract_tb(sys.exc_traceback)[-1][-1]
91 self.failUnlessEqual(src, 'raise NotImplementedError')
92 finally:
93 sys.path[:] = savedpath
94 for f in os.listdir(testdir):
95 os.unlink(os.path.join(testdir, f))
96 os.rmdir(testdir)
98 def test_members(self):
99 # Covers Python/structmember.c::listmembers()
100 try:
102 except:
103 import sys
104 sys.exc_traceback.__members__
106 def test_main():
107 run_unittest(TracebackCases)
110 if __name__ == "__main__":
111 test_main()