annotate Lib/test/test_traceback.py @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents 97d7cd365dd5
children 4380e04343ee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17117
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1 """Test cases for traceback module"""
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2
46592
4e47548edd36 Rename the test_traceback_print() function to traceback_print() to prevent
Brett Cannon <bcannon@gmail.com>
parents: 46589
diff changeset
3 from _testcapi import traceback_print
46589
07a4e1710262 Fix a bug introduced by the warnings rewrite where tracebacks were being
Brett Cannon <bcannon@gmail.com>
parents: 39883
diff changeset
4 from StringIO import StringIO
07a4e1710262 Fix a bug introduced by the warnings rewrite where tracebacks were being
Brett Cannon <bcannon@gmail.com>
parents: 39883
diff changeset
5 import sys
17117
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
6 import unittest
46589
07a4e1710262 Fix a bug introduced by the warnings rewrite where tracebacks were being
Brett Cannon <bcannon@gmail.com>
parents: 39883
diff changeset
7 from test.test_support import run_unittest, is_jython, Error
17117
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
8
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
9 import traceback
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
10
52953
75def7ed9d5f Rolled back revisions 71237 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52903
diff changeset
11 try:
75def7ed9d5f Rolled back revisions 71237 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52903
diff changeset
12 raise KeyError
75def7ed9d5f Rolled back revisions 71237 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52903
diff changeset
13 except KeyError:
75def7ed9d5f Rolled back revisions 71237 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52903
diff changeset
14 type_, value, tb = sys.exc_info()
75def7ed9d5f Rolled back revisions 71237 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52903
diff changeset
15 file_ = StringIO()
75def7ed9d5f Rolled back revisions 71237 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52903
diff changeset
16 traceback_print(tb, file_)
75def7ed9d5f Rolled back revisions 71237 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52903
diff changeset
17 example_traceback = file_.getvalue()
75def7ed9d5f Rolled back revisions 71237 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52903
diff changeset
18 else:
75def7ed9d5f Rolled back revisions 71237 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52903
diff changeset
19 raise Error("unable to create test traceback string")
75def7ed9d5f Rolled back revisions 71237 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52903
diff changeset
20
46589
07a4e1710262 Fix a bug introduced by the warnings rewrite where tracebacks were being
Brett Cannon <bcannon@gmail.com>
parents: 39883
diff changeset
21
17117
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
22 class TracebackCases(unittest.TestCase):
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
23 # For now, a very minimal set of tests. I want to be sure that
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
24 # formatting of SyntaxErrors works based on changes for 2.1.
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
25
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
26 def get_exception_format(self, func, exc):
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
27 try:
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
28 func()
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
29 except exc, value:
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
30 return traceback.format_exception_only(exc, value)
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
31 else:
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
32 raise ValueError, "call did not raise exception"
17341
83ee63bcf036 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17117
diff changeset
33
17117
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
34 def syntax_error_with_caret(self):
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
35 compile("def fact(x):\n\treturn x!\n", "?", "exec")
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
36
56830
97d7cd365dd5 Merged revisions 73206,73232,73299,73683,74020,74185,74544,74643,74647,74817,74838-74839,74865,74946,75402,75459,75604,75696 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52953
diff changeset
37 def syntax_error_with_caret_2(self):
97d7cd365dd5 Merged revisions 73206,73232,73299,73683,74020,74185,74544,74643,74647,74817,74838-74839,74865,74946,75402,75459,75604,75696 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52953
diff changeset
38 compile("1 +\n", "?", "exec")
97d7cd365dd5 Merged revisions 73206,73232,73299,73683,74020,74185,74544,74643,74647,74817,74838-74839,74865,74946,75402,75459,75604,75696 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52953
diff changeset
39
17117
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
40 def syntax_error_without_caret(self):
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
41 # XXX why doesn't compile raise the same traceback?
24442
226ba34bd998 Complete the absolute import patch for the test suite. All relative
Barry Warsaw <barry@python.org>
parents: 24364
diff changeset
42 import test.badsyntax_nocaret
37407
2834ccf552f3 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37334
diff changeset
43
37334
a4950ea556b5 traceback now shows error position for all SyntaxError subclasses,
Georg Brandl <georg@python.org>
parents: 35890
diff changeset
44 def syntax_error_bad_indentation(self):
a4950ea556b5 traceback now shows error position for all SyntaxError subclasses,
Georg Brandl <georg@python.org>
parents: 35890
diff changeset
45 compile("def spam():\n print 1\n print 2", "?", "exec")
17117
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
46
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
47 def test_caret(self):
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
48 err = self.get_exception_format(self.syntax_error_with_caret,
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
49 SyntaxError)
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
50 self.assert_(len(err) == 4)
39163
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
51 self.assert_(err[1].strip() == "return x!")
17117
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
52 self.assert_("^" in err[2]) # third line has caret
39163
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
53 self.assert_(err[1].find("!") == err[2].find("^")) # in the right place
17341
83ee63bcf036 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17117
diff changeset
54
56830
97d7cd365dd5 Merged revisions 73206,73232,73299,73683,74020,74185,74544,74643,74647,74817,74838-74839,74865,74946,75402,75459,75604,75696 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52953
diff changeset
55 err = self.get_exception_format(self.syntax_error_with_caret_2,
97d7cd365dd5 Merged revisions 73206,73232,73299,73683,74020,74185,74544,74643,74647,74817,74838-74839,74865,74946,75402,75459,75604,75696 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52953
diff changeset
56 SyntaxError)
97d7cd365dd5 Merged revisions 73206,73232,73299,73683,74020,74185,74544,74643,74647,74817,74838-74839,74865,74946,75402,75459,75604,75696 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52953
diff changeset
57 self.assert_("^" in err[2]) # third line has caret
97d7cd365dd5 Merged revisions 73206,73232,73299,73683,74020,74185,74544,74643,74647,74817,74838-74839,74865,74946,75402,75459,75604,75696 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52953
diff changeset
58 self.assert_(err[2].count('\n') == 1) # and no additional newline
97d7cd365dd5 Merged revisions 73206,73232,73299,73683,74020,74185,74544,74643,74647,74817,74838-74839,74865,74946,75402,75459,75604,75696 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52953
diff changeset
59 self.assert_(err[1].find("+") == err[2].find("^")) # in the right place
97d7cd365dd5 Merged revisions 73206,73232,73299,73683,74020,74185,74544,74643,74647,74817,74838-74839,74865,74946,75402,75459,75604,75696 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52953
diff changeset
60
17117
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
61 def test_nocaret(self):
26071
b063c43f4df0 Skip the test_nocaret test when running as jython. Jython happens to add
Finn Bock <bckfnn@worldonline.dk>
parents: 24442
diff changeset
62 if is_jython:
b063c43f4df0 Skip the test_nocaret test when running as jython. Jython happens to add
Finn Bock <bckfnn@worldonline.dk>
parents: 24442
diff changeset
63 # jython adds a caret in this case (why shouldn't it?)
b063c43f4df0 Skip the test_nocaret test when running as jython. Jython happens to add
Finn Bock <bckfnn@worldonline.dk>
parents: 24442
diff changeset
64 return
17117
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
65 err = self.get_exception_format(self.syntax_error_without_caret,
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
66 SyntaxError)
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
67 self.assert_(len(err) == 3)
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
68 self.assert_(err[1].strip() == "[x for x in x] = x")
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
69
37334
a4950ea556b5 traceback now shows error position for all SyntaxError subclasses,
Georg Brandl <georg@python.org>
parents: 35890
diff changeset
70 def test_bad_indentation(self):
a4950ea556b5 traceback now shows error position for all SyntaxError subclasses,
Georg Brandl <georg@python.org>
parents: 35890
diff changeset
71 err = self.get_exception_format(self.syntax_error_bad_indentation,
a4950ea556b5 traceback now shows error position for all SyntaxError subclasses,
Georg Brandl <georg@python.org>
parents: 35890
diff changeset
72 IndentationError)
a4950ea556b5 traceback now shows error position for all SyntaxError subclasses,
Georg Brandl <georg@python.org>
parents: 35890
diff changeset
73 self.assert_(len(err) == 4)
39163
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
74 self.assert_(err[1].strip() == "print 2")
37334
a4950ea556b5 traceback now shows error position for all SyntaxError subclasses,
Georg Brandl <georg@python.org>
parents: 35890
diff changeset
75 self.assert_("^" in err[2])
39163
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
76 self.assert_(err[1].find("2") == err[2].find("^"))
37334
a4950ea556b5 traceback now shows error position for all SyntaxError subclasses,
Georg Brandl <georg@python.org>
parents: 35890
diff changeset
77
33839
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
78 def test_bug737473(self):
33847
5510995ea26f Paper over bug 1054615 by passing sane values to os.utime().
Tim Peters <tim.peters@gmail.com>
parents: 33839
diff changeset
79 import sys, os, tempfile, time
5510995ea26f Paper over bug 1054615 by passing sane values to os.utime().
Tim Peters <tim.peters@gmail.com>
parents: 33839
diff changeset
80
33839
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
81 savedpath = sys.path[:]
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
82 testdir = tempfile.mkdtemp()
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
83 try:
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
84 sys.path.insert(0, testdir)
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
85 testfile = os.path.join(testdir, 'test_bug737473.py')
33878
ef5030bc5f67 * Bumped up the sleep() delay to four seconds so this test will run
Raymond Hettinger <python@rcn.com>
parents: 33874
diff changeset
86 print >> open(testfile, 'w'), """
33839
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
87 def test():
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
88 raise ValueError"""
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
89
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
90 if 'test_bug737473' in sys.modules:
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
91 del sys.modules['test_bug737473']
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
92 import test_bug737473
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
93
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
94 try:
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
95 test_bug737473.test()
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
96 except ValueError:
33848
3398c5ce8b7a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33847
diff changeset
97 # this loads source code to linecache
33839
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
98 traceback.extract_tb(sys.exc_traceback)
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
99
33878
ef5030bc5f67 * Bumped up the sleep() delay to four seconds so this test will run
Raymond Hettinger <python@rcn.com>
parents: 33874
diff changeset
100 # If this test runs too quickly, test_bug737473.py's mtime
ef5030bc5f67 * Bumped up the sleep() delay to four seconds so this test will run
Raymond Hettinger <python@rcn.com>
parents: 33874
diff changeset
101 # attribute will remain unchanged even if the file is rewritten.
ef5030bc5f67 * Bumped up the sleep() delay to four seconds so this test will run
Raymond Hettinger <python@rcn.com>
parents: 33874
diff changeset
102 # Consequently, the file would not reload. So, added a sleep()
ef5030bc5f67 * Bumped up the sleep() delay to four seconds so this test will run
Raymond Hettinger <python@rcn.com>
parents: 33874
diff changeset
103 # delay to assure that a new, distinct timestamp is written.
ef5030bc5f67 * Bumped up the sleep() delay to four seconds so this test will run
Raymond Hettinger <python@rcn.com>
parents: 33874
diff changeset
104 # Since WinME with FAT32 has multisecond resolution, more than
ef5030bc5f67 * Bumped up the sleep() delay to four seconds so this test will run
Raymond Hettinger <python@rcn.com>
parents: 33874
diff changeset
105 # three seconds are needed for this test to pass reliably :-(
ef5030bc5f67 * Bumped up the sleep() delay to four seconds so this test will run
Raymond Hettinger <python@rcn.com>
parents: 33874
diff changeset
106 time.sleep(4)
33874
0dc3f9705577 Bug #1057993: Use sleep() always instead of os.utime() possibly broken
Hye-Shik Chang <hyeshik@gmail.com>
parents: 33850
diff changeset
107
33878
ef5030bc5f67 * Bumped up the sleep() delay to four seconds so this test will run
Raymond Hettinger <python@rcn.com>
parents: 33874
diff changeset
108 print >> open(testfile, 'w'), """
33839
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
109 def test():
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
110 raise NotImplementedError"""
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
111 reload(test_bug737473)
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
112 try:
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
113 test_bug737473.test()
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
114 except NotImplementedError:
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
115 src = traceback.extract_tb(sys.exc_traceback)[-1][-1]
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
116 self.failUnlessEqual(src, 'raise NotImplementedError')
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
117 finally:
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
118 sys.path[:] = savedpath
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
119 for f in os.listdir(testdir):
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
120 os.unlink(os.path.join(testdir, f))
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
121 os.rmdir(testdir)
20013
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 17558
diff changeset
122
38083
bb3e0c889632 Fix the formatting of KeyboardInterrupt -- a bad issubclass() call.
Guido van Rossum <guido@python.org>
parents: 37652
diff changeset
123 def test_base_exception(self):
bb3e0c889632 Fix the formatting of KeyboardInterrupt -- a bad issubclass() call.
Guido van Rossum <guido@python.org>
parents: 37652
diff changeset
124 # Test that exceptions derived from BaseException are formatted right
bb3e0c889632 Fix the formatting of KeyboardInterrupt -- a bad issubclass() call.
Guido van Rossum <guido@python.org>
parents: 37652
diff changeset
125 e = KeyboardInterrupt()
bb3e0c889632 Fix the formatting of KeyboardInterrupt -- a bad issubclass() call.
Guido van Rossum <guido@python.org>
parents: 37652
diff changeset
126 lst = traceback.format_exception_only(e.__class__, e)
bb3e0c889632 Fix the formatting of KeyboardInterrupt -- a bad issubclass() call.
Guido van Rossum <guido@python.org>
parents: 37652
diff changeset
127 self.assertEqual(lst, ['KeyboardInterrupt\n'])
bb3e0c889632 Fix the formatting of KeyboardInterrupt -- a bad issubclass() call.
Guido van Rossum <guido@python.org>
parents: 37652
diff changeset
128
39163
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
129 # String exceptions are deprecated, but legal. The quirky form with
39167
07cee01a4fcc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 39163
diff changeset
130 # separate "type" and "value" tends to break things, because
39163
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
131 # not isinstance(value, type)
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
132 # and a string cannot be the first argument to issubclass.
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
133 #
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
134 # Note that sys.last_type and sys.last_value do not get set if an
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
135 # exception is caught, so we sort of cheat and just emulate them.
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
136 #
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
137 # test_string_exception1 is equivalent to
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
138 #
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
139 # >>> raise "String Exception"
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
140 #
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
141 # test_string_exception2 is equivalent to
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
142 #
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
143 # >>> raise "String Exception", "String Value"
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
144 #
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
145 def test_string_exception1(self):
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
146 str_type = "String Exception"
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
147 err = traceback.format_exception_only(str_type, None)
39362
ec4f35d2a693 Bug #1531405, format_exception no longer raises an exception if
Neal Norwitz <nnorwitz@gmail.com>
parents: 39167
diff changeset
148 self.assertEqual(len(err), 1)
ec4f35d2a693 Bug #1531405, format_exception no longer raises an exception if
Neal Norwitz <nnorwitz@gmail.com>
parents: 39167
diff changeset
149 self.assertEqual(err[0], str_type + '\n')
39163
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
150
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
151 def test_string_exception2(self):
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
152 str_type = "String Exception"
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
153 str_value = "String Value"
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
154 err = traceback.format_exception_only(str_type, str_value)
39362
ec4f35d2a693 Bug #1531405, format_exception no longer raises an exception if
Neal Norwitz <nnorwitz@gmail.com>
parents: 39167
diff changeset
155 self.assertEqual(len(err), 1)
ec4f35d2a693 Bug #1531405, format_exception no longer raises an exception if
Neal Norwitz <nnorwitz@gmail.com>
parents: 39167
diff changeset
156 self.assertEqual(err[0], str_type + ': ' + str_value + '\n')
ec4f35d2a693 Bug #1531405, format_exception no longer raises an exception if
Neal Norwitz <nnorwitz@gmail.com>
parents: 39167
diff changeset
157
ec4f35d2a693 Bug #1531405, format_exception no longer raises an exception if
Neal Norwitz <nnorwitz@gmail.com>
parents: 39167
diff changeset
158 def test_format_exception_only_bad__str__(self):
ec4f35d2a693 Bug #1531405, format_exception no longer raises an exception if
Neal Norwitz <nnorwitz@gmail.com>
parents: 39167
diff changeset
159 class X(Exception):
ec4f35d2a693 Bug #1531405, format_exception no longer raises an exception if
Neal Norwitz <nnorwitz@gmail.com>
parents: 39167
diff changeset
160 def __str__(self):
ec4f35d2a693 Bug #1531405, format_exception no longer raises an exception if
Neal Norwitz <nnorwitz@gmail.com>
parents: 39167
diff changeset
161 1/0
ec4f35d2a693 Bug #1531405, format_exception no longer raises an exception if
Neal Norwitz <nnorwitz@gmail.com>
parents: 39167
diff changeset
162 err = traceback.format_exception_only(X, X())
ec4f35d2a693 Bug #1531405, format_exception no longer raises an exception if
Neal Norwitz <nnorwitz@gmail.com>
parents: 39167
diff changeset
163 self.assertEqual(len(err), 1)
ec4f35d2a693 Bug #1531405, format_exception no longer raises an exception if
Neal Norwitz <nnorwitz@gmail.com>
parents: 39167
diff changeset
164 str_value = '<unprintable %s object>' % X.__name__
ec4f35d2a693 Bug #1531405, format_exception no longer raises an exception if
Neal Norwitz <nnorwitz@gmail.com>
parents: 39167
diff changeset
165 self.assertEqual(err[0], X.__name__ + ': ' + str_value + '\n')
39167
07cee01a4fcc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 39163
diff changeset
166
39883
ecaafc32c500 Fix a bug in traceback.format_exception_only() that led to an error
Georg Brandl <georg@python.org>
parents: 39362
diff changeset
167 def test_without_exception(self):
ecaafc32c500 Fix a bug in traceback.format_exception_only() that led to an error
Georg Brandl <georg@python.org>
parents: 39362
diff changeset
168 err = traceback.format_exception_only(None, None)
ecaafc32c500 Fix a bug in traceback.format_exception_only() that led to an error
Georg Brandl <georg@python.org>
parents: 39362
diff changeset
169 self.assertEqual(err, ['None\n'])
ecaafc32c500 Fix a bug in traceback.format_exception_only() that led to an error
Georg Brandl <georg@python.org>
parents: 39362
diff changeset
170
39163
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
171
46589
07a4e1710262 Fix a bug introduced by the warnings rewrite where tracebacks were being
Brett Cannon <bcannon@gmail.com>
parents: 39883
diff changeset
172 class TracebackFormatTests(unittest.TestCase):
07a4e1710262 Fix a bug introduced by the warnings rewrite where tracebacks were being
Brett Cannon <bcannon@gmail.com>
parents: 39883
diff changeset
173
52953
75def7ed9d5f Rolled back revisions 71237 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52903
diff changeset
174 def test_traceback_indentation(self):
46589
07a4e1710262 Fix a bug introduced by the warnings rewrite where tracebacks were being
Brett Cannon <bcannon@gmail.com>
parents: 39883
diff changeset
175 # Make sure that the traceback is properly indented.
52953
75def7ed9d5f Rolled back revisions 71237 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52903
diff changeset
176 tb_lines = example_traceback.splitlines()
46589
07a4e1710262 Fix a bug introduced by the warnings rewrite where tracebacks were being
Brett Cannon <bcannon@gmail.com>
parents: 39883
diff changeset
177 self.assertEquals(len(tb_lines), 3)
07a4e1710262 Fix a bug introduced by the warnings rewrite where tracebacks were being
Brett Cannon <bcannon@gmail.com>
parents: 39883
diff changeset
178 banner, location, source_line = tb_lines
07a4e1710262 Fix a bug introduced by the warnings rewrite where tracebacks were being
Brett Cannon <bcannon@gmail.com>
parents: 39883
diff changeset
179 self.assert_(banner.startswith('Traceback'))
07a4e1710262 Fix a bug introduced by the warnings rewrite where tracebacks were being
Brett Cannon <bcannon@gmail.com>
parents: 39883
diff changeset
180 self.assert_(location.startswith(' File'))
47933
4f804ee772eb #3342: In tracebacks, printed source lines were not indented since r62555.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46592
diff changeset
181 self.assert_(source_line.startswith(' raise'))
46589
07a4e1710262 Fix a bug introduced by the warnings rewrite where tracebacks were being
Brett Cannon <bcannon@gmail.com>
parents: 39883
diff changeset
182
07a4e1710262 Fix a bug introduced by the warnings rewrite where tracebacks were being
Brett Cannon <bcannon@gmail.com>
parents: 39883
diff changeset
183
20013
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 17558
diff changeset
184 def test_main():
46589
07a4e1710262 Fix a bug introduced by the warnings rewrite where tracebacks were being
Brett Cannon <bcannon@gmail.com>
parents: 39883
diff changeset
185 run_unittest(TracebackCases, TracebackFormatTests)
20013
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 17558
diff changeset
186
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 17558
diff changeset
187
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 17558
diff changeset
188 if __name__ == "__main__":
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 17558
diff changeset
189 test_main()