summaryrefslogtreecommitdiff
path: root/alot
diff options
context:
space:
mode:
authorDylan Baker <dylan@pnwbakers.com>2016-12-14 12:21:35 -0800
committerDylan Baker <dylan@pnwbakers.com>2016-12-14 15:43:39 -0800
commit8be904b96144e71458851d6e156dd4bae4398aa3 (patch)
tree55d3f6f59f5992f86558d03c5944b746b94e8d3b /alot
parent15a724dfff40ec3b35af2526584e81ff82b4c40b (diff)
use open as a context manager instead of oneline open().read()
While open().read() is nice for its terseness it has the problem that it causes an fd to leak until the gc collects it. For short lived scripts this isn't a big deal, but for a program like alot that run for long periods of time it's better to be correct and ensure that the fd is closed.
Diffstat (limited to 'alot')
-rw-r--r--alot/commands/globals.py6
-rw-r--r--alot/helper.py3
2 files changed, 6 insertions, 3 deletions
diff --git a/alot/commands/globals.py b/alot/commands/globals.py
index 7d5c1d3e..437b3644 100644
--- a/alot/commands/globals.py
+++ b/alot/commands/globals.py
@@ -733,7 +733,8 @@ class ComposeCommand(Command):
priority='error')
return
try:
- self.envelope.parse_template(open(path).read())
+ with open(path) as f:
+ self.envelope.parse_template(f.read())
except Exception as e:
ui.notify(str(e), priority='error')
return
@@ -792,7 +793,8 @@ class ComposeCommand(Command):
self.envelope.attach(sig, filename=name)
logging.debug('attached')
else:
- sigcontent = open(sig).read()
+ with open(sig) as f:
+ sigcontent = f.read()
enc = helper.guess_encoding(sigcontent)
mimetype = helper.guess_mimetype(sigcontent)
if mimetype.startswith('text'):
diff --git a/alot/helper.py b/alot/helper.py
index f968306d..ba68bd45 100644
--- a/alot/helper.py
+++ b/alot/helper.py
@@ -465,7 +465,8 @@ def libmagic_version_at_least(version):
# TODO: make this work on blobs, not paths
def mimewrap(path, filename=None, ctype=None):
- content = open(path, 'rb').read()
+ with open(path, 'rb') as f:
+ content = f.read()
if not ctype:
ctype = guess_mimetype(content)
# libmagic < 5.12 incorrectly detects excel/powerpoint files as