From 9320e2fe2207dc7dea7ac27c8d0829c5d8f57ed0 Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Mon, 9 Sep 2019 17:43:28 -0400 Subject: [PATCH] where possible, open files using context managers --- src/calibre/library/catalogs/epub_mobi_builder.py | 3 ++- src/calibre/library/database2.py | 7 +++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/calibre/library/catalogs/epub_mobi_builder.py b/src/calibre/library/catalogs/epub_mobi_builder.py index e6e4ebfb22..b629718bcf 100644 --- a/src/calibre/library/catalogs/epub_mobi_builder.py +++ b/src/calibre/library/catalogs/epub_mobi_builder.py @@ -2961,7 +2961,8 @@ class CatalogBuilder(object): left = max(int((MI_WIDTH - width) / 2.), 0) top = max(int((MI_HEIGHT - height) / 2.), 0) draw.text((left, top), text, fill=(0, 0, 0), font=font) - img.save(open(out_path, 'wb'), 'GIF') + with open(out_path, 'wb') as f: + img.save(f, 'GIF') def generate_ncx_header(self): """ Generate the basic NCX file. diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index af0639ed4b..1b1fbfa1f0 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -3517,10 +3517,9 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): self.set_metadata(id, mi, commit=True, ignore_errors=True) npath = self.run_import_plugins(path, format) format = os.path.splitext(npath)[-1].lower().replace('.', '').upper() - stream = lopen(npath, 'rb') - format = check_ebook_format(stream, format) - self.add_format(id, format, stream, index_is_id=True) - stream.close() + with lopen(npath, 'rb') as stream: + format = check_ebook_format(stream, format) + self.add_format(id, format, stream, index_is_id=True) postimport.append((id, format)) self.conn.commit() self.data.refresh_ids(self, ids) # Needed to update format list and size