def test_traceback_format_with_vars(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, with_vars=True)) else: raise Error("unable to create test traceback string") # Make sure that the traceback is properly indented. tb_lines = traceback_fmt.splitlines() self.assertEquals(len(tb_lines), 8) banner, location, variables, var1, var2, var3, var4, source_line = tb_lines self.assertTrue(banner.startswith('Traceback')) self.assertTrue(location.startswith(' File')) self.assertTrue(variables.startswith(' Local variables')) 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, with_vars=False)) 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'))