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

Method output_difference

Lib/doctest.py:1817–1861  ·  view source on GitHub ↗

Return a string describing the differences between the expected output for a given example (`example`) and the actual output (`got`). `optionflags` is the set of option flags used to compare `want` and `got`.

(self, example, got, optionflags)

Source from the content-addressed store, hash-verified

1815 return want.count('\n') > 2 and got.count('\n') > 2
1816
1817 def output_difference(self, example, got, optionflags):
1818 """
1819 Return a string describing the differences between the
1820 expected output for a given example (`example`) and the actual
1821 output (`got`). `optionflags` is the set of option flags used
1822 to compare `want` and `got`.
1823 """
1824 want = example.want
1825 # If <BLANKLINE>s are being used, then replace blank lines
1826 # with <BLANKLINE> in the actual output string.
1827 if not (optionflags & DONT_ACCEPT_BLANKLINE):
1828 got = re.sub('(?m)^[ ]*(?=\n)', BLANKLINE_MARKER, got)
1829
1830 # Check if we should use diff.
1831 if self._do_a_fancy_diff(want, got, optionflags):
1832 # Split want & got into lines.
1833 want_lines = want.splitlines(keepends=True)
1834 got_lines = got.splitlines(keepends=True)
1835 # Use difflib to find their differences.
1836 if optionflags & REPORT_UDIFF:
1837 diff = difflib.unified_diff(want_lines, got_lines, n=2)
1838 diff = list(diff)[2:] # strip the diff header
1839 kind = 'unified diff with -expected +actual'
1840 elif optionflags & REPORT_CDIFF:
1841 diff = difflib.context_diff(want_lines, got_lines, n=2)
1842 diff = list(diff)[2:] # strip the diff header
1843 kind = 'context diff with expected followed by actual'
1844 elif optionflags & REPORT_NDIFF:
1845 engine = difflib.Differ(charjunk=difflib.IS_CHARACTER_JUNK)
1846 diff = list(engine.compare(want_lines, got_lines))
1847 kind = 'ndiff with -expected +actual'
1848 else:
1849 assert 0, 'Bad diff option'
1850 return 'Differences (%s):\n' % kind + _indent(''.join(diff))
1851
1852 # If we're not using diff, then simply list the expected
1853 # output followed by the actual output.
1854 if want and got:
1855 return 'Expected:\n%sGot:\n%s' % (_indent(want), _indent(got))
1856 elif want:
1857 return 'Expected:\n%sGot nothing\n' % _indent(want)
1858 elif got:
1859 return 'Expected nothing\nGot:\n%s' % _indent(got)
1860 else:
1861 return 'Expected nothing\nGot nothing\n'
1862
1863class DocTestFailure(Exception):
1864 """A DocTest example has failed in debugging mode.

Callers 2

report_failureMethod · 0.80
report_failureMethod · 0.80

Calls 7

_do_a_fancy_diffMethod · 0.95
compareMethod · 0.95
listClass · 0.85
_indentFunction · 0.85
subMethod · 0.45
splitlinesMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected