diff --git a/src/calibre/customize/ui.py b/src/calibre/customize/ui.py index 4b2aa18a2b..59e8968c9d 100644 --- a/src/calibre/customize/ui.py +++ b/src/calibre/customize/ui.py @@ -3,6 +3,7 @@ __license__ = 'GPL v3' __copyright__ = '2008, Kovid Goyal ' import os, shutil, traceback, functools, sys, re +from contextlib import closing from calibre.customize import Plugin, FileTypePlugin, MetadataReaderPlugin, \ MetadataWriterPlugin @@ -51,25 +52,25 @@ def load_plugin(path_to_zip_file): #print 'Loading plugin from', path_to_zip_file if not os.access(path_to_zip_file, os.R_OK): raise PluginNotFound - zf = ZipFile(path_to_zip_file) - for name in zf.namelist(): - if name.lower().endswith('plugin.py'): - locals = {} - raw = zf.read(name) - match = re.search(r'coding[:=]\s*([-\w.]+)', raw[:300]) - encoding = 'utf-8' - if match is not None: - encoding = match.group(1) - raw = raw.decode(encoding) - raw = re.sub('\r\n', '\n', raw) - exec raw in locals - for x in locals.values(): - if isinstance(x, type) and issubclass(x, Plugin): - if x.minimum_calibre_version > version or \ - platform not in x.supported_platforms: - continue + with closing(ZipFile(path_to_zip_file)) as zf: + for name in zf.namelist(): + if name.lower().endswith('plugin.py'): + locals = {} + raw = zf.read(name) + match = re.search(r'coding[:=]\s*([-\w.]+)', raw[:300]) + encoding = 'utf-8' + if match is not None: + encoding = match.group(1) + raw = raw.decode(encoding) + raw = re.sub('\r\n', '\n', raw) + exec raw in locals + for x in locals.values(): + if isinstance(x, type) and issubclass(x, Plugin): + if x.minimum_calibre_version > version or \ + platform not in x.supported_platforms: + continue - return x + return x raise InvalidPlugin(_('No valid plugin found in ')+path_to_zip_file)