~ubuntu-branches/ubuntu/karmic/pypy/karmic

« back to all changes in this revision

Viewing changes to lib-python/modified-2.4.1/test/test_traceback.py

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2007-04-13 09:33:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070413093309-yoojh4jcoocu2krz
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Test cases for traceback module"""
 
2
 
 
3
import unittest
 
4
from test.test_support import run_unittest, is_jython
 
5
 
 
6
import traceback
 
7
 
 
8
class TracebackCases(unittest.TestCase):
 
9
    # For now, a very minimal set of tests.  I want to be sure that
 
10
    # formatting of SyntaxErrors works based on changes for 2.1.
 
11
 
 
12
    def get_exception_format(self, func, exc):
 
13
        try:
 
14
            func()
 
15
        except exc, value:
 
16
            return traceback.format_exception_only(exc, value)
 
17
        else:
 
18
            raise ValueError, "call did not raise exception"
 
19
 
 
20
    def syntax_error_with_caret(self):
 
21
        compile("def fact(x):\n\treturn x!\n", "?", "exec")
 
22
 
 
23
    def syntax_error_without_caret(self):
 
24
        # XXX why doesn't compile raise the same traceback?
 
25
        import test.badsyntax_nocaret
 
26
 
 
27
    def test_caret(self):
 
28
        err = self.get_exception_format(self.syntax_error_with_caret,
 
29
                                        SyntaxError)
 
30
        self.assert_(len(err) == 4)
 
31
        self.assert_("^" in err[2]) # third line has caret
 
32
        self.assert_(err[1].strip() == "return x!")
 
33
 
 
34
    def test_bug737473(self):
 
35
        import sys, os, tempfile, time
 
36
 
 
37
        savedpath = sys.path[:]
 
38
        testdir = tempfile.mkdtemp()
 
39
        try:
 
40
            sys.path.insert(0, testdir)
 
41
            testfile = os.path.join(testdir, 'test_bug737473.py')
 
42
            f = open(testfile, 'w')
 
43
            print >> f, """
 
44
def test():
 
45
    raise ValueError"""
 
46
            f.close()
 
47
 
 
48
            if 'test_bug737473' in sys.modules:
 
49
                del sys.modules['test_bug737473']
 
50
            import test_bug737473
 
51
 
 
52
            try:
 
53
                test_bug737473.test()
 
54
            except ValueError:
 
55
                # this loads source code to linecache
 
56
                traceback.extract_tb(sys.exc_traceback)
 
57
 
 
58
            # If this test runs too quickly, test_bug737473.py's mtime
 
59
            # attribute will remain unchanged even if the file is rewritten.
 
60
            # Consequently, the file would not reload.  So, added a sleep()
 
61
            # delay to assure that a new, distinct timestamp is written.
 
62
            # Since WinME with FAT32 has multisecond resolution, more than
 
63
            # three seconds are needed for this test to pass reliably :-(
 
64
            time.sleep(4)
 
65
 
 
66
            f = open(testfile, 'w')
 
67
            print >> f, """
 
68
def test():
 
69
    raise NotImplementedError"""
 
70
            f.close()
 
71
            reload(test_bug737473)
 
72
            try:
 
73
                test_bug737473.test()
 
74
            except NotImplementedError:
 
75
                src = traceback.extract_tb(sys.exc_traceback)[-1][-1]
 
76
                self.failUnlessEqual(src, 'raise NotImplementedError')
 
77
        finally:
 
78
            sys.path[:] = savedpath
 
79
            for f in os.listdir(testdir):
 
80
                os.unlink(os.path.join(testdir, f))
 
81
            os.rmdir(testdir)
 
82
 
 
83
def test_main():
 
84
    run_unittest(TracebackCases)
 
85
 
 
86
 
 
87
if __name__ == "__main__":
 
88
    test_main()