diff --git a/src/calibre/db/cache.py b/src/calibre/db/cache.py index d0fa2403b9..ff4476bc6a 100644 --- a/src/calibre/db/cache.py +++ b/src/calibre/db/cache.py @@ -1528,6 +1528,7 @@ class Cache(object): :param run_hooks: If True, file type plugins are run on the format before and after being added. :param dbapi: Internal use only. ''' + needs_close = False if run_hooks: # Run import plugins, the write lock is not held to cater for # broken plugins that might spin the event loop by popping up a @@ -1535,6 +1536,7 @@ class Cache(object): npath = run_import_plugins(stream_or_path, fmt) fmt = os.path.splitext(npath)[-1].lower().replace('.', '').upper() stream_or_path = lopen(npath, 'rb') + needs_close = True fmt = check_ebook_format(stream_or_path, fmt) with self.write_lock: @@ -1550,8 +1552,17 @@ class Cache(object): if name and not replace: return False - stream = stream_or_path if hasattr(stream_or_path, 'read') else lopen(stream_or_path, 'rb') - size, fname = self._do_add_format(book_id, fmt, stream, name) + if hasattr(stream_or_path, 'read'): + stream = stream_or_path + else: + stream = lopen(stream_or_path, 'rb') + needs_close = True + try: + stream = stream_or_path if hasattr(stream_or_path, 'read') else lopen(stream_or_path, 'rb') + size, fname = self._do_add_format(book_id, fmt, stream, name) + finally: + if needs_close: + stream.close() del stream max_size = self.fields['formats'].table.update_fmt(book_id, fmt, fname, size, self.backend) diff --git a/src/calibre/devices/udisks.py b/src/calibre/devices/udisks.py index 0847b002bd..d0ad9e1985 100644 --- a/src/calibre/devices/udisks.py +++ b/src/calibre/devices/udisks.py @@ -20,10 +20,11 @@ def node_mountpoint(node): return raw.replace(b'\\040', b' ').replace(b'\\011', b'\t').replace(b'\\012', b'\n').replace(b'\\0134', b'\\').decode('utf-8') - for line in open('/proc/mounts', 'rb').readlines(): - line = line.split() - if line[0] == node: - return de_mangle(line[1]) + with open('/proc/mounts', 'rb') as src: + for line in src.readlines(): + line = line.split() + if line[0] == node: + return de_mangle(line[1]) return None diff --git a/src/calibre/devices/usbms/device.py b/src/calibre/devices/usbms/device.py index fae4edc465..41d908f07c 100644 --- a/src/calibre/devices/usbms/device.py +++ b/src/calibre/devices/usbms/device.py @@ -456,6 +456,12 @@ class Device(DeviceConfig, DevicePlugin): q = getattr(detected_device, attr) return q == val + def getnum(usb_dir): + def rc(q): + with open(j(usb_dir, q), 'rb') as f: + return raw2num(f.read().decode('utf-8')) + return rc + for x, isfile in walk('/sys/devices'): if isfile and x.endswith('idVendor'): usb_dir = d(x) @@ -465,8 +471,7 @@ class Device(DeviceConfig, DevicePlugin): break if usb_dir is None: continue - e = lambda q : raw2num(open(j(usb_dir, q), 'rb').read().decode('utf-8')) - ven, prod, bcd = map(e, ('idVendor', 'idProduct', 'bcdDevice')) + ven, prod, bcd = map(getnum(usb_dir), ('idVendor', 'idProduct', 'bcdDevice')) if not (test(ven, 'idVendor') and test(prod, 'idProduct') and test(bcd, 'bcdDevice')): usb_dir = None @@ -487,7 +492,8 @@ class Device(DeviceConfig, DevicePlugin): sz = j(x, 'size') node = parts[idx+1] try: - exists = int(open(sz, 'rb').read().decode('utf-8')) > 0 + with open(sz, 'rb') as szf: + exists = szf.read().decode('utf-8') > 0 if exists: node = self.find_largest_partition(x) ok[node] = True