[Python-Dev] opinions on issue2142 ('\ No newline at end of file' to difflib.unified_diff)?

Trent Mick trentm at gmail.com
Thu Oct 7 00:53:59 CEST 2010

Soliciting opinions on issue 2142 (http://bugs.python.org/issue2142).
There are patched available so this isn't vapour. :)

The issue is this (I'll discuss only unified_diff(), but the same
applies to context_diff()):

>>> from difflib import *
>>> gen = unified_diff("one\ntwo\nthree".splitlines(1),
...                    "one\ntwo\ntrois".splitlines(1))
>>> print ''.join(gen)
@@ -1,3 +1,3 @@

Where as with `diff`, `hg` and `git`:

diff -r 667b0870428d a
--- a/a Wed Oct 06 15:39:50 2010 -0700
+++ b/a Wed Oct 06 15:40:31 2010 -0700
@@ -1,3 +1,3 @@
\ No newline at end of file
\ No newline at end of file

While originally marked as a *bug*, the issue was changed to be a
*feature* request, because arguably `difflib.unified_diff()` is fine,
and the problem is in the naive use of the following to create a patch
that will work with `patch`:



1. Change `difflib.unified_diff` to emit:

    ['---  \n', '+++  \n', '@@ -1,3 +1,3 @@\n', ' one\n', ' two\n',
'-three\n', '\ No newline at end of file', '+trois\n', '\ No newline
at end of file']

instead of:

    ['---  \n', '+++  \n', '@@ -1,3 +1,3 @@\n', ' one\n', ' two\n',
'-three', '+trois']

for this case.

2. Add a `add_end_of_file_newline_markers_to_make_patch_happy` keyword
arg (probably with a different name:) to `difflib.unified_diff` to do
this additional handling. The reason is to not surprise existing code
that would be surprised with those "\No newline at end of file"

3. Not touch `difflib.unified_diff` and instead update
documentation to discuss the issue and show how users of unified_diff
should handle this case themselves.


Orthogonal: *After* a decision is made for the Python 3.3 tree we can
discuss if including this in either of Python 2.7 or 3.2 would be

Trent Mick

More information about the Python-Dev mailing list