+++ /dev/null
-"""Test cases for traceback module"""\r
-\r
-from _testcapi import traceback_print\r
-from StringIO import StringIO\r
-import sys\r
-import unittest\r
-from imp import reload\r
-from test.test_support import run_unittest, is_jython, Error\r
-\r
-import traceback\r
-\r
-\r
-class TracebackCases(unittest.TestCase):\r
- # For now, a very minimal set of tests. I want to be sure that\r
- # formatting of SyntaxErrors works based on changes for 2.1.\r
-\r
- def get_exception_format(self, func, exc):\r
- try:\r
- func()\r
- except exc, value:\r
- return traceback.format_exception_only(exc, value)\r
- else:\r
- raise ValueError, "call did not raise exception"\r
-\r
- def syntax_error_with_caret(self):\r
- compile("def fact(x):\n\treturn x!\n", "?", "exec")\r
-\r
- def syntax_error_with_caret_2(self):\r
- compile("1 +\n", "?", "exec")\r
-\r
- def syntax_error_without_caret(self):\r
- # XXX why doesn't compile raise the same traceback?\r
- import test.badsyntax_nocaret\r
-\r
- def syntax_error_bad_indentation(self):\r
- compile("def spam():\n print 1\n print 2", "?", "exec")\r
-\r
- def test_caret(self):\r
- err = self.get_exception_format(self.syntax_error_with_caret,\r
- SyntaxError)\r
- self.assertTrue(len(err) == 4)\r
- self.assertTrue(err[1].strip() == "return x!")\r
- self.assertIn("^", err[2]) # third line has caret\r
- self.assertTrue(err[1].find("!") == err[2].find("^")) # in the right place\r
-\r
- err = self.get_exception_format(self.syntax_error_with_caret_2,\r
- SyntaxError)\r
- self.assertIn("^", err[2]) # third line has caret\r
- self.assertTrue(err[2].count('\n') == 1) # and no additional newline\r
- self.assertTrue(err[1].find("+") == err[2].find("^")) # in the right place\r
-\r
- def test_nocaret(self):\r
- if is_jython:\r
- # jython adds a caret in this case (why shouldn't it?)\r
- return\r
- err = self.get_exception_format(self.syntax_error_without_caret,\r
- SyntaxError)\r
- self.assertTrue(len(err) == 3)\r
- self.assertTrue(err[1].strip() == "[x for x in x] = x")\r
-\r
- def test_bad_indentation(self):\r
- err = self.get_exception_format(self.syntax_error_bad_indentation,\r
- IndentationError)\r
- self.assertTrue(len(err) == 4)\r
- self.assertTrue(err[1].strip() == "print 2")\r
- self.assertIn("^", err[2])\r
- self.assertTrue(err[1].find("2") == err[2].find("^"))\r
-\r
- def test_bug737473(self):\r
- import os, tempfile, time\r
-\r
- savedpath = sys.path[:]\r
- testdir = tempfile.mkdtemp()\r
- try:\r
- sys.path.insert(0, testdir)\r
- testfile = os.path.join(testdir, 'test_bug737473.py')\r
- print >> open(testfile, 'w'), """\r
-def test():\r
- raise ValueError"""\r
-\r
- if 'test_bug737473' in sys.modules:\r
- del sys.modules['test_bug737473']\r
- import test_bug737473\r
-\r
- try:\r
- test_bug737473.test()\r
- except ValueError:\r
- # this loads source code to linecache\r
- traceback.extract_tb(sys.exc_traceback)\r
-\r
- # If this test runs too quickly, test_bug737473.py's mtime\r
- # attribute will remain unchanged even if the file is rewritten.\r
- # Consequently, the file would not reload. So, added a sleep()\r
- # delay to assure that a new, distinct timestamp is written.\r
- # Since WinME with FAT32 has multisecond resolution, more than\r
- # three seconds are needed for this test to pass reliably :-(\r
- time.sleep(4)\r
-\r
- print >> open(testfile, 'w'), """\r
-def test():\r
- raise NotImplementedError"""\r
- reload(test_bug737473)\r
- try:\r
- test_bug737473.test()\r
- except NotImplementedError:\r
- src = traceback.extract_tb(sys.exc_traceback)[-1][-1]\r
- self.assertEqual(src, 'raise NotImplementedError')\r
- finally:\r
- sys.path[:] = savedpath\r
- for f in os.listdir(testdir):\r
- os.unlink(os.path.join(testdir, f))\r
- os.rmdir(testdir)\r
-\r
- def test_base_exception(self):\r
- # Test that exceptions derived from BaseException are formatted right\r
- e = KeyboardInterrupt()\r
- lst = traceback.format_exception_only(e.__class__, e)\r
- self.assertEqual(lst, ['KeyboardInterrupt\n'])\r
-\r
- # String exceptions are deprecated, but legal. The quirky form with\r
- # separate "type" and "value" tends to break things, because\r
- # not isinstance(value, type)\r
- # and a string cannot be the first argument to issubclass.\r
- #\r
- # Note that sys.last_type and sys.last_value do not get set if an\r
- # exception is caught, so we sort of cheat and just emulate them.\r
- #\r
- # test_string_exception1 is equivalent to\r
- #\r
- # >>> raise "String Exception"\r
- #\r
- # test_string_exception2 is equivalent to\r
- #\r
- # >>> raise "String Exception", "String Value"\r
- #\r
- def test_string_exception1(self):\r
- str_type = "String Exception"\r
- err = traceback.format_exception_only(str_type, None)\r
- self.assertEqual(len(err), 1)\r
- self.assertEqual(err[0], str_type + '\n')\r
-\r
- def test_string_exception2(self):\r
- str_type = "String Exception"\r
- str_value = "String Value"\r
- err = traceback.format_exception_only(str_type, str_value)\r
- self.assertEqual(len(err), 1)\r
- self.assertEqual(err[0], str_type + ': ' + str_value + '\n')\r
-\r
- def test_format_exception_only_bad__str__(self):\r
- class X(Exception):\r
- def __str__(self):\r
- 1 // 0\r
- err = traceback.format_exception_only(X, X())\r
- self.assertEqual(len(err), 1)\r
- str_value = '<unprintable %s object>' % X.__name__\r
- self.assertEqual(err[0], X.__name__ + ': ' + str_value + '\n')\r
-\r
- def test_without_exception(self):\r
- err = traceback.format_exception_only(None, None)\r
- self.assertEqual(err, ['None\n'])\r
-\r
- def test_unicode(self):\r
- err = AssertionError('\xff')\r
- lines = traceback.format_exception_only(type(err), err)\r
- self.assertEqual(lines, ['AssertionError: \xff\n'])\r
-\r
- err = AssertionError(u'\xe9')\r
- lines = traceback.format_exception_only(type(err), err)\r
- self.assertEqual(lines, ['AssertionError: \\xe9\n'])\r
-\r
-\r
-class TracebackFormatTests(unittest.TestCase):\r
-\r
- def test_traceback_format(self):\r
- try:\r
- raise KeyError('blah')\r
- except KeyError:\r
- type_, value, tb = sys.exc_info()\r
- traceback_fmt = 'Traceback (most recent call last):\n' + \\r
- ''.join(traceback.format_tb(tb))\r
- file_ = StringIO()\r
- traceback_print(tb, file_)\r
- python_fmt = file_.getvalue()\r
- else:\r
- raise Error("unable to create test traceback string")\r
-\r
- # Make sure that Python and the traceback module format the same thing\r
- self.assertEqual(traceback_fmt, python_fmt)\r
-\r
- # Make sure that the traceback is properly indented.\r
- tb_lines = python_fmt.splitlines()\r
- self.assertEqual(len(tb_lines), 3)\r
- banner, location, source_line = tb_lines\r
- self.assertTrue(banner.startswith('Traceback'))\r
- self.assertTrue(location.startswith(' File'))\r
- self.assertTrue(source_line.startswith(' raise'))\r
-\r
-\r
-def test_main():\r
- run_unittest(TracebackCases, TracebackFormatTests)\r
-\r
-\r
-if __name__ == "__main__":\r
- test_main()\r