def test_traceback_format(self): if traceback_print is None: raise unittest.SkipTest('Requires _testcapi') 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'))
def test_traceback_format(self): if due_to_ironpython_bug("http://www.codeplex.com/IronPython/WorkItem/View.aspx?WorkItemId=21116"): return 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.assertEquals(traceback_fmt, python_fmt) # Make sure that the traceback is properly indented. tb_lines = python_fmt.splitlines() self.assertEquals(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'))
def check_traceback_format(self, cleanup_func=None): try: if issubclass(six.binary_type, six.string_types): # Python 2.6 or other platform where the interpreter # is likely going to be spitting out bytes, which will # then fail with io.StringIO(), so we skip the cross- # checks with the C API there. Note that _testcapi # is included in (at least) Ubuntu CPython packages, which # makes the import check less effective than desired. raise ImportError from _testcapi import traceback_print except ImportError: traceback_print = None 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 = u('Traceback (most recent call last):\n') + \ u('').join(traceback.format_tb(tb)) if traceback_print is not None: 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: self.fail("unable to create test traceback string") # Make sure that Python and the traceback module format the same thing if traceback_print is not None: 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 = traceback_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 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.assertEquals(traceback_fmt, python_fmt) # Make sure that the traceback is properly indented. tb_lines = python_fmt.splitlines() self.assertEquals(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"))
def test_traceback_format(self): from _testcapi import traceback_print 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'))
"""Test cases for traceback module""" from _testcapi import traceback_print from StringIO import StringIO import sys import unittest from test.test_support import run_unittest, is_jython, Error 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 TracebackCases(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, value: return traceback.format_exception_only(exc, value) else: