More immediate resource closures

This commit is contained in:
Kovid Goyal 2023-10-16 13:34:17 +05:30
parent 691c4c3f7a
commit 6660bd828b
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 37 additions and 24 deletions

View File

@ -1570,10 +1570,12 @@ class DB:
return fmt_path return fmt_path
if not fmt: if not fmt:
return return
candidates = ()
with suppress(OSError):
candidates = os.scandir(path)
q = fmt.lower() q = fmt.lower()
try:
candidates = os.scandir(path)
except OSError:
return
with candidates:
for x in candidates: for x in candidates:
if x.name.endswith(q) and x.is_file(): if x.name.endswith(q) and x.is_file():
if not do_file_rename: if not do_file_rename:

View File

@ -1130,6 +1130,7 @@ class Cache:
if as_file: if as_file:
ret = SpooledTemporaryFile(SPOOL_SIZE) ret = SpooledTemporaryFile(SPOOL_SIZE)
if not self.copy_cover_to(book_id, ret): if not self.copy_cover_to(book_id, ret):
ret.close()
return return
ret.seek(0) ret.seek(0)
elif as_path: elif as_path:
@ -1379,6 +1380,7 @@ class Cache:
try: try:
self.copy_format_to(book_id, fmt, ret) self.copy_format_to(book_id, fmt, ret)
except NoSuchFormat: except NoSuchFormat:
ret.close()
return None return None
ret.seek(0) ret.seek(0)
# Various bits of code try to use the name as the default # Various bits of code try to use the name as the default
@ -1953,6 +1955,8 @@ class Cache:
name = None name = None
if name and not replace: if name and not replace:
if needs_close:
stream_or_path.close()
return False return False
if hasattr(stream_or_path, 'read'): if hasattr(stream_or_path, 'read'):
@ -1961,7 +1965,6 @@ class Cache:
stream = open(stream_or_path, 'rb') stream = open(stream_or_path, 'rb')
needs_close = True needs_close = True
try: try:
stream = stream_or_path if hasattr(stream_or_path, 'read') else open(stream_or_path, 'rb')
size, fname = self._do_add_format(book_id, fmt, stream, name) size, fname = self._do_add_format(book_id, fmt, stream, name)
finally: finally:
if needs_close: if needs_close:

View File

@ -370,6 +370,10 @@ class AddRemoveTest(BaseTest):
src_db = self.init_cache() src_db = self.init_cache()
dest_db = self.init_cache(self.cloned_library) dest_db = self.init_cache(self.cloned_library)
def read(x, mode='r'):
with open(x, mode) as f:
return f.read()
def a(**kw): def a(**kw):
ts = utcnow() ts = utcnow()
kw['timestamp'] = utcnow().isoformat() kw['timestamp'] = utcnow().isoformat()
@ -401,8 +405,8 @@ class AddRemoveTest(BaseTest):
def assert_has_extra_files(book_id): def assert_has_extra_files(book_id):
bookdir = os.path.dirname(dest_db.format_abspath(book_id, '__COVER_INTERNAL__')) bookdir = os.path.dirname(dest_db.format_abspath(book_id, '__COVER_INTERNAL__'))
self.assertEqual('exf', open(os.path.join(bookdir, 'exf')).read()) self.assertEqual('exf', read(os.path.join(bookdir, 'exf')))
self.assertEqual('recurse', open(os.path.join(bookdir, 'sub', 'recurse')).read()) self.assertEqual('recurse', read(os.path.join(bookdir, 'sub', 'recurse')))
def assert_does_not_have_extra_files(book_id): def assert_does_not_have_extra_files(book_id):
bookdir = os.path.dirname(dest_db.format_abspath(book_id, '__COVER_INTERNAL__')) bookdir = os.path.dirname(dest_db.format_abspath(book_id, '__COVER_INTERNAL__'))

View File

@ -270,7 +270,11 @@ class ReadingTest(BaseTest):
for book_id, cdata in iteritems(covers): for book_id, cdata in iteritems(covers):
self.assertEqual(cdata, cache.cover(book_id), 'Reading of cover failed') self.assertEqual(cdata, cache.cover(book_id), 'Reading of cover failed')
f = cache.cover(book_id, as_file=True) f = cache.cover(book_id, as_file=True)
try:
self.assertEqual(cdata, f.read() if f else f, 'Reading of cover as file failed') self.assertEqual(cdata, f.read() if f else f, 'Reading of cover as file failed')
finally:
if f:
f.close()
if cdata: if cdata:
with open(cache.cover(book_id, as_path=True), 'rb') as f: with open(cache.cover(book_id, as_path=True), 'rb') as f:
self.assertEqual(cdata, f.read(), 'Reading of cover as path failed') self.assertEqual(cdata, f.read(), 'Reading of cover as path failed')
@ -441,7 +445,7 @@ class ReadingTest(BaseTest):
old = formats[book_id][fmt] old = formats[book_id][fmt]
self.assertEqual(old, cache.format(book_id, fmt), self.assertEqual(old, cache.format(book_id, fmt),
'Old and new format disagree') 'Old and new format disagree')
f = cache.format(book_id, fmt, as_file=True) with cache.format(book_id, fmt, as_file=True) as f:
self.assertEqual(old, f.read(), self.assertEqual(old, f.read(),
'Failed to read format as file') 'Failed to read format as file')
with open(cache.format(book_id, fmt, as_path=True, with open(cache.format(book_id, fmt, as_path=True,