changeset 52595:4c7194ed56ab

relink: don't leak a file descriptor when comparing file content It only would have happened in an exception case, but there are now language contructs to tighten this up.
author Matt Harbison <matt_harbison@yahoo.com>
date Mon, 16 Dec 2024 21:06:27 -0500
parents 7108459cc801
children 2380fa1158bd
files hgext/relink.py
diffstat 1 files changed, 10 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/relink.py	Mon Dec 16 21:01:02 2024 -0500
+++ b/hgext/relink.py	Mon Dec 16 21:06:27 2024 -0500
@@ -184,19 +184,17 @@
         source = os.path.join(src, f)
         tgt = os.path.join(dst, f)
         # Binary mode, so that read() works correctly, especially on Windows
-        sfp = open(source, 'rb')
-        dfp = open(tgt, 'rb')
-        sin = sfp.read(CHUNKLEN)
-        while sin:
-            din = dfp.read(CHUNKLEN)
-            if sin != din:
-                break
+        with open(source, 'rb') as sfp, open(tgt, 'rb') as dfp:
             sin = sfp.read(CHUNKLEN)
-        sfp.close()
-        dfp.close()
-        if sin:
-            ui.debug(b'not linkable: %s\n' % f)
-            continue
+            while sin:
+                din = dfp.read(CHUNKLEN)
+                if sin != din:
+                    break
+                sin = sfp.read(CHUNKLEN)
+
+            if sin:
+                ui.debug(b'not linkable: %s\n' % f)
+                continue
         try:
             relinkfile(source, tgt)
             progress.update(pos, item=f)