def check_traceback_format(self, cleanup_func=None):
     from _testcapi import traceback_print
     try:
         self.some_exception()
     except KeyError:
         type_, value, tb = sys.exc_info()
         if cleanup_func is not None:
             cleanup_func(tb.tb_next)
         traceback_fmt = 'Traceback (most recent call last):\n' + ''.join(
             traceback.format_tb(tb))
         file_ = StringIO()
         traceback_print(tb, file_)
         python_fmt = file_.getvalue()
         with captured_output('stderr') as tbstderr:
             traceback.print_tb(tb)
         tbfile = StringIO()
         traceback.print_tb(tb, file=tbfile)
         with captured_output('stderr') as excstderr:
             traceback.print_exc()
         excfmt = traceback.format_exc()
         excfile = StringIO()
         traceback.print_exc(file=excfile)
     else:
         raise Error('unable to create test traceback string')
     self.assertEqual(traceback_fmt, python_fmt)
     self.assertEqual(tbstderr.getvalue(), tbfile.getvalue())
     self.assertEqual(excstderr.getvalue(), excfile.getvalue())
     self.assertEqual(excfmt, excfile.getvalue())
     tb_lines = python_fmt.splitlines()
     self.assertEqual(len(tb_lines), 5)
     banner = tb_lines[0]
     location, source_line = tb_lines[-2:]
     self.assertTrue(banner.startswith('Traceback'))
     self.assertTrue(location.startswith('  File'))
     self.assertTrue(source_line.startswith('    raise'))
示例#2
0
    def check_traceback_format(self, cleanup_func=None):
        from _testcapi import traceback_print
        try:
            self.some_exception()
        except KeyError:
            type_, value, tb = sys.exc_info()
            if cleanup_func is not None:
                # Clear the inner frames, not this one
                cleanup_func(tb.tb_next)
            traceback_fmt = 'Traceback (most recent call last):\n' + \
                            ''.join(traceback.format_tb(tb))
            file_ = StringIO()
            traceback_print(tb, file_)
            python_fmt = file_.getvalue()
            # Call all _tb and _exc functions
            with captured_output("stderr") as tbstderr:
                traceback.print_tb(tb)
            tbfile = StringIO()
            traceback.print_tb(tb, file=tbfile)
            with captured_output("stderr") as excstderr:
                traceback.print_exc()
            excfmt = traceback.format_exc()
            excfile = StringIO()
            traceback.print_exc(file=excfile)
        else:
            raise Error("unable to create test traceback string")

        # Make sure that Python and the traceback module format the same thing
        self.assertEqual(traceback_fmt, python_fmt)
        # Now verify the _tb func output
        self.assertEqual(tbstderr.getvalue(), tbfile.getvalue())
        # Now verify the _exc func output
        self.assertEqual(excstderr.getvalue(), excfile.getvalue())
        self.assertEqual(excfmt, excfile.getvalue())

        # Make sure that the traceback is properly indented.
        tb_lines = python_fmt.splitlines()
        self.assertEqual(len(tb_lines), 5)
        banner = tb_lines[0]
        location, source_line = tb_lines[-2:]
        self.assertTrue(banner.startswith('Traceback'))
        self.assertTrue(location.startswith('  File'))
        self.assertTrue(source_line.startswith('    raise'))
    def test_traceback_format(self):
        try:
            raise KeyError('blah')
        except KeyError:
            type_, value, tb = sys.exc_info()
            traceback_fmt = 'Traceback (most recent call last):\n' + \
                            ''.join(traceback.format_tb(tb))
            file_ = StringIO()
            traceback_print(tb, file_)
            python_fmt = file_.getvalue()
        else:
            raise Error("unable to create test traceback string")

        # Make sure that Python and the traceback module format the same thing
        self.assertEqual(traceback_fmt, python_fmt)

        # Make sure that the traceback is properly indented.
        tb_lines = python_fmt.splitlines()
        self.assertEqual(len(tb_lines), 3)
        banner, location, source_line = tb_lines
        self.assertTrue(banner.startswith('Traceback'))
        self.assertTrue(location.startswith('  File'))
        self.assertTrue(source_line.startswith('    raise'))
import unittest
import re
from test.support import run_unittest, is_jython, Error, captured_output
from test.support import TESTFN, unlink

import traceback

try:
    raise KeyError
except KeyError:
    type_, value, tb = sys.exc_info()
    file_ = StringIO()
    traceback_print(tb, file_)
    example_traceback = file_.getvalue()
else:
    raise Error("unable to create test traceback string")


class SyntaxTracebackCases(unittest.TestCase):
    # For now, a very minimal set of tests.  I want to be sure that
    # formatting of SyntaxErrors works based on changes for 2.1.

    def get_exception_format(self, func, exc):
        try:
            func()
        except exc as value:
            return traceback.format_exception_only(exc, value)
        else:
            raise ValueError("call did not raise exception")

    def syntax_error_with_caret(self):