From 60555c13894cec318da725ee542a4355c83baa57 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 26 Aug 2024 16:01:53 +0530 Subject: [PATCH] Fix unclosed resource warnings in fetch backend tests --- src/calibre/scraper/qt.py | 6 ++++++ src/calibre/scraper/test_fetch_backend.py | 8 ++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/calibre/scraper/qt.py b/src/calibre/scraper/qt.py index 02878dbed9..233fc3227c 100644 --- a/src/calibre/scraper/qt.py +++ b/src/calibre/scraper/qt.py @@ -102,6 +102,12 @@ class FakeResponse: def close(self): self._data.close() + def __enter__(self): + return self + + def __exit__(self, *a): + self._data.close() + def shutdown_browser(bref): br = bref() diff --git a/src/calibre/scraper/test_fetch_backend.py b/src/calibre/scraper/test_fetch_backend.py index 4cc1080d48..7d2e659fb7 100644 --- a/src/calibre/scraper/test_fetch_backend.py +++ b/src/calibre/scraper/test_fetch_backend.py @@ -109,10 +109,10 @@ class TestFetchBackend(unittest.TestCase): req = Request(url, headers=headers) else: req = url - res = br.open(req, data=data, timeout=timeout) - raw = res.read() - ans = json.loads(raw) - ans['final_url'] = res.geturl() + with br.open(req, data=data, timeout=timeout) as res: + raw = res.read() + ans = json.loads(raw) + ans['final_url'] = res.geturl() return ans def test_with_timeout(no_response=True):