From 772512aee09e21be5e2a7e62c7b3a52d8ab8d163 Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Fri, 24 Apr 2026 13:58:19 +0200 Subject: [PATCH] FileContext.open() / close(): Only call wrapped once FileContext's _open() and _close() are called everytime their wrapper is called, which tasks the caller with keeping track of whether they were already called or not. Be a little easier on the caller, keep track in an open count, and call _open() only once for multiple calls to open(), and close() likewise. The caller still needs to make sure the number of open() and close() calls matches. Signed-off-by: Jan Lindemann --- src/python/jw/pkg/lib/FileContext.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/python/jw/pkg/lib/FileContext.py b/src/python/jw/pkg/lib/FileContext.py index 8d104e80..0a952b4f 100644 --- a/src/python/jw/pkg/lib/FileContext.py +++ b/src/python/jw/pkg/lib/FileContext.py @@ -37,6 +37,7 @@ class FileContext(abc.ABC): self.__log_name: str|None = None self.__in_pipe = in_pipe self.__out_pipe = out_pipe + self.__open_count = 0 assert verbose_default is not None async def __aenter__(self): @@ -75,13 +76,18 @@ class FileContext(abc.ABC): pass async def open(self) -> None: - await self._open() + self.__open_count += 1 + if self.__open_count == 1: + await self._open() async def _close(self) -> None: pass async def close(self) -> None: - await self._close() + if self.__open_count == 1: + await self._close() + self.__open_count -= 1 + assert self.__open_count >= 0 @classmethod def schema_from_uri(cls, uri: str) -> str: