From fc157167f4b298ba5feee879638bb2fae45dd790 Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Wed, 27 Mar 2024 09:05:41 +0800 Subject: [PATCH] fix: close local files after read Signed-off-by: Frost Ming --- src/unearth/fetchers/sync.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/unearth/fetchers/sync.py b/src/unearth/fetchers/sync.py index ba7f691..ab50928 100644 --- a/src/unearth/fetchers/sync.py +++ b/src/unearth/fetchers/sync.py @@ -26,6 +26,11 @@ def is_absolute_url(self) -> bool: httpx.URL.is_absolute_url = property(is_absolute_url) +class FileByteStream(IteratorByteStream): + def close(self) -> None: + self._stream.close() # type: ignore[attr-defined] + + class LocalFSTransport(httpx.BaseTransport): def handle_request(self, request: httpx.Request) -> httpx.Response: link = Link(str(request.url)) @@ -50,7 +55,7 @@ def handle_request(self, request: httpx.Request) -> httpx.Response: return httpx.Response( status_code=200, headers=headers, - stream=IteratorByteStream(path.open("rb")), + stream=FileByteStream(path.open("rb")), )