From cff4d491ea15e1ea9541813df99cf961b4d4a682 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 23 Jun 2021 09:07:39 +0530 Subject: [PATCH] Close a bunch of file objects explicitly --- src/calibre/ebooks/oeb/polish/container.py | 6 ++++-- src/calibre/spell/dictionary.py | 6 ++++-- src/calibre/utils/imghdr.py | 12 +++++++++++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/calibre/ebooks/oeb/polish/container.py b/src/calibre/ebooks/oeb/polish/container.py index 9ec4b7704f..753e83d018 100644 --- a/src/calibre/ebooks/oeb/polish/container.py +++ b/src/calibre/ebooks/oeb/polish/container.py @@ -611,7 +611,8 @@ class Container(ContainerBase): # {{{ :param decode: If True and the file has a text based MIME type, decode it and return a unicode object instead of raw bytes. :param normalize_to_nfc: If True the returned unicode object is normalized to the NFC normal form as is required for the EPUB and AZW3 file formats. ''' - ans = self.open(name).read() + with self.open(name) as nf: + ans = nf.read() mime = self.mime_map.get(name, guess_type(name)) if decode and (mime in OEB_STYLES or mime in OEB_DOCS or mime == 'text/plain' or mime[-4:] in {'+xml', '/xml'}): ans = self.decode(ans, normalize_to_nfc=normalize_to_nfc) @@ -1205,7 +1206,8 @@ class EpubContainer(Container): container_path = join(self.root, 'META-INF', 'container.xml') if not exists(container_path): raise InvalidEpub('No META-INF/container.xml in epub') - container = safe_xml_fromstring(open(container_path, 'rb').read()) + with open(container_path, 'rb') as cf: + container = safe_xml_fromstring(cf.read()) opf_files = container.xpath(( r'child::ocf:rootfiles/ocf:rootfile' '[@media-type="%s" and @full-path]'%guess_type('a.opf') diff --git a/src/calibre/spell/dictionary.py b/src/calibre/spell/dictionary.py index 3e0d236c4d..4f450c2715 100644 --- a/src/calibre/spell/dictionary.py +++ b/src/calibre/spell/dictionary.py @@ -52,7 +52,8 @@ def builtin_dictionaries(): if _builtins is None: dics = [] for lc in glob.glob(os.path.join(P('dictionaries', allow_user_override=False), '*/locales')): - locales = list(filter(None, open(lc, 'rb').read().decode('utf-8').splitlines())) + with open(lc, 'rb') as lcf: + locales = list(filter(None, lcf.read().decode('utf-8').splitlines())) locale = locales[0] base = os.path.dirname(lc) dics.append(Dictionary( @@ -67,7 +68,8 @@ def custom_dictionaries(reread=False): if _custom is None or reread: dics = [] for lc in glob.glob(os.path.join(config_dir, 'dictionaries', '*/locales')): - locales = list(filter(None, open(lc, 'rb').read().decode('utf-8').splitlines())) + with open(lc, 'rb') as cdf: + locales = list(filter(None, cdf.read().decode('utf-8').splitlines())) try: name, locale, locales = locales[0], locales[1], locales[1:] except IndexError: diff --git a/src/calibre/utils/imghdr.py b/src/calibre/utils/imghdr.py index f7bc9b0dc1..2b806a60a9 100644 --- a/src/calibre/utils/imghdr.py +++ b/src/calibre/utils/imghdr.py @@ -40,14 +40,24 @@ def identify(src): ''' Recognize file format and sizes. Returns format, width, height. width and height will be -1 if not found and fmt will be None if the image is not recognized. ''' - width = height = -1 + needs_close = False if isinstance(src, unicode_type): stream = lopen(src, 'rb') + needs_close = True elif isinstance(src, bytes): stream = ReadOnlyFileBuffer(src) else: stream = src + try: + return _identify(stream) + finally: + if needs_close: + stream.close() + + +def _identify(stream): + width = height = -1 pos = stream.tell() head = stream.read(HSIZE)