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 <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-04-24 13:58:19 +02:00
commit 772512aee0
Signed by: Jan Lindemann
GPG key ID: 3750640C9E25DD61

View file

@ -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:
self.__open_count += 1
if self.__open_count == 1:
await self._open()
async def _close(self) -> None:
pass
async def close(self) -> None:
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: