From 669e408cc6fe4f696eabda17e251636e7aa5a7bb Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 29 Dec 2025 15:30:33 +0530 Subject: [PATCH] Ensure various Cache objects used in tests are properly closed rather than relying on their __del__ method --- src/calibre/db/tests/base.py | 5 +++++ src/calibre/srv/tests/base.py | 6 +++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/calibre/db/tests/base.py b/src/calibre/db/tests/base.py index a5d7a7c424..cdbb25ec4a 100644 --- a/src/calibre/db/tests/base.py +++ b/src/calibre/db/tests/base.py @@ -30,12 +30,16 @@ class BaseTest(unittest.TestCase): from calibre.utils.recycle_bin import nuke_recycle nuke_recycle() self.paths_to_remove = [] + self.objects_to_close = [] self.library_path = self.mkdtemp() self.create_db(self.library_path) def tearDown(self): from calibre.utils.recycle_bin import restore_recyle restore_recyle() + for x in self.objects_to_close: + x.close() + self.objects_to_close = [] gc.collect(), gc.collect() for x in self.paths_to_remove: try: @@ -71,6 +75,7 @@ class BaseTest(unittest.TestCase): backend = DB(library_path or self.library_path) cache = Cache(backend) cache.init() + self.objects_to_close.append(cache) return cache def mkdtemp(self): diff --git a/src/calibre/srv/tests/base.py b/src/calibre/srv/tests/base.py index 7c651e65ec..1a7e12f59d 100644 --- a/src/calibre/srv/tests/base.py +++ b/src/calibre/srv/tests/base.py @@ -58,11 +58,15 @@ class LibraryBaseTest(BaseTest): from calibre.utils.recycle_bin import nuke_recycle nuke_recycle() self.library_path = self.mkdtemp() + self.objects_to_close = [] self.create_db(self.library_path) def tearDown(self): from calibre.utils.recycle_bin import restore_recyle restore_recyle() + for x in self.objects_to_close: + x.close() + self.objects_to_close = [] gc.collect(), gc.collect() try: shutil.rmtree(self.library_path) @@ -92,7 +96,7 @@ class LibraryBaseTest(BaseTest): db.add_format(1, 'EPUB', src, run_hooks=False) db.add_format(1, 'FMT2', BytesIO(b'book1fmt2'), run_hooks=False) db.add_format(2, 'FMT1', BytesIO(b'book2fmt1'), run_hooks=False) - db.backend.conn.close() + db.close() return dest def create_server(self, *args, **kwargs):