use open context managers in more places

Use open() as a context manager to simplify the close logic and make
the code easier to read & understand.  This is also more Pythonic.

Change-Id: I579d03cca86f99b2c6c6a1f557f6e5704e2515a7
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/244734
Reviewed-by: David Pursehouse <dpursehouse@collab.net>
Tested-by: Mike Frysinger <vapier@google.com>
This commit is contained in:
Mike Frysinger
2019-11-11 05:40:22 -05:00
parent f454512619
commit 3164d40e22
7 changed files with 21 additions and 66 deletions

View File

@ -106,11 +106,8 @@ least one of these before using this command.""", file=sys.stderr)
raise EditorError('editor failed with exit status %d: %s %s'
% (rc, editor, path))
fd2 = open(path)
try:
with open(path) as fd2:
return fd2.read()
finally:
fd2.close()
finally:
if fd:
os.close(fd)