PyXR

c:\python24\lib \ test \ test_traceback.py



0001 """Test cases for traceback module"""
0002 
0003 import unittest
0004 from test.test_support import run_unittest, is_jython
0005 
0006 import traceback
0007 
0008 class TracebackCases(unittest.TestCase):
0009     # For now, a very minimal set of tests.  I want to be sure that
0010     # formatting of SyntaxErrors works based on changes for 2.1.
0011 
0012     def get_exception_format(self, func, exc):
0013         try:
0014             func()
0015         except exc, value:
0016             return traceback.format_exception_only(exc, value)
0017         else:
0018             raise ValueError, "call did not raise exception"
0019 
0020     def syntax_error_with_caret(self):
0021         compile("def fact(x):\n\treturn x!\n", "?", "exec")
0022 
0023     def syntax_error_without_caret(self):
0024         # XXX why doesn't compile raise the same traceback?
0025         import test.badsyntax_nocaret
0026 
0027     def test_caret(self):
0028         err = self.get_exception_format(self.syntax_error_with_caret,
0029                                         SyntaxError)
0030         self.assert_(len(err) == 4)
0031         self.assert_("^" in err[2]) # third line has caret
0032         self.assert_(err[1].strip() == "return x!")
0033 
0034     def test_nocaret(self):
0035         if is_jython:
0036             # jython adds a caret in this case (why shouldn't it?)
0037             return
0038         err = self.get_exception_format(self.syntax_error_without_caret,
0039                                         SyntaxError)
0040         self.assert_(len(err) == 3)
0041         self.assert_(err[1].strip() == "[x for x in x] = x")
0042 
0043     def test_bug737473(self):
0044         import sys, os, tempfile, time
0045 
0046         savedpath = sys.path[:]
0047         testdir = tempfile.mkdtemp()
0048         try:
0049             sys.path.insert(0, testdir)
0050             testfile = os.path.join(testdir, 'test_bug737473.py')
0051             print >> open(testfile, 'w'), """
0052 def test():
0053     raise ValueError"""
0054 
0055             if 'test_bug737473' in sys.modules:
0056                 del sys.modules['test_bug737473']
0057             import test_bug737473
0058 
0059             try:
0060                 test_bug737473.test()
0061             except ValueError:
0062                 # this loads source code to linecache
0063                 traceback.extract_tb(sys.exc_traceback)
0064 
0065             # If this test runs too quickly, test_bug737473.py's mtime
0066             # attribute will remain unchanged even if the file is rewritten.
0067             # Consequently, the file would not reload.  So, added a sleep()
0068             # delay to assure that a new, distinct timestamp is written.
0069             # Since WinME with FAT32 has multisecond resolution, more than
0070             # three seconds are needed for this test to pass reliably :-(
0071             time.sleep(4)
0072 
0073             print >> open(testfile, 'w'), """
0074 def test():
0075     raise NotImplementedError"""
0076             reload(test_bug737473)
0077             try:
0078                 test_bug737473.test()
0079             except NotImplementedError:
0080                 src = traceback.extract_tb(sys.exc_traceback)[-1][-1]
0081                 self.failUnlessEqual(src, 'raise NotImplementedError')
0082         finally:
0083             sys.path[:] = savedpath
0084             for f in os.listdir(testdir):
0085                 os.unlink(os.path.join(testdir, f))
0086             os.rmdir(testdir)
0087 
0088 def test_main():
0089     run_unittest(TracebackCases)
0090 
0091 
0092 if __name__ == "__main__":
0093     test_main()
0094 

Generated by PyXR 0.9.4
SourceForge.net Logo