summaryrefslogtreecommitdiffstats
path: root/Lib/difflib.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2011-04-12 22:19:33 (GMT)
committerRaymond Hettinger <python@rcn.com>2011-04-12 22:19:33 (GMT)
commitf03d3028e0e9ce1a9d4d108d8ecf210a917cd5f8 (patch)
treef76c33f71f15a1484b494727881de764fad70ec8 /Lib/difflib.py
parent94f90dd4a1038232c354c3506a5cf365c4a88531 (diff)
downloadcpython-f03d3028e0e9ce1a9d4d108d8ecf210a917cd5f8.zip
cpython-f03d3028e0e9ce1a9d4d108d8ecf210a917cd5f8.tar.gz
cpython-f03d3028e0e9ce1a9d4d108d8ecf210a917cd5f8.tar.bz2
Issue 11747: Fix output format for context diffs.
Diffstat (limited to 'Lib/difflib.py')
-rw-r--r--Lib/difflib.py30
1 files changed, 25 insertions, 5 deletions
diff --git a/Lib/difflib.py b/Lib/difflib.py
index c5005a1..e6cc6ee 100644
--- a/Lib/difflib.py
+++ b/Lib/difflib.py
@@ -1144,7 +1144,11 @@ def IS_CHARACTER_JUNK(ch, ws=" \t"):
return ch in ws
-def _format_range(start, stop):
+########################################################################
+### Unified Diff
+########################################################################
+
+def _format_range_unified(start, stop):
'Convert range to the "ed" format'
# Per the diff spec at http://www.unix.org/single_unix_specification/
beginning = start + 1 # lines start numbering with one
@@ -1206,8 +1210,8 @@ def unified_diff(a, b, fromfile='', tofile='', fromfiledate='',
yield '+++ {}{}{}'.format(tofile, todate, lineterm)
first, last = group[0], group[-1]
- file1_range = _format_range(first[1], last[2])
- file2_range = _format_range(first[3], last[4])
+ file1_range = _format_range_unified(first[1], last[2])
+ file2_range = _format_range_unified(first[3], last[4])
yield '@@ -{} +{} @@{}'.format(file1_range, file2_range, lineterm)
for tag, i1, i2, j1, j2 in group:
@@ -1222,6 +1226,22 @@ def unified_diff(a, b, fromfile='', tofile='', fromfiledate='',
for line in b[j1:j2]:
yield '+' + line
+
+########################################################################
+### Context Diff
+########################################################################
+
+def _format_range_context(start, stop):
+ 'Convert range to the "ed" format'
+ # Per the diff spec at http://www.unix.org/single_unix_specification/
+ beginning = start + 1 # lines start numbering with one
+ length = stop - start
+ if not length:
+ beginning -= 1 # empty ranges begin at line just before the range
+ if length <= 1:
+ return '{}'.format(beginning)
+ return '{},{}'.format(beginning, beginning + length - 1)
+
# See http://www.unix.org/single_unix_specification/
def context_diff(a, b, fromfile='', tofile='',
fromfiledate='', tofiledate='', n=3, lineterm='\n'):
@@ -1280,7 +1300,7 @@ def context_diff(a, b, fromfile='', tofile='',
first, last = group[0], group[-1]
yield '***************' + lineterm
- file1_range = _format_range(first[1], last[2])
+ file1_range = _format_range_context(first[1], last[2])
yield '*** {} ****{}'.format(file1_range, lineterm)
if any(tag in {'replace', 'delete'} for tag, _, _, _, _ in group):
@@ -1289,7 +1309,7 @@ def context_diff(a, b, fromfile='', tofile='',
for line in a[i1:i2]:
yield prefix[tag] + line
- file2_range = _format_range(first[3], last[4])
+ file2_range = _format_range_context(first[3], last[4])
yield '--- {} ----{}'.format(file2_range, lineterm)
if any(tag in {'replace', 'insert'} for tag, _, _, _, _ in group):