MCPcopy Index your code
hub / github.com/python/cpython / check_traceback_format

Method check_traceback_format

Lib/test/test_traceback.py:1907–1954  ·  view source on GitHub ↗
(self, cleanup_func=None)

Source from the content-addressed store, hash-verified

1905
1906 @cpython_only
1907 def check_traceback_format(self, cleanup_func=None):
1908 from _testcapi import traceback_print
1909 try:
1910 self.some_exception()
1911 except KeyError as e:
1912 tb = e.__traceback__
1913 if cleanup_func is not None:
1914 # Clear the inner frames, not this one
1915 cleanup_func(tb.tb_next)
1916 traceback_fmt = 'Traceback (most recent call last):\n' + \
1917 ''.join(traceback.format_tb(tb))
1918 # clear caret lines from traceback_fmt since internal API does
1919 # not emit them
1920 traceback_fmt = "\n".join(
1921 self._filter_debug_ranges(traceback_fmt.splitlines())
1922 ) + "\n"
1923 file_ = StringIO()
1924 traceback_print(tb, file_)
1925 python_fmt = file_.getvalue()
1926 # Call all _tb and _exc functions
1927 with captured_output("stderr") as tbstderr:
1928 traceback.print_tb(tb)
1929 tbfile = StringIO()
1930 traceback.print_tb(tb, file=tbfile)
1931 with captured_output("stderr") as excstderr:
1932 traceback.print_exc()
1933 excfmt = traceback.format_exc()
1934 excfile = StringIO()
1935 traceback.print_exc(file=excfile)
1936 else:
1937 raise Error("unable to create test traceback string")
1938
1939 # Make sure that Python and the traceback module format the same thing
1940 self.assertEqual(traceback_fmt, python_fmt)
1941 # Now verify the _tb func output
1942 self.assertEqual(tbstderr.getvalue(), tbfile.getvalue())
1943 # Now verify the _exc func output
1944 self.assertEqual(excstderr.getvalue(), excfile.getvalue())
1945 self.assertEqual(excfmt, excfile.getvalue())
1946
1947 # Make sure that the traceback is properly indented.
1948 tb_lines = python_fmt.splitlines()
1949 banner = tb_lines[0]
1950 self.assertEqual(len(tb_lines), 5)
1951 location, source_line = tb_lines[-2], tb_lines[-1]
1952 self.assertStartsWith(banner, 'Traceback')
1953 self.assertStartsWith(location, ' File')
1954 self.assertStartsWith(source_line, ' raise')
1955
1956 def test_traceback_format(self):
1957 self.check_traceback_format()

Calls 12

some_exceptionMethod · 0.95
_filter_debug_rangesMethod · 0.95
getvalueMethod · 0.95
StringIOClass · 0.90
captured_outputFunction · 0.90
ErrorClass · 0.90
print_excMethod · 0.80
assertStartsWithMethod · 0.80
joinMethod · 0.45
splitlinesMethod · 0.45
assertEqualMethod · 0.45
getvalueMethod · 0.45

Tested by

no test coverage detected