Skip to content

Commit 53cf5f0

Browse files
bpo-24746: Avoid stripping trailing whitespace in doctest fancy diff (GH-10639)
(cherry picked from commit cbb1645) Co-authored-by: Sanyam Khurana <[email protected]>
1 parent 88ad48b commit 53cf5f0

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

Lib/doctest.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,8 +1690,6 @@ def output_difference(self, example, got, optionflags):
16901690
kind = 'ndiff with -expected +actual'
16911691
else:
16921692
assert 0, 'Bad diff option'
1693-
# Remove trailing whitespace on diff output.
1694-
diff = [line.rstrip() + '\n' for line in diff]
16951693
return 'Differences (%s):\n' % kind + _indent(''.join(diff))
16961694

16971695
# If we're not using diff, then simply list the expected

Lib/test/test_doctest.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2450,6 +2450,11 @@ def test_unittest_reportflags():
24502450
Then the default eporting options are ignored:
24512451
24522452
>>> result = suite.run(unittest.TestResult())
2453+
"""
2454+
"""
2455+
*NOTE*: These doctest are intentionally not placed in raw string to depict
2456+
the trailing whitespace using `\x20` in the diff below.
2457+
24532458
>>> print(result.failures[0][1]) # doctest: +ELLIPSIS
24542459
Traceback ...
24552460
Failed example:
@@ -2463,7 +2468,7 @@ def test_unittest_reportflags():
24632468
Differences (ndiff with -expected +actual):
24642469
a
24652470
- <BLANKLINE>
2466-
+
2471+
+\x20
24672472
b
24682473
<BLANKLINE>
24692474
<BLANKLINE>
@@ -2952,6 +2957,47 @@ def test_CLI(): r"""
29522957
29532958
"""
29542959

2960+
def test_no_trailing_whitespace_stripping():
2961+
r"""
2962+
The fancy reports had a bug for a long time where any trailing whitespace on
2963+
the reported diff lines was stripped, making it impossible to see the
2964+
differences in line reported as different that differed only in the amount of
2965+
trailing whitespace. The whitespace still isn't particularly visible unless
2966+
you use NDIFF, but at least it is now there to be found.
2967+
2968+
*NOTE*: This snippet was intentionally put inside a raw string to get rid of
2969+
leading whitespace error in executing the example below
2970+
2971+
>>> def f(x):
2972+
... r'''
2973+
... >>> print('\n'.join(['a ', 'b']))
2974+
... a
2975+
... b
2976+
... '''
2977+
"""
2978+
"""
2979+
*NOTE*: These doctest are not placed in raw string to depict the trailing whitespace
2980+
using `\x20`
2981+
2982+
>>> test = doctest.DocTestFinder().find(f)[0]
2983+
>>> flags = doctest.REPORT_NDIFF
2984+
>>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
2985+
... # doctest: +ELLIPSIS
2986+
**********************************************************************
2987+
File ..., line 3, in f
2988+
Failed example:
2989+
print('\n'.join(['a ', 'b']))
2990+
Differences (ndiff with -expected +actual):
2991+
- a
2992+
+ a
2993+
b
2994+
TestResults(failed=1, attempted=1)
2995+
2996+
*NOTE*: `\x20` is for checking the trailing whitespace on the +a line above.
2997+
We cannot use actual spaces there, as a commit hook prevents from committing
2998+
patches that contain trailing whitespace. More info on Issue 24746.
2999+
"""
3000+
29553001
######################################################################
29563002
## Main
29573003
######################################################################
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Avoid stripping trailing whitespace in doctest fancy diff. Orignial patch by
2+
R. David Murray & Jairo Trad. Enhanced by Sanyam Khurana.

0 commit comments

Comments
 (0)