1
"""Test cases for traceback module"""
4
from test.test_support import run_unittest, is_jython
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):
16
return traceback.format_exception_only(exc, value)
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
28
err = self.get_exception_format(self.syntax_error_with_caret,
30
self.assert_(len(err) == 4)
31
self.assert_("^" in err[2]) # third line has caret
32
self.assert_(err[1].strip() == "return x!")
34
def test_bug737473(self):
35
import sys, os, tempfile, time
37
savedpath = sys.path[:]
38
testdir = tempfile.mkdtemp()
40
sys.path.insert(0, testdir)
41
testfile = os.path.join(testdir, 'test_bug737473.py')
42
f = open(testfile, 'w')
48
if 'test_bug737473' in sys.modules:
49
del sys.modules['test_bug737473']
55
# this loads source code to linecache
56
traceback.extract_tb(sys.exc_traceback)
58
# If this test runs too quickly, test_bug737473.py's mtime
59
# attribute will remain unchanged even if the file is rewritten.
60
# Consequently, the file would not reload. So, added a sleep()
61
# delay to assure that a new, distinct timestamp is written.
62
# Since WinME with FAT32 has multisecond resolution, more than
63
# three seconds are needed for this test to pass reliably :-(
66
f = open(testfile, 'w')
69
raise NotImplementedError"""
71
reload(test_bug737473)
74
except NotImplementedError:
75
src = traceback.extract_tb(sys.exc_traceback)[-1][-1]
76
self.failUnlessEqual(src, 'raise NotImplementedError')
78
sys.path[:] = savedpath
79
for f in os.listdir(testdir):
80
os.unlink(os.path.join(testdir, f))
84
run_unittest(TracebackCases)
87
if __name__ == "__main__":