]> granicus.if.org Git - libexpat/commitdiff
udiffer.py: Extract function to resolve code duplication
authorSebastian Pipping <sebastian@pipping.org>
Sat, 9 Sep 2017 15:50:41 +0000 (17:50 +0200)
committerSebastian Pipping <sebastian@pipping.org>
Sat, 9 Sep 2017 16:21:18 +0000 (18:21 +0200)
expat/tests/udiffer.py

index ef6a7307e80a11fee1a2c064f8448e9cb78dc5ce..4f7f759958116521c7a0b18a4cdacfa00ad08b1c 100755 (executable)
 import sys
 import difflib
 
+
+def _read_lines(filename):
+    try:
+        with open(filename) as f:
+            return f.readlines()
+    except UnicodeDecodeError:
+        with open(filename, encoding='utf_16') as f:
+            return f.readlines()
+
+
 if len(sys.argv) != 3:
     sys.exit(2)
 
-try:
-    with open(sys.argv[1]) as infile1:
-        first = infile1.readlines()
-except UnicodeDecodeError:
-    with open(sys.argv[1], encoding="utf_16") as infile1:
-        first = infile1.readlines()
-try:
-    with open(sys.argv[2]) as infile2:
-        second = infile2.readlines()
-except UnicodeDecodeError:
-    with open(sys.argv[2], encoding="utf_16") as infile2:
-        second = infile2.readlines()
+first = _read_lines(sys.argv[1])
+second = _read_lines(sys.argv[2])
 
 diffs = list(difflib.unified_diff(first, second,
                                   fromfile=sys.argv[1], tofile=sys.argv[2]))