FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.15] gh-71896: Add a docs warning about trailing newlines ignored by `difflib.HtmlDiff` (GH-153930) by miss-islington · Pull Request #153965 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
5 changes: 5 additions & 0 deletions Doc/library/difflib.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ diffs. For comparing directories and files, see also, the :mod:`filecmp` module.
with inter-line and intra-line change highlights. The table can be generated in
either full or contextual difference mode.

.. warning::

The trailing newlines get stripped before the diff, so the result can be
incomplete. See :gh:`71896` for details.

The constructor for this class is:


Expand Down
2 changes: 2 additions & 0 deletions Lib/difflib.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2016,6 +2016,8 @@ def make_table(self,fromlines,tolines,fromdesc='',todesc='',context=False,

# change tabs to spaces before it gets more difficult after we insert
# markup
# it also removes trailing newlines, causing some diffs to be missed
# see: gh-71896
fromlines,tolines = self._tab_newline_replace(fromlines,tolines)

# create diffs iterator which generates side by side from/to data
Expand Down
23 changes: 23 additions & 0 deletions Lib/test/test_difflib.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,29 @@ def test_make_file_usascii_charset_with_nonascii_input(self):
self.assertIn('charset="us-ascii"', output)
self.assertIn('ımplıcıt', output)

def test_strip_trailing_newlines_before_diff(self):
# characterization test for the current buggy behavior
# see: gh-71896
html_diff = difflib.HtmlDiff()
from_lines = [
"Line 1: no newline after",
"Line 2: one newline after\n",
"Line 3: several newlines after\n\n\n\n\n",
]
to_lines = [
"Line 1: no newline after",
"Line 2: one newline after", # actually no \n
"Line 3: several newlines after", # actually no \n
]
output = html_diff.make_table(from_lines, to_lines)
# we (currently) expect no line change, so all equal
self.assertNotIn('class="diff_add"', output)
self.assertNotIn('class="diff_chg"', output)
self.assertNotIn('class="diff_sub"', output)
self.assertEqual(output.count('>Line&nbsp;1:&nbsp;no&nbsp;newline&nbsp;after<'), 2)
self.assertEqual(output.count('>Line&nbsp;2:&nbsp;one&nbsp;newline&nbsp;after<'), 2)
self.assertEqual(output.count('>Line&nbsp;3:&nbsp;several&nbsp;newlines&nbsp;after<'), 2)

class TestDiffer(unittest.TestCase):
def test_close_matches_aligned(self):
# Of the 4 closely matching pairs, we want 1 to match with 3,
Expand Down
Loading

Back | FazBrowse Home | New Git URL