# HG changeset patch
# User Antonio Muci <
a....@inwind.it>
# Date 1764792040 -3600
# Wed Dec 03 21:00:40 2025 +0100
# Branch stable
# Node ID c8190fecad6c84d13ae8bff95a8a8abd6de2f50c
# Parent 4fe75c79a27914b607cbc4b9e3400a0c2affaa99
thgrepo: use a context manager to get rid of ResourceWarning when starting up
Before this change, starting up thg generated a ResourceWarning: unclosed file
on .hg/branch and .hg/dirstate (no other action needed).
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
````
No need to do anything in the GUI. The console shows:
```
[...]
<BASE>/tortoisehg/hgqt/thgrepo.py:350: ResourceWarning: unclosed file <_io.BufferedReader name=b'<BASE>/.hg/branch'>
return self._repo.vfs(b'branch').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)
<BASE>/tortoisehg/hgqt/thgrepo.py:347: ResourceWarning: unclosed file <_io.BufferedReader name=b'<BASE>/.hg/dirstate'>
return self._repo.vfs(b'dirstate').read(40)
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)
```
Initial discussion at
https://foss.heptapod.net/mercurial/tortoisehg/thg/-/issues/6037
diff --git a/tortoisehg/hgqt/thgrepo.py b/tortoisehg/hgqt/thgrepo.py
--- a/tortoisehg/hgqt/thgrepo.py
+++ b/tortoisehg/hgqt/thgrepo.py
@@ -344,10 +344,12 @@ class RepoWatcher(QObject):
return changeflags
def _readparents(self):
- return self._repo.vfs(b'dirstate').read(40)
+ with self._repo.vfs(b'dirstate') as f:
+ return f.read(40)
def _readbranch(self):
- return self._repo.vfs(b'branch').read()
+ with self._repo.vfs(b'branch') as f:
+ return f.read()
def _checkuimtime(self):
'Check for modified config files, or a new .hg/hgrc file'