[PATCH 1 of 4] thgrepo: use a context manager to get rid of ResourceWarning when starting up

2 views
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 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'

Antonio Muci

unread,
Dec 3, 2025, 4:36:22 PMDec 3
to thg...@googlegroups.com, a....@inwind.it
# HG changeset patch
# User Antonio Muci <a....@inwind.it>
# Date 1764792623 -3600
# Wed Dec 03 21:10:23 2025 +0100
# Branch stable
# Node ID 4f4ce1fa4e0ccda3a3850516df855c4a38955f49
# Parent 740cab310732f256ae5cd2a46a811bb6c9dfe45e
thgrepo: use a context manager to get rid of ResourceWarning when exiting thg

Before this change, exiting form thg while the row representing the working copy
is selected would cause 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
- exit the application

The generated warning is:

```
<BASE>/tortoisehg/hgqt/commit.py:1002: ResourceWarning: unclosed file <_io.BufferedWriter name=b'<BASE>/.hg/cur-message.txt'>
self.repo.vfs(b'cur-message.txt', b'w').write(msg)
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
@@ -999,7 +999,8 @@ class CommitWidget(QWidget, qtlib.TaskWi
s.setValue(gpref+'history-'+repoid, self.msghistory)
s.setValue(gpref+'userhist', self.userhist)
msg = self.getMessage(True)
- self.repo.vfs(b'cur-message.txt', b'w').write(msg)
+ with self.repo.vfs(b'cur-message.txt', b'w') as f:
+ f.write(msg)
except OSError:
pass

Yuya Nishihara

unread,
Dec 4, 2025, 7:08:15 AMDec 4
to 'Antonio Muci' via TortoiseHg Developers, a....@inwind.it
On Wed, 03 Dec 2025 22:36:14 +0100, 'Antonio Muci' via TortoiseHg
Developers wrote:
> # 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

Queued, thanks.
Reply all
Reply to author
Forward
0 new messages