Mercurial > hg
diff mercurial/posix.py @ 45717:755214a84b9d
posix: use context managers in a couple of places
Differential Revision: https://phab.mercurial-scm.org/D9205
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Tue, 13 Oct 2020 16:41:01 -0400 |
parents | 8e8fd938ca07 |
children | 87c35b5a14eb |
line wrap: on
line diff
--- a/mercurial/posix.py Wed Oct 14 14:43:39 2020 -0700 +++ b/mercurial/posix.py Tue Oct 13 16:41:01 2020 -0400 @@ -144,26 +144,24 @@ if l: if not stat.S_ISLNK(s): # switch file to link - fp = open(f, b'rb') - data = fp.read() - fp.close() + with open(f, b'rb') as fp: + data = fp.read() unlink(f) try: os.symlink(data, f) except OSError: # failed to make a link, rewrite file - fp = open(f, b"wb") - fp.write(data) - fp.close() + with open(f, b"wb") as fp: + fp.write(data) + # no chmod needed at this point return if stat.S_ISLNK(s): # switch link to file data = os.readlink(f) unlink(f) - fp = open(f, b"wb") - fp.write(data) - fp.close() + with open(f, b"wb") as fp: + fp.write(data) s = 0o666 & ~umask # avoid restatting for chmod sx = s & 0o100