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
27 def syntax_error_bad_indentation(self
):
28 compile("def spam():\n print 1\n print 2", "?", "exec")
31 err
= self
.get_exception_format(self
.syntax_error_with_caret
,
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
):
39 # jython adds a caret in this case (why shouldn't it?)
41 err
= self
.get_exception_format(self
.syntax_error_without_caret
,
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
,
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()
59 sys
.path
.insert(0, testdir
)
60 testfile
= os
.path
.join(testdir
, 'test_bug737473.py')
61 print >> open(testfile
, 'w'), """
65 if 'test_bug737473' in sys
.modules
:
66 del sys
.modules
['test_bug737473']
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 :-(
83 print >> open(testfile
, 'w'), """
85 raise NotImplementedError"""
86 reload(test_bug737473
)
89 except NotImplementedError:
90 src
= traceback
.extract_tb(sys
.exc_traceback
)[-1][-1]
91 self
.failUnlessEqual(src
, 'raise NotImplementedError')
93 sys
.path
[:] = savedpath
94 for f
in os
.listdir(testdir
):
95 os
.unlink(os
.path
.join(testdir
, f
))
98 def test_members(self
):
99 # Covers Python/structmember.c::listmembers()
104 sys
.exc_traceback
.__members
__
107 run_unittest(TracebackCases
)
110 if __name__
== "__main__":