Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b2416e5

Browse files
committed
Merged revisions 80004 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r80004 | r.david.murray | 2010-04-12 12:35:19 -0400 (Mon, 12 Apr 2010) | 13 lines Issue #7585: use tab between components in unified and context diff headers. Instead of spaces between the filename and date (or whatever the string is that follows the filename, if any) use tabs. This is what the unix 'diff' command does, for example, and difflib was intended to follow the 'standard' way of doing diffs. This improves compatibility with patch tools. The docs and examples are also changed to recommended that the date format used be the ISO 8601 format, which is what modern diff tools emit by default. Patch by Anatoly Techtonik. ........
1 parent af9c3cf commit b2416e5

5 files changed

Lines changed: 51 additions & 21 deletions

File tree

Doc/library/difflib.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ diffs. For comparing directories and files, see also, the :mod:`filecmp` module.
140140

141141
The context diff format normally has a header for filenames and modification
142142
times. Any or all of these may be specified using strings for *fromfile*,
143-
*tofile*, *fromfiledate*, and *tofiledate*. The modification times are normally
144-
expressed in the format returned by :func:`time.ctime`. If not specified, the
143+
*tofile*, *fromfiledate*, and *tofiledate*. The modification times are normally
144+
expressed in the ISO 8601 format. If not specified, the
145145
strings default to blanks.
146146

147147
>>> s1 = ['bacon\n', 'eggs\n', 'ham\n', 'guido\n']
@@ -272,8 +272,8 @@ diffs. For comparing directories and files, see also, the :mod:`filecmp` module.
272272

273273
The context diff format normally has a header for filenames and modification
274274
times. Any or all of these may be specified using strings for *fromfile*,
275-
*tofile*, *fromfiledate*, and *tofiledate*. The modification times are normally
276-
expressed in the format returned by :func:`time.ctime`. If not specified, the
275+
*tofile*, *fromfiledate*, and *tofiledate*. The modification times are normally
276+
expressed in the ISO 8601 format. If not specified, the
277277
strings default to blanks.
278278

279279

Lib/difflib.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,18 +1160,18 @@ def unified_diff(a, b, fromfile='', tofile='', fromfiledate='',
11601160
11611161
The unidiff format normally has a header for filenames and modification
11621162
times. Any or all of these may be specified using strings for
1163-
'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'. The modification
1164-
times are normally expressed in the format returned by time.ctime().
1163+
'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'.
1164+
The modification times are normally expressed in the ISO 8601 format.
11651165
11661166
Example:
11671167
11681168
>>> for line in unified_diff('one two three four'.split(),
11691169
... 'zero one tree four'.split(), 'Original', 'Current',
1170-
... 'Sat Jan 26 23:30:50 1991', 'Fri Jun 06 10:20:52 2003',
1170+
... '2005-01-26 23:30:50', '2010-04-02 10:20:52',
11711171
... lineterm=''):
1172-
... print(line)
1173-
--- Original Sat Jan 26 23:30:50 1991
1174-
+++ Current Fri Jun 06 10:20:52 2003
1172+
... print(line) # doctest: +NORMALIZE_WHITESPACE
1173+
--- Original 2005-01-26 23:30:50
1174+
+++ Current 2010-04-02 10:20:52
11751175
@@ -1,4 +1,4 @@
11761176
+zero
11771177
one
@@ -1184,8 +1184,10 @@ def unified_diff(a, b, fromfile='', tofile='', fromfiledate='',
11841184
started = False
11851185
for group in SequenceMatcher(None,a,b).get_grouped_opcodes(n):
11861186
if not started:
1187-
yield '--- %s %s%s' % (fromfile, fromfiledate, lineterm)
1188-
yield '+++ %s %s%s' % (tofile, tofiledate, lineterm)
1187+
fromdate = '\t%s' % fromfiledate if fromfiledate else ''
1188+
todate = '\t%s' % tofiledate if tofiledate else ''
1189+
yield '--- %s%s%s' % (fromfile, fromdate, lineterm)
1190+
yield '+++ %s%s%s' % (tofile, todate, lineterm)
11891191
started = True
11901192
i1, i2, j1, j2 = group[0][1], group[-1][2], group[0][3], group[-1][4]
11911193
yield "@@ -%d,%d +%d,%d @@%s" % (i1+1, i2-i1, j1+1, j2-j1, lineterm)
@@ -1223,17 +1225,16 @@ def context_diff(a, b, fromfile='', tofile='',
12231225
The context diff format normally has a header for filenames and
12241226
modification times. Any or all of these may be specified using
12251227
strings for 'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'.
1226-
The modification times are normally expressed in the format returned
1227-
by time.ctime(). If not specified, the strings default to blanks.
1228+
The modification times are normally expressed in the ISO 8601 format.
1229+
If not specified, the strings default to blanks.
12281230
12291231
Example:
12301232
12311233
>>> print(''.join(context_diff('one\ntwo\nthree\nfour\n'.splitlines(1),
1232-
... 'zero\none\ntree\nfour\n'.splitlines(1), 'Original', 'Current',
1233-
... 'Sat Jan 26 23:30:50 1991', 'Fri Jun 06 10:22:46 2003')),
1234+
... 'zero\none\ntree\nfour\n'.splitlines(1), 'Original', 'Current')),
12341235
... end="")
1235-
*** Original Sat Jan 26 23:30:50 1991
1236-
--- Current Fri Jun 06 10:22:46 2003
1236+
*** Original
1237+
--- Current
12371238
***************
12381239
*** 1,4 ****
12391240
one
@@ -1251,8 +1252,10 @@ def context_diff(a, b, fromfile='', tofile='',
12511252
prefixmap = {'insert':'+ ', 'delete':'- ', 'replace':'! ', 'equal':' '}
12521253
for group in SequenceMatcher(None,a,b).get_grouped_opcodes(n):
12531254
if not started:
1254-
yield '*** %s %s%s' % (fromfile, fromfiledate, lineterm)
1255-
yield '--- %s %s%s' % (tofile, tofiledate, lineterm)
1255+
fromdate = '\t%s' % fromfiledate if fromfiledate else ''
1256+
todate = '\t%s' % tofiledate if tofiledate else ''
1257+
yield '*** %s%s%s' % (fromfile, fromdate, lineterm)
1258+
yield '--- %s%s%s' % (tofile, todate, lineterm)
12561259
started = True
12571260

12581261
yield '***************%s' % (lineterm,)

Lib/test/test_difflib.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,32 @@ def test_recursion_limit(self):
159159
difflib.SequenceMatcher(None, old, new).get_opcodes()
160160

161161

162+
class TestOutputFormat(unittest.TestCase):
163+
def test_tab_delimiter(self):
164+
args = ['one', 'two', 'Original', 'Current',
165+
'2005-01-26 23:30:50', '2010-04-02 10:20:52']
166+
ud = difflib.unified_diff(*args, lineterm='')
167+
self.assertEqual(list(ud)[0:2], [
168+
"--- Original\t2005-01-26 23:30:50",
169+
"+++ Current\t2010-04-02 10:20:52"])
170+
cd = difflib.context_diff(*args, lineterm='')
171+
self.assertEqual(list(cd)[0:2], [
172+
"*** Original\t2005-01-26 23:30:50",
173+
"--- Current\t2010-04-02 10:20:52"])
174+
175+
def test_no_trailing_tab_on_empty_filedate(self):
176+
args = ['one', 'two', 'Original', 'Current']
177+
ud = difflib.unified_diff(*args, lineterm='')
178+
self.assertEqual(list(ud)[0:2], ["--- Original", "+++ Current"])
179+
180+
cd = difflib.context_diff(*args, lineterm='')
181+
self.assertEqual(list(cd)[0:2], ["*** Original", "--- Current"])
182+
183+
162184
def test_main():
163185
difflib.HtmlDiff._default_prefix = 0
164186
Doctests = doctest.DocTestSuite(difflib)
165-
run_unittest(TestSFpatches, TestSFbugs, Doctests)
187+
run_unittest(TestSFpatches, TestSFbugs, TestOutputFormat, Doctests)
166188

167189
if __name__ == '__main__':
168190
test_main()

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,7 @@ Christian Tanzer
755755
Steven Taschuk
756756
Monty Taylor
757757
Amy Taylor
758+
Anatoly Techtonik
758759
Tobias Thelen
759760
James Thomas
760761
Robin Thomas

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,10 @@ C-API
312312
Library
313313
-------
314314

315+
- Issue #7585: difflib context and unified diffs now place a tab between
316+
filename and date, conforming to the 'standards' they were originally
317+
designed to follow. This improves compatibility with patch tools.
318+
315319
- Issue #7472: Fixed typo in email.encoders module; messages using ISO-2022
316320
character sets will now consistently use a Content-Transfer-Encoding of
317321
7bit rather than sometimes being marked as 8bit.

0 commit comments

Comments
 (0)