more immediate resource closures

This commit is contained in:
Kovid Goyal 2023-10-17 06:10:28 +05:30
parent 1d28aa1d6a
commit 2de9e40eee
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 18 additions and 11 deletions

View File

@ -63,11 +63,15 @@ def run(*args):
if len(args) == 1: if len(args) == 1:
args = shlex.split(args[0]) args = shlex.split(args[0])
print(' '.join(args), flush=True) print(' '.join(args), flush=True)
p = subprocess.Popen(args)
try: try:
ret = subprocess.Popen(args).wait(timeout=600) ret = p.wait(timeout=600)
except subprocess.TimeoutExpired as err: except subprocess.TimeoutExpired as err:
ret = 1
print(err, file=sys.stderr, flush=True) print(err, file=sys.stderr, flush=True)
print('Timed out running:', ' '.join(args), flush=True, file=sys.stderr) print('Timed out running:', ' '.join(args), flush=True, file=sys.stderr)
p.kill()
if ret != 0: if ret != 0:
raise SystemExit(ret) raise SystemExit(ret)

View File

@ -13,6 +13,11 @@ from calibre.db.tests.base import BaseTest
from calibre.ptempfile import TemporaryDirectory from calibre.ptempfile import TemporaryDirectory
def read(x, mode='r'):
with open(x, mode) as f:
return f.read()
class FilesystemTest(BaseTest): class FilesystemTest(BaseTest):
def get_filesystem_data(self, cache, book_id): def get_filesystem_data(self, cache, book_id):
@ -82,8 +87,8 @@ class FilesystemTest(BaseTest):
def side_data(book_id=1): def side_data(book_id=1):
bookdir = os.path.dirname(cache.format_abspath(book_id, '__COVER_INTERNAL__')) bookdir = os.path.dirname(cache.format_abspath(book_id, '__COVER_INTERNAL__'))
return { return {
'a.side': open(os.path.join(bookdir, 'a.side')).read(), 'a.side': read(os.path.join(bookdir, 'a.side')),
'a.fmt1': open(os.path.join(bookdir, 'subdir', 'a.fmt1')).read(), 'a.fmt1': read(os.path.join(bookdir, 'subdir', 'a.fmt1')),
} }
def check_that_filesystem_and_db_entries_match(book_id): def check_that_filesystem_and_db_entries_match(book_id):
@ -183,8 +188,8 @@ class FilesystemTest(BaseTest):
wam.delete_originals() wam.delete_originals()
self.assertEqual([], os.listdir(tdir1)) self.assertEqual([], os.listdir(tdir1))
self.assertEqual({'a', 'b'}, set(os.listdir(tdir2))) self.assertEqual({'a', 'b'}, set(os.listdir(tdir2)))
self.assertEqual(raw, open(os.path.join(tdir2, 'a'), 'rb').read()) self.assertEqual(raw, read(os.path.join(tdir2, 'a'), 'rb'))
self.assertEqual(raw, open(os.path.join(tdir2, 'b'), 'rb').read()) self.assertEqual(raw, read(os.path.join(tdir2, 'b'), 'rb'))
def test_library_move(self): def test_library_move(self):
' Test moving of library ' ' Test moving of library '
@ -239,10 +244,6 @@ class FilesystemTest(BaseTest):
def test_export_import(self): def test_export_import(self):
from calibre.db.cache import import_library from calibre.db.cache import import_library
from calibre.utils.exim import Exporter, Importer 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() cache = self.init_cache()
bookdir = os.path.dirname(cache.format_abspath(1, '__COVER_INTERNAL__')) bookdir = os.path.dirname(cache.format_abspath(1, '__COVER_INTERNAL__'))
with open(os.path.join(bookdir, 'exf'), 'w') as f: with open(os.path.join(bookdir, 'exf'), 'w') as f:

View File

@ -59,7 +59,8 @@ class ContainerTests(BaseTest):
c2.commit_item(text) c2.commit_item(text)
for c in (c1, c2): for c in (c1, c2):
self.assertEqual(1, nlinks_file(c.name_path_map[text])) self.assertEqual(1, nlinks_file(c.name_path_map[text]))
self.assertNotEqual(c1.open(text).read(), c2.open(text).read()) with c1.open(text) as c1f, c2.open(text) as c2f:
self.assertNotEqual(c1f.read(), c2f.read())
name = spine_names[1] name = spine_names[1]
with c1.open(name, mode='r+b') as f: with c1.open(name, mode='r+b') as f:
@ -67,7 +68,8 @@ class ContainerTests(BaseTest):
f.write(b' ') f.write(b' ')
for c in (c1, c2): for c in (c1, c2):
self.assertEqual(1, nlinks_file(c.name_path_map[name])) self.assertEqual(1, nlinks_file(c.name_path_map[name]))
self.assertNotEqual(c1.open(name).read(), c2.open(name).read()) with c1.open(text) as c1f, c2.open(text) as c2f:
self.assertNotEqual(c1f.read(), c2f.read())
x = base + 'out.' + fmt x = base + 'out.' + fmt
for c in (c1, c2): for c in (c1, c2):