[PATCH 4 of 4] thg: always use repo.vfs() as a context manager throughout the code base

1 view
Skip to first unread message

Antonio Muci

unread,
Dec 3, 2025, 4:36:21 PMDec 3
to thg...@googlegroups.com, a....@inwind.it
# HG changeset patch
# User Antonio Muci <a....@inwind.it>
# Date 1764793390 -3600
# Wed Dec 03 21:23:10 2025 +0100
# Branch stable
# Node ID 50aead3c96d86e76cc1ad222645f13c47cdb3f9f
# Parent 4f4ce1fa4e0ccda3a3850516df855c4a38955f49
thg: always use repo.vfs() as a context manager throughout the code base

Based on previous commits, and after ascertaining that there are no
counterindications to using repo.vfs() as a context manager, let's change every
invocation of repo.vfs().

I have no immediate reproducers for issues solved by there changes; however, I
do not see any drawbacks in being more conservative with respect to resource
cleanup.

diff --git a/tortoisehg/hgqt/commit.py b/tortoisehg/hgqt/commit.py
--- a/tortoisehg/hgqt/commit.py
+++ b/tortoisehg/hgqt/commit.py
@@ -981,7 +981,8 @@ class CommitWidget(QWidget, qtlib.TaskWi
except OSError:
pass
try:
- curmsg = self.repo.vfs(b'last-message.txt').read()
+ with self.repo.vfs(b'last-message.txt') as f:
+ curmsg = f.read()
if curmsg:
self.addMessageToHistory(hglib.tounicode(curmsg))
except OSError:
diff --git a/tortoisehg/hgqt/compress.py b/tortoisehg/hgqt/compress.py
--- a/tortoisehg/hgqt/compress.py
+++ b/tortoisehg/hgqt/compress.py
@@ -123,7 +123,8 @@ class CompressDialog(QDialog):
tip, base = self.revs
revs = [c for c in self.repo.revs(b'%d::%d' % (base, tip)) if c != base]
descs = [self.repo[c].description() for c in revs]
- self.repo.vfs(b'cur-message.txt', b'w').write(b'\n* * *\n'.join(descs))
+ with self.repo.vfs(b'cur-message.txt', b'w') as f:
+ f.write(b'\n* * *\n'.join(descs))

dlg = commit.CommitDialog(self._repoagent, [], {}, self)
dlg.finished.connect(dlg.deleteLater)
diff --git a/tortoisehg/util/hglib.py b/tortoisehg/util/hglib.py
--- a/tortoisehg/util/hglib.py
+++ b/tortoisehg/util/hglib.py
@@ -388,7 +388,8 @@ def readundodesc(repo) -> Tuple[str, int
"""Read short description and changelog size of last transaction"""
if os.path.exists(repo.sjoin(b'undo')):
try:
- args = repo.vfs(b'undo.desc', b'r').read().splitlines()
+ with repo.vfs(b'undo.desc', b'r') as f:
+ args = f.read().splitlines()
return tounicode(args[1]), int(args[0])
except (OSError, IndexError, ValueError):
pass
Reply all
Reply to author
Forward
0 new messages