From 2de9e40eee05b4b487a4c9feded2545a99d2f29b Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 17 Oct 2023 06:10:28 +0530 Subject: [PATCH] more immediate resource closures --- setup/unix-ci.py | 6 +++++- src/calibre/db/tests/filesystem.py | 17 +++++++++-------- .../ebooks/oeb/polish/tests/container.py | 6 ++++-- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/setup/unix-ci.py b/setup/unix-ci.py index 020cb762d3..6040677fde 100644 --- a/setup/unix-ci.py +++ b/setup/unix-ci.py @@ -63,11 +63,15 @@ def run(*args): if len(args) == 1: args = shlex.split(args[0]) print(' '.join(args), flush=True) + p = subprocess.Popen(args) try: - ret = subprocess.Popen(args).wait(timeout=600) + ret = p.wait(timeout=600) except subprocess.TimeoutExpired as err: + ret = 1 print(err, file=sys.stderr, flush=True) print('Timed out running:', ' '.join(args), flush=True, file=sys.stderr) + p.kill() + if ret != 0: raise SystemExit(ret) diff --git a/src/calibre/db/tests/filesystem.py b/src/calibre/db/tests/filesystem.py index eb4784d97f..cea9c5d795 100644 --- a/src/calibre/db/tests/filesystem.py +++ b/src/calibre/db/tests/filesystem.py @@ -13,6 +13,11 @@ from calibre.db.tests.base import BaseTest from calibre.ptempfile import TemporaryDirectory +def read(x, mode='r'): + with open(x, mode) as f: + return f.read() + + class FilesystemTest(BaseTest): def get_filesystem_data(self, cache, book_id): @@ -82,8 +87,8 @@ class FilesystemTest(BaseTest): def side_data(book_id=1): bookdir = os.path.dirname(cache.format_abspath(book_id, '__COVER_INTERNAL__')) return { - 'a.side': open(os.path.join(bookdir, 'a.side')).read(), - 'a.fmt1': open(os.path.join(bookdir, 'subdir', 'a.fmt1')).read(), + 'a.side': read(os.path.join(bookdir, 'a.side')), + 'a.fmt1': read(os.path.join(bookdir, 'subdir', 'a.fmt1')), } def check_that_filesystem_and_db_entries_match(book_id): @@ -183,8 +188,8 @@ class FilesystemTest(BaseTest): wam.delete_originals() self.assertEqual([], os.listdir(tdir1)) self.assertEqual({'a', 'b'}, set(os.listdir(tdir2))) - self.assertEqual(raw, open(os.path.join(tdir2, 'a'), 'rb').read()) - self.assertEqual(raw, open(os.path.join(tdir2, 'b'), 'rb').read()) + self.assertEqual(raw, read(os.path.join(tdir2, 'a'), 'rb')) + self.assertEqual(raw, read(os.path.join(tdir2, 'b'), 'rb')) def test_library_move(self): ' Test moving of library ' @@ -239,10 +244,6 @@ 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: diff --git a/src/calibre/ebooks/oeb/polish/tests/container.py b/src/calibre/ebooks/oeb/polish/tests/container.py index 9e284421c2..7f4ed12946 100644 --- a/src/calibre/ebooks/oeb/polish/tests/container.py +++ b/src/calibre/ebooks/oeb/polish/tests/container.py @@ -59,7 +59,8 @@ class ContainerTests(BaseTest): c2.commit_item(text) for c in (c1, c2): 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] with c1.open(name, mode='r+b') as f: @@ -67,7 +68,8 @@ class ContainerTests(BaseTest): f.write(b' ') for c in (c1, c2): 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 for c in (c1, c2):