[PATCH 2 of 4] thgrepo: use a context manager to get rid of ResourceWarning when clicking on working copy

0 views
Skip to first unread message

Antonio Muci

unread,
Dec 3, 2025, 4:36:20 PMDec 3
to thg...@googlegroups.com, a....@inwind.it
# HG changeset patch
# User Antonio Muci <a....@inwind.it>
# Date 1764792397 -3600
# Wed Dec 03 21:06:37 2025 +0100
# Branch stable
# Node ID 740cab310732f256ae5cd2a46a811bb6c9dfe45e
# Parent c8190fecad6c84d13ae8bff95a8a8abd6de2f50c
thgrepo: use a context manager to get rid of ResourceWarning when clicking on working copy

Before this change, clicking on the row representing the working directory (like
when doing before committing) generated a ResourceWarning: unclosed file
on .hg/cur-message.txt.

Tested on python3.14.0, hg 7.1.1, thg 7.0.1.

Command line code to replicate the issue:
```
$ python3.14 -X tracemalloc -Wall ./thg
````

Click on the working directory in the main window to replicate the issue.
The generated warning is:

```
<BASE>/tortoisehg/hgqt/commit.py:978: ResourceWarning: unclosed file <_io.BufferedReader name=b'<BASE>/.hg/cur-message.txt'>
curmsg = self.repo.vfs(b'cur-message.txt').read()
Object allocated at (most recent call last):
File "/usr/lib64/python3.14/site-packages/mercurial/pycompat.py", lineno 407
return builtins.open(name, sysstr(mode), buffering, encoding)
```

diff --git a/tortoisehg/hgqt/commit.py b/tortoisehg/hgqt/commit.py
--- a/tortoisehg/hgqt/commit.py
+++ b/tortoisehg/hgqt/commit.py
@@ -975,7 +975,8 @@ class CommitWidget(QWidget, qtlib.TaskWi
self.userhist = qtlib.readStringList(s, gpref + 'userhist')
self.userhist = [u for u in self.userhist if u]
try:
- curmsg = self.repo.vfs(b'cur-message.txt').read()
+ with self.repo.vfs(b'cur-message.txt') as f:
+ curmsg = f.read()
self.setMessage(hglib.tounicode(curmsg))
except OSError:
pass
Reply all
Reply to author
Forward
0 new messages