Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_traceback.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | b36cb4602e21 |
| children |
| 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 |
|
63556
dba99828d91c
Merged revisions 77942,79023 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
61234
diff
changeset
|
7 from imp import reload |
|
46589
07a4e1710262
Fix a bug introduced by the warnings rewrite where tracebacks were being
Brett Cannon <bcannon@gmail.com>
parents:
39883
diff
changeset
|
8 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
|
9 |
|
33d8dd02aa99
Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
10 import traceback |
|
33d8dd02aa99
Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
11 |
|
46589
07a4e1710262
Fix a bug introduced by the warnings rewrite where tracebacks were being
Brett Cannon <bcannon@gmail.com>
parents:
39883
diff
changeset
|
12 |
|
17117
33d8dd02aa99
Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
13 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
|
14 # 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
|
15 # 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
|
16 |
|
33d8dd02aa99
Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
17 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
|
18 try: |
|
33d8dd02aa99
Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
19 func() |
|
33d8dd02aa99
Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
20 except exc, value: |
|
33d8dd02aa99
Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
21 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
|
22 else: |
|
33d8dd02aa99
Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
23 raise ValueError, "call did not raise exception" |
|
17341
83ee63bcf036
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
17117
diff
changeset
|
24 |
|
17117
33d8dd02aa99
Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
25 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
|
26 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
|
27 |
|
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
|
28 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
|
29 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
|
30 |
|
17117
33d8dd02aa99
Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
31 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
|
32 # 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
|
33 import test.badsyntax_nocaret |
|
37407
2834ccf552f3
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
37334
diff
changeset
|
34 |
|
37334
a4950ea556b5
traceback now shows error position for all SyntaxError subclasses,
Georg Brandl <georg@python.org>
parents:
35890
diff
changeset
|
35 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
|
36 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
|
37 |
|
33d8dd02aa99
Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
38 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
|
39 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
|
40 SyntaxError) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54582
diff
changeset
|
41 self.assertTrue(len(err) == 4) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54582
diff
changeset
|
42 self.assertTrue(err[1].strip() == "return x!") |
|
58460
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58198
diff
changeset
|
43 self.assertIn("^", err[2]) # third line has caret |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54582
diff
changeset
|
44 self.assertTrue(err[1].find("!") == err[2].find("^")) # in the right place |
|
17341
83ee63bcf036
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
17117
diff
changeset
|
45 |
|
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
|
46 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
|
47 SyntaxError) |
|
58460
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58198
diff
changeset
|
48 self.assertIn("^", err[2]) # third line has caret |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54582
diff
changeset
|
49 self.assertTrue(err[2].count('\n') == 1) # and no additional newline |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54582
diff
changeset
|
50 self.assertTrue(err[1].find("+") == err[2].find("^")) # in the right place |
|
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
|
51 |
|
17117
33d8dd02aa99
Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
52 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
|
53 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
|
54 # 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
|
55 return |
|
17117
33d8dd02aa99
Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
56 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
|
57 SyntaxError) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54582
diff
changeset
|
58 self.assertTrue(len(err) == 3) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54582
diff
changeset
|
59 self.assertTrue(err[1].strip() == "[x for x in x] = x") |
|
17117
33d8dd02aa99
Test that traceback module works with SyntaxErrors with or without carets.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
60 |
|
37334
a4950ea556b5
traceback now shows error position for all SyntaxError subclasses,
Georg Brandl <georg@python.org>
parents:
35890
diff
changeset
|
61 def test_bad_indentation(self): |
|
a4950ea556b5
traceback now shows error position for all SyntaxError subclasses,
Georg Brandl <georg@python.org>
parents:
35890
diff
changeset
|
62 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
|
63 IndentationError) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54582
diff
changeset
|
64 self.assertTrue(len(err) == 4) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54582
diff
changeset
|
65 self.assertTrue(err[1].strip() == "print 2") |
|
58460
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58198
diff
changeset
|
66 self.assertIn("^", err[2]) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54582
diff
changeset
|
67 self.assertTrue(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
|
68 |
|
33839
4dd97c37c2cc
SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
26071
diff
changeset
|
69 def test_bug737473(self): |
|
58817
70e115c34f97
Remove unused imports in test modules.
Georg Brandl <georg@python.org>
parents:
58669
diff
changeset
|
70 import os, tempfile, time |
|
33847
5510995ea26f
Paper over bug 1054615 by passing sane values to os.utime().
Tim Peters <tim.peters@gmail.com>
parents:
33839
diff
changeset
|
71 |
|
33839
4dd97c37c2cc
SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
26071
diff
changeset
|
72 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
|
73 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
|
74 try: |
|
4dd97c37c2cc
SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
26071
diff
changeset
|
75 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
|
76 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
|
77 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
|
78 def test(): |
|
4dd97c37c2cc
SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
26071
diff
changeset
|
79 raise ValueError""" |
|
4dd97c37c2cc
SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
26071
diff
changeset
|
80 |
|
4dd97c37c2cc
SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
26071
diff
changeset
|
81 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
|
82 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
|
83 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
|
84 |
|
4dd97c37c2cc
SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
26071
diff
changeset
|
85 try: |
|
4dd97c37c2cc
SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
26071
diff
changeset
|
86 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
|
87 except ValueError: |
|
33848
3398c5ce8b7a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33847
diff
changeset
|
88 # 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
|
89 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
|
90 |
|
33878
ef5030bc5f67
* Bumped up the sleep() delay to four seconds so this test will run
Raymond Hettinger <python@rcn.com>
parents:
33874
diff
changeset
|
91 # 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
|
92 # 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
|
93 # 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
|
94 # 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
|
95 # 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
|
96 # 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
|
97 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
|
98 |
|
33878
ef5030bc5f67
* Bumped up the sleep() delay to four seconds so this test will run
Raymond Hettinger <python@rcn.com>
parents:
33874
diff
changeset
|
99 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
|
100 def test(): |
|
4dd97c37c2cc
SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
26071
diff
changeset
|
101 raise NotImplementedError""" |
|
4dd97c37c2cc
SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
26071
diff
changeset
|
102 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
|
103 try: |
|
4dd97c37c2cc
SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
26071
diff
changeset
|
104 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
|
105 except NotImplementedError: |
|
4dd97c37c2cc
SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
26071
diff
changeset
|
106 src = traceback.extract_tb(sys.exc_traceback)[-1][-1] |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54582
diff
changeset
|
107 self.assertEqual(src, 'raise NotImplementedError') |
|
33839
4dd97c37c2cc
SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
26071
diff
changeset
|
108 finally: |
|
4dd97c37c2cc
SF #737473: Show up-to-date source code in tracebacks always.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
26071
diff
changeset
|
109 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
|
110 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
|
111 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
|
112 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
|
113 |
|
38083
bb3e0c889632
Fix the formatting of KeyboardInterrupt -- a bad issubclass() call.
Guido van Rossum <guido@python.org>
parents:
37652
diff
changeset
|
114 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
|
115 # 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
|
116 e = KeyboardInterrupt() |
|
bb3e0c889632
Fix the formatting of KeyboardInterrupt -- a bad issubclass() call.
Guido van Rossum <guido@python.org>
parents:
37652
diff
changeset
|
117 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
|
118 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
|
119 |
|
39163
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
120 # String exceptions are deprecated, but legal. The quirky form with |
|
39167
07cee01a4fcc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
39163
diff
changeset
|
121 # 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
|
122 # not isinstance(value, type) |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
123 # 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
|
124 # |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
125 # 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
|
126 # 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
|
127 # |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
128 # 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
|
129 # |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
130 # >>> raise "String Exception" |
|
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 # 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
|
133 # |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
134 # >>> 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
|
135 # |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
136 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
|
137 str_type = "String Exception" |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
138 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
|
139 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
|
140 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
|
141 |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
142 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
|
143 str_type = "String Exception" |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
144 str_value = "String Value" |
|
89e090fef985
Patch #1515343: Fix printing of deprecated string exceptions with a
Georg Brandl <georg@python.org>
parents:
38083
diff
changeset
|
145 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
|
146 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
|
147 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
|
148 |
|
ec4f35d2a693
Bug #1531405, format_exception no longer raises an exception if
Neal Norwitz <nnorwitz@gmail.com>
parents:
39167
diff
changeset
|
149 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
|
150 class X(Exception): |
|
ec4f35d2a693
Bug #1531405, format_exception no longer raises an exception if
Neal Norwitz <nnorwitz@gmail.com>
parents:
39167
diff
changeset
|
151 def __str__(self): |
|
63556
dba99828d91c
Merged revisions 77942,79023 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
61234
diff
changeset
|
152 1 // 0 |
|
39362
ec4f35d2a693
Bug #1531405, format_exception no longer raises an exception if
Neal Norwitz <nnorwitz@gmail.com>
parents:
39167
diff
changeset
|
153 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
|
154 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
|
155 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
|
156 self.assertEqual(err[0], X.__name__ + ': ' + str_value + '\n') |
|
39167
07cee01a4fcc
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
39163
diff
changeset
|
157 |
|
39883
ecaafc32c500
Fix a bug in traceback.format_exception_only() that led to an error
Georg Brandl <georg@python.org>
parents:
39362
diff
changeset
|
158 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
|
159 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
|
160 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
|
161 |
|
61234
4380e04343ee
Merged revisions 80777 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
56830
diff
changeset
|
162 def test_unicode(self): |
|
4380e04343ee
Merged revisions 80777 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
56830
diff
changeset
|
163 err = AssertionError('\xff') |
|
4380e04343ee
Merged revisions 80777 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
56830
diff
changeset
|
164 lines = traceback.format_exception_only(type(err), err) |
|
4380e04343ee
Merged revisions 80777 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
56830
diff
changeset
|
165 self.assertEqual(lines, ['AssertionError: \xff\n']) |
|
4380e04343ee
Merged revisions 80777 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
56830
diff
changeset
|
166 |
|
4380e04343ee
Merged revisions 80777 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
56830
diff
changeset
|
167 err = AssertionError(u'\xe9') |
|
4380e04343ee
Merged revisions 80777 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
56830
diff
changeset
|
168 lines = traceback.format_exception_only(type(err), err) |
|
4380e04343ee
Merged revisions 80777 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
56830
diff
changeset
|
169 self.assertEqual(lines, ['AssertionError: \\xe9\n']) |
|
4380e04343ee
Merged revisions 80777 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
56830
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 |
|
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
|
174 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
|
175 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
|
176 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
|
177 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
|
178 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
|
179 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
|
180 ''.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
|
181 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
|
182 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
|
183 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
|
184 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
|
185 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
|
186 |
|
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
|
187 # Make sure that Python and the traceback module format the same thing |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
61232
diff
changeset
|
188 self.assertEqual(traceback_fmt, python_fmt) |
|
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
|
189 |
|
46589
07a4e1710262
Fix a bug introduced by the warnings rewrite where tracebacks were being
Brett Cannon <bcannon@gmail.com>
parents:
39883
diff
changeset
|
190 # 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
|
191 tb_lines = python_fmt.splitlines() |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
61232
diff
changeset
|
192 self.assertEqual(len(tb_lines), 3) |
|
46589
07a4e1710262
Fix a bug introduced by the warnings rewrite where tracebacks were being
Brett Cannon <bcannon@gmail.com>
parents:
39883
diff
changeset
|
193 banner, location, source_line = tb_lines |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54582
diff
changeset
|
194 self.assertTrue(banner.startswith('Traceback')) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54582
diff
changeset
|
195 self.assertTrue(location.startswith(' File')) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54582
diff
changeset
|
196 self.assertTrue(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
|
197 |
|
07a4e1710262
Fix a bug introduced by the warnings rewrite where tracebacks were being
Brett Cannon <bcannon@gmail.com>
parents:
39883
diff
changeset
|
198 |
|
20013
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
17558
diff
changeset
|
199 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
|
200 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
|
201 |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
17558
diff
changeset
|
202 |
|
e3184e46303b
Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents:
17558
diff
changeset
|
203 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
|
204 test_main() |
