[PATCH 1 of 4] repowidget: use context manager in detectPatches() (the file was never closed)

1 view
Skip to first unread message

Antonio Muci

unread,
May 25, 2026, 4:08:55 AMMay 25
to thg...@googlegroups.com, a....@inwind.it
# HG changeset patch
# User Antonio Muci <a....@inwind.it>
# Date 1779696063 -7200
# Mon May 25 10:01:03 2026 +0200
# Branch stable
# Node ID f436999a6fc93007cd3f147512975e538a1024da
# Parent 8301a77afa3194434aa909bf8876c5319ab33d00
repowidget: use context manager in detectPatches() (the file was never closed)

The use of a context manager here is not a mere quirk, because the file
descriptor pf was never closed.

diff --git a/tortoisehg/hgqt/repowidget.py b/tortoisehg/hgqt/repowidget.py
--- a/tortoisehg/hgqt/repowidget.py
+++ b/tortoisehg/hgqt/repowidget.py
@@ -773,14 +773,14 @@ class RepoWidget(QWidget):
if not os.path.isfile(p):
continue
try:
- pf = open(p, 'rb')
- earlybytes = pf.read(4096)
- if b'\0' in earlybytes:
- continue
- pf.seek(0)
- with hglib.extractpatch(self.repo.ui, pf) as data:
- if data.get('filename'):
- filepaths.append(p)
+ with open(p, 'rb') as pf:
+ earlybytes = pf.read(4096)
+ if b'\0' in earlybytes:
+ continue
+ pf.seek(0)
+ with hglib.extractpatch(self.repo.ui, pf) as data:
+ if data.get('filename'):
+ filepaths.append(p)
except OSError:
pass
return filepaths

Antonio Muci

unread,
May 25, 2026, 4:08:56 AMMay 25
to thg...@googlegroups.com, a....@inwind.it
# HG changeset patch
# User Antonio Muci <a....@inwind.it>
# Date 1779696270 -7200
# Mon May 25 10:04:30 2026 +0200
# Branch stable
# Node ID 9a3dd29b9a8c3e961528232dd04086a0c3ad82fd
# Parent d932cf48ea4f92f1173804e3de3b57ac9a7cd4cf
patchctx: use context manager in _files()

diff --git a/tortoisehg/util/patchctx.py b/tortoisehg/util/patchctx.py
--- a/tortoisehg/util/patchctx.py
+++ b/tortoisehg/util/patchctx.py
@@ -225,30 +225,28 @@ class patchctx:
return type, rawpath.split(b'/', 1)[-1]

files = {}
- pf = open(self._path, 'rb')
- try:
- # consume comments and headers
- for i in range(self._ph.diffstartline):
- pf.readline()
- for chunk in patch.parsepatch(pf):
- if not isinstance(chunk, patch.header):
- continue
- top = patch.parsefilename(chunk.header[-2])
- bot = patch.parsefilename(chunk.header[-1])
- type, path = get_path(top, bot)
- if path not in chunk.files():
- type, path = 0, chunk.files()[-1]
- if path not in files:
- self._status[type].append(path)
- files[path] = [chunk]
- self._fileorder.append(path)
- files[path].extend(chunk.hunks)
- except (patch.PatchError, AttributeError) as e:
- self._status[2].append(self._parseErrorFileName)
- files[self._parseErrorFileName] = []
- self._parseerror = e
- if 'THGDEBUG' in os.environ:
- print(hglib.exception_str(e))
- finally:
- pf.close()
+ with open(self._path, 'rb') as pf:
+ try:
+ # consume comments and headers
+ for i in range(self._ph.diffstartline):
+ pf.readline()
+ for chunk in patch.parsepatch(pf):
+ if not isinstance(chunk, patch.header):
+ continue
+ top = patch.parsefilename(chunk.header[-2])
+ bot = patch.parsefilename(chunk.header[-1])
+ type, path = get_path(top, bot)
+ if path not in chunk.files():
+ type, path = 0, chunk.files()[-1]
+ if path not in files:
+ self._status[type].append(path)
+ files[path] = [chunk]
+ self._fileorder.append(path)
+ files[path].extend(chunk.hunks)
+ except (patch.PatchError, AttributeError) as e:
+ self._status[2].append(self._parseErrorFileName)
+ files[self._parseErrorFileName] = []
+ self._parseerror = e
+ if 'THGDEBUG' in os.environ:
+ print(hglib.exception_str(e))
return files

Antonio Muci

unread,
May 25, 2026, 4:08:56 AMMay 25
to thg...@googlegroups.com, a....@inwind.it
# HG changeset patch
# User Antonio Muci <a....@inwind.it>
# Date 1779696229 -7200
# Mon May 25 10:03:49 2026 +0200
# Branch stable
# Node ID d932cf48ea4f92f1173804e3de3b57ac9a7cd4cf
# Parent 2a7ceff74ffd5ddc75b7af07064be1bc87608e7e
partialcommit: use context manager in partialcommit()

diff --git a/tortoisehg/util/partialcommit.py b/tortoisehg/util/partialcommit.py
--- a/tortoisehg/util/partialcommit.py
+++ b/tortoisehg/util/partialcommit.py
@@ -28,17 +28,15 @@ def partialcommit(orig, ui, repo, *pats,
# the wrapped workingfilectx methods will see this filestore and use
# the patched file data rather than the working copy data (for only the
# files modified by the patch)
- fp = open(patchfilename, 'rb')
- store = patch.filestore()
- try:
- # patch files in tmp directory
- patch.patchrepo(ui, repo, repo[b'.'], store, fp, 1, prefix=b'')
- store.keys = set(list(store.files.keys()) + list(store.data.keys()))
- repo._filestore = store
- except patch.PatchError as e:
- raise error.Abort(stringutil.forcebytestr(e))
- finally:
- fp.close()
+ with open(patchfilename, 'rb') as fp:
+ store = patch.filestore()
+ try:
+ # patch files in tmp directory
+ patch.patchrepo(ui, repo, repo[b'.'], store, fp, 1, prefix=b'')
+ store.keys = set(list(store.files.keys()) + list(store.data.keys()))
+ repo._filestore = store
+ except patch.PatchError as e:
+ raise error.Abort(stringutil.forcebytestr(e))

try:
ret = orig(ui, repo, *pats, **opts)

Antonio Muci

unread,
May 25, 2026, 4:08:56 AMMay 25
to thg...@googlegroups.com, a....@inwind.it
# HG changeset patch
# User Antonio Muci <a....@inwind.it>
# Date 1779696099 -7200
# Mon May 25 10:01:39 2026 +0200
# Branch stable
# Node ID 2a7ceff74ffd5ddc75b7af07064be1bc87608e7e
# Parent f436999a6fc93007cd3f147512975e538a1024da
run: use a context manager in get_lines_from_listfile()

diff --git a/tortoisehg/hgqt/run.py b/tortoisehg/hgqt/run.py
--- a/tortoisehg/hgqt/run.py
+++ b/tortoisehg/hgqt/run.py
@@ -234,9 +234,8 @@ def get_lines_from_listfile(filename, is
if filename == b'-':
lines = [x.replace(b"\n", b"") for x in procutil.stdin.readlines()]
else:
- fd = open(filename, "rb")
- lines = [ x.replace(b"\n", b"") for x in fd.readlines() ]
- fd.close()
+ with open(filename, "rb") as fd:
+ lines = [ x.replace(b"\n", b"") for x in fd.readlines() ]
os.unlink(filename)
if isutf8:
_linesutf8 = lines

Yuya Nishihara

unread,
May 25, 2026, 9:16:02 AMMay 25
to 'Antonio Muci' via TortoiseHg Developers, a....@inwind.it
On Mon, 25 May 2026 10:08:52 +0200, 'Antonio Muci' via TortoiseHg Developers wrote:
> # HG changeset patch
> # User Antonio Muci <a....@inwind.it>
> # Date 1779696063 -7200
> # Mon May 25 10:01:03 2026 +0200
> # Branch stable
> # Node ID f436999a6fc93007cd3f147512975e538a1024da
> # Parent 8301a77afa3194434aa909bf8876c5319ab33d00
> repowidget: use context manager in detectPatches() (the file was never closed)

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