posix: use context managers in a couple of places
authorMatt Harbison <matt_harbison@yahoo.com>
Tue, 13 Oct 2020 16:41:01 -0400
changeset 45717 755214a84b9d
parent 45716 9628d3cd9d13
child 45718 87c35b5a14eb
posix: use context managers in a couple of places Differential Revision: https://phab.mercurial-scm.org/D9205
mercurial/posix.py
--- 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