[Python-checkins] [3.6] compare with difflib not diff(1) (GH-5450) (GH-5453)

Benjamin Peterson webhook-mailer at python.org
Tue Jan 30 14:31:16 EST 2018


https://github.com/python/cpython/commit/eb126eddbd7542ac9d7cd2736116aee2e0bd03dd
commit: eb126eddbd7542ac9d7cd2736116aee2e0bd03dd
branch: 3.6
author: Benjamin Peterson <benjamin at python.org>
committer: GitHub <noreply at github.com>
date: 2018-01-30T11:31:10-08:00
summary:

[3.6] compare with difflib not diff(1) (GH-5450) (GH-5453)

Co-authored-by: Benjamin Peterson <benjamin at python.org>

files:
M Lib/lib2to3/tests/test_parser.py

diff --git a/Lib/lib2to3/tests/test_parser.py b/Lib/lib2to3/tests/test_parser.py
index 9d444c204aa6..3fc499522454 100644
--- a/Lib/lib2to3/tests/test_parser.py
+++ b/Lib/lib2to3/tests/test_parser.py
@@ -12,6 +12,7 @@
 from test.support import verbose
 
 # Python imports
+import difflib
 import importlib
 import operator
 import os
@@ -429,8 +430,8 @@ def test_all_project_files(self):
                 except ParseError as err:
                     self.fail('ParseError on file %s (%s)' % (filepath, err))
             new = str(tree)
-            x = diff(filepath, new, encoding=encoding)
-            if x:
+            if new != source:
+                print(diff_texts(source, new, filepath))
                 self.fail("Idempotency failed: %s" % filepath)
 
     def test_extended_unpacking(self):
@@ -473,14 +474,9 @@ def test_multiline_str_literals(self):
         self.validate(s)
 
 
-def diff(fn, result, encoding='utf-8'):
-    try:
-        with open('@', 'w', encoding=encoding, newline='\n') as f:
-            f.write(str(result))
-        fn = fn.replace('"', '\\"')
-        return subprocess.call(['diff', '-u', fn, '@'], stdout=(subprocess.DEVNULL if verbose < 1 else None))
-    finally:
-        try:
-            os.remove("@")
-        except OSError:
-            pass
+def diff_texts(a, b, filename):
+    a = a.splitlines()
+    b = b.splitlines()
+    return difflib.unified_diff(a, b, filename, filename,
+                                "(original)", "(reserialized)",
+                                lineterm="")



More information about the Python-checkins mailing list