annotate Lib/test/test_traceback.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 3889d9528601
children 75def7ed9d5f ed6ed3c839f5
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
46589
07a4e1710262 Fix a bug introduced by the warnings rewrite where tracebacks were being
Brett Cannon <bcannon@gmail.com>
parents: 39883
diff changeset
11
17117
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
12 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
13 # 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
14 # 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
15
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
16 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
17 try:
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
18 func()
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
19 except exc, value:
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
20 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
21 else:
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
22 raise ValueError, "call did not raise exception"
17341
83ee63bcf036 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 17117
diff changeset
23
17117
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
24 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
25 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
26
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
27 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
28 # 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
29 import test.badsyntax_nocaret
37407
2834ccf552f3 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37334
diff changeset
30
37334
a4950ea556b5 traceback now shows error position for all SyntaxError subclasses,
Georg Brandl <georg@python.org>
parents: 35890
diff changeset
31 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
32 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
33
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
34 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
35 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
36 SyntaxError)
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
37 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
38 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
39 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
40 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
41
17117
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
42 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
43 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
44 # 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
45 return
17117
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
46 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
47 SyntaxError)
33d8dd02aa99 Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
48 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
49 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
50
37334
a4950ea556b5 traceback now shows error position for all SyntaxError subclasses,
Georg Brandl <georg@python.org>
parents: 35890
diff changeset
51 def test_bad_indentation(self):
a4950ea556b5 traceback now shows error position for all SyntaxError subclasses,
Georg Brandl <georg@python.org>
parents: 35890
diff changeset
52 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
53 IndentationError)
a4950ea556b5 traceback now shows error position for all SyntaxError subclasses,
Georg Brandl <georg@python.org>
parents: 35890
diff changeset
54 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
55 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
56 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
57 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
58
33839
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
59 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
60 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
61
33839
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
62 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
63 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
64 try:
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
65 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
66 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
67 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
68 def test():
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
69 raise ValueError"""
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
70
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
71 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
72 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
73 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
74
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
75 try:
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
76 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
77 except ValueError:
33848
3398c5ce8b7a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 33847
diff changeset
78 # 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
79 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
80
33878
ef5030bc5f67 * Bumped up the sleep() delay to four seconds so this test will run
Raymond Hettinger <python@rcn.com>
parents: 33874
diff changeset
81 # 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
82 # 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
83 # 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
84 # 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
85 # 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
86 # 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
87 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
88
33878
ef5030bc5f67 * Bumped up the sleep() delay to four seconds so this test will run
Raymond Hettinger <python@rcn.com>
parents: 33874
diff changeset
89 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
90 def test():
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
91 raise NotImplementedError"""
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
92 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
93 try:
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
94 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
95 except NotImplementedError:
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
96 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
97 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
98 finally:
4dd97c37c2cc SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26071
diff changeset
99 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
100 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
101 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
102 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
103
38083
bb3e0c889632 Fix the formatting of KeyboardInterrupt -- a bad issubclass() call.
Guido van Rossum <guido@python.org>
parents: 37652
diff changeset
104 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
105 # 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
106 e = KeyboardInterrupt()
bb3e0c889632 Fix the formatting of KeyboardInterrupt -- a bad issubclass() call.
Guido van Rossum <guido@python.org>
parents: 37652
diff changeset
107 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
108 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
109
39163
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
110 # String exceptions are deprecated, but legal. The quirky form with
39167
07cee01a4fcc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 39163
diff changeset
111 # 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
112 # not isinstance(value, type)
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
113 # 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
114 #
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
115 # 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
116 # 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
117 #
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
118 # 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
119 #
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
120 # >>> raise "String Exception"
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
121 #
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
122 # 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
123 #
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
124 # >>> 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
125 #
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
126 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
127 str_type = "String Exception"
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
128 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
129 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
130 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
131
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
132 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
133 str_type = "String Exception"
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
134 str_value = "String Value"
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
135 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
136 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
137 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
138
ec4f35d2a693 Bug #1531405, format_exception no longer raises an exception if
Neal Norwitz <nnorwitz@gmail.com>
parents: 39167
diff changeset
139 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
140 class X(Exception):
ec4f35d2a693 Bug #1531405, format_exception no longer raises an exception if
Neal Norwitz <nnorwitz@gmail.com>
parents: 39167
diff changeset
141 def __str__(self):
ec4f35d2a693 Bug #1531405, format_exception no longer raises an exception if
Neal Norwitz <nnorwitz@gmail.com>
parents: 39167
diff changeset
142 1/0
ec4f35d2a693 Bug #1531405, format_exception no longer raises an exception if
Neal Norwitz <nnorwitz@gmail.com>
parents: 39167
diff changeset
143 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
144 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
145 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
146 self.assertEqual(err[0], X.__name__ + ': ' + str_value + '\n')
39167
07cee01a4fcc Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 39163
diff changeset
147
39883
ecaafc32c500 Fix a bug in traceback.format_exception_only() that led to an error
Georg Brandl <georg@python.org>
parents: 39362
diff changeset
148 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
149 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
150 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
151
39163
89e090fef985 Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents: 38083
diff changeset
152
46589
07a4e1710262 Fix a bug introduced by the warnings rewrite where tracebacks were being
Brett Cannon <bcannon@gmail.com>
parents: 39883
diff changeset
153 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
154
52903
3889d9528601 Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52221
diff changeset
155 def test_traceback_format(self):
3889d9528601 Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52221
diff changeset
156 try:
3889d9528601 Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52221
diff changeset
157 raise KeyError('blah')
3889d9528601 Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52221
diff changeset
158 except KeyError:
3889d9528601 Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52221
diff changeset
159 type_, value, tb = sys.exc_info()
3889d9528601 Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52221
diff changeset
160 traceback_fmt = 'Traceback (most recent call last):\n' + \
3889d9528601 Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52221
diff changeset
161 ''.join(traceback.format_tb(tb))
3889d9528601 Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52221
diff changeset
162 file_ = StringIO()
3889d9528601 Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52221
diff changeset
163 traceback_print(tb, file_)
3889d9528601 Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52221
diff changeset
164 python_fmt = file_.getvalue()
3889d9528601 Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52221
diff changeset
165 else:
3889d9528601 Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52221
diff changeset
166 raise Error("unable to create test traceback string")
3889d9528601 Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52221
diff changeset
167
3889d9528601 Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52221
diff changeset
168 # Make sure that Python and the traceback module format the same thing
3889d9528601 Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52221
diff changeset
169 self.assertEquals(traceback_fmt, python_fmt)
3889d9528601 Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52221
diff changeset
170
46589
07a4e1710262 Fix a bug introduced by the warnings rewrite where tracebacks were being
Brett Cannon <bcannon@gmail.com>
parents: 39883
diff changeset
171 # Make sure that the traceback is properly indented.
52903
3889d9528601 Merged revisions 71058,71149-71150,71212,71214-71216,71222,71225,71234,71237-71238,71240-71241,71243,71249,71251 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52221
diff changeset
172 tb_lines = python_fmt.splitlines()
46589
07a4e1710262 Fix a bug introduced by the warnings rewrite where tracebacks were being
Brett Cannon <bcannon@gmail.com>
parents: 39883
diff changeset
173 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
174 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
175 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
176 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
177 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
178
07a4e1710262 Fix a bug introduced by the warnings rewrite where tracebacks were being
Brett Cannon <bcannon@gmail.com>
parents: 39883
diff changeset
179
20013
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 17558
diff changeset
180 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
181 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
182
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 17558
diff changeset
183
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 17558
diff changeset
184 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
185 test_main()