From 324a1e1aed7847cd5d054c8507a3d12484fb1260 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 16 Oct 2023 12:25:57 +0530 Subject: [PATCH] More explicit resource closage --- src/calibre/db/cache.py | 19 +++++++++---------- src/calibre/db/tests/filesystem.py | 8 ++++++-- src/calibre/utils/exim.py | 1 + 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/calibre/db/cache.py b/src/calibre/db/cache.py index 314dbebea2..db670318fc 100644 --- a/src/calibre/db/cache.py +++ b/src/calibre/db/cache.py @@ -3480,18 +3480,17 @@ def import_library(library_key, importer, library_path, progress=None, abort=Non cache._update_path((book_id,), mark_as_dirtied=False) for fmt, fmtkey in iteritems(fmt_key_map): if fmt == '.cover': - stream = importer.start_file(fmtkey, _('Cover for %s') % title) - path = cache._field_for('path', book_id).replace('/', os.sep) - cache.backend.set_cover(book_id, path, stream, no_processing=True) + with closing(importer.start_file(fmtkey, _('Cover for %s') % title)) as stream: + path = cache._field_for('path', book_id).replace('/', os.sep) + cache.backend.set_cover(book_id, path, stream, no_processing=True) else: - stream = importer.start_file(fmtkey, _('{0} format for {1}').format(fmt.upper(), title)) - size, fname = cache._do_add_format(book_id, fmt, stream, mtime=stream.mtime) - cache.fields['formats'].table.update_fmt(book_id, fmt, fname, size, cache.backend) - stream.close() + with closing(importer.start_file(fmtkey, _('{0} format for {1}').format(fmt.upper(), title))) as stream: + size, fname = cache._do_add_format(book_id, fmt, stream, mtime=stream.mtime) + cache.fields['formats'].table.update_fmt(book_id, fmt, fname, size, cache.backend) for relpath, efkey in extra_files.get(book_id, {}).items(): - stream = importer.start_file(efkey, _('Extra file {0} for book {1}').format(relpath, title)) - path = cache._field_for('path', book_id).replace('/', os.sep) - cache.backend.add_extra_file(relpath, stream, path) + with closing(importer.start_file(efkey, _('Extra file {0} for book {1}').format(relpath, title))) as stream: + path = cache._field_for('path', book_id).replace('/', os.sep) + cache.backend.add_extra_file(relpath, stream, path) cache.dump_metadata({book_id}) if progress is not None: progress(_('Completed'), total, total) diff --git a/src/calibre/db/tests/filesystem.py b/src/calibre/db/tests/filesystem.py index 476404168b..eb4784d97f 100644 --- a/src/calibre/db/tests/filesystem.py +++ b/src/calibre/db/tests/filesystem.py @@ -239,6 +239,10 @@ class FilesystemTest(BaseTest): def test_export_import(self): from calibre.db.cache import import_library from calibre.utils.exim import Exporter, Importer + def read(x, mode='r'): + with open(x, mode) as f: + return f.read() + cache = self.init_cache() bookdir = os.path.dirname(cache.format_abspath(1, '__COVER_INTERNAL__')) with open(os.path.join(bookdir, 'exf'), 'w') as f: @@ -262,8 +266,8 @@ class FilesystemTest(BaseTest): self.assertEqual(cache.format(book_id, fmt), ic.format(book_id, fmt)) self.assertEqual(cache.format_metadata(book_id, fmt)['mtime'], cache.format_metadata(book_id, fmt)['mtime']) bookdir = os.path.dirname(ic.format_abspath(1, '__COVER_INTERNAL__')) - self.assertEqual('exf', open(os.path.join(bookdir, 'exf')).read()) - self.assertEqual('recurse', open(os.path.join(bookdir, 'sub', 'recurse')).read()) + self.assertEqual('exf', read(os.path.join(bookdir, 'exf'))) + self.assertEqual('recurse', read(os.path.join(bookdir, 'sub', 'recurse'))) r1 = cache.add_notes_resource(b'res1', 'res.jpg', mtime=time.time()-113) r2 = cache.add_notes_resource(b'res2', 'res.jpg', mtime=time.time()-1115) cache.set_notes_for('authors', 2, 'some notes', resource_hashes=(r1, r2)) diff --git a/src/calibre/utils/exim.py b/src/calibre/utils/exim.py index 3415a58229..21413045cf 100644 --- a/src/calibre/utils/exim.py +++ b/src/calibre/utils/exim.py @@ -256,6 +256,7 @@ class FileSource: def close(self): if self.check_hash and self.hasher.hexdigest() != self.digest: self.importer.corrupted_files.append(self.description) + self.f.close() self.hasher = self.f = None