From 9938a9f7db37283a1e3d2c52e1246c573ecd649b Mon Sep 17 00:00:00 2001 From: Douglas Bagnall Date: Thu, 19 Nov 2020 16:44:42 +1300 Subject: [PATCH] selftest/subunit: python file modernisation Python idioms for iterating over a line and closing it have improved, and we should keep up. Signed-off-by: Douglas Bagnall Reviewed-by: Noel Power --- selftest/subunithelper.py | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/selftest/subunithelper.py b/selftest/subunithelper.py index 26158cad339..4fbb5442839 100644 --- a/selftest/subunithelper.py +++ b/selftest/subunithelper.py @@ -45,10 +45,7 @@ def parse_results(msg_ops, statistics, fh): exitcode = 0 open_tests = {} - while fh: - l = fh.readline() - if l == "": - break + for l in fh: parts = l.split(None, 1) if not len(parts) == 2 or not l.startswith(parts[0]): msg_ops.output_msg(l) @@ -80,10 +77,7 @@ def parse_results(msg_ops, statistics, fh): reason = "" # reason may be specified in next lines terminated = False - while fh: - l = fh.readline() - if l == "": - break + for l in fh: msg_ops.control_msg(l) if l == "]\n": terminated = True @@ -250,8 +244,7 @@ def read_test_regexes(*names): files.append(name) for filename in files: - f = open(filename, 'r') - try: + with open(filename, 'r') as f: for l in f: l = l.strip() if l == "" or l[0] == "#": @@ -261,8 +254,7 @@ def read_test_regexes(*names): ret[regex.strip()] = reason.strip() else: ret[l] = None - finally: - f.close() + return ret -- 2.47.3