From 7c0f6d71cce80dab8cfd87f32b756c48b9e305cc Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Sat, 9 Sep 2017 17:50:41 +0200 Subject: [PATCH] udiffer.py: Extract function to resolve code duplication --- expat/tests/udiffer.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/expat/tests/udiffer.py b/expat/tests/udiffer.py index ef6a7307..4f7f7599 100755 --- a/expat/tests/udiffer.py +++ b/expat/tests/udiffer.py @@ -31,21 +31,21 @@ 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])) -- 2.40.0