Commit 515cfe23 authored by Bram Schoenmakers's avatar Bram Schoenmakers Committed by GitHub

Merge pull request #143 from mruwek/properly-close-and-save-tempfile

Close temporary file after saving to it
parents a7c495bf fc265004
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import os import os
import codecs
import tempfile import tempfile
from subprocess import CalledProcessError, check_call from subprocess import CalledProcessError, check_call
...@@ -54,16 +55,16 @@ class EditCommand(MultiCommand): ...@@ -54,16 +55,16 @@ class EditCommand(MultiCommand):
self.multi_mode = False self.multi_mode = False
def _todos_to_temp(self): def _todos_to_temp(self):
f = tempfile.NamedTemporaryFile() f = tempfile.NamedTemporaryFile(delete=False)
for todo in self.todos: for todo in self.todos:
f.write((todo.source() + "\n").encode('utf-8')) f.write((todo.source() + "\n").encode('utf-8'))
f.seek(0) f.close()
return f return f
def _todos_from_temp(self, p_temp_file): def _todos_from_temp(self, p_temp_file):
p_temp_file.seek(0) f = codecs.open(p_temp_file.name, encoding='utf-8')
todos = p_temp_file.read().decode('utf-8').splitlines() todos = f.read().splitlines()
todo_objs = [] todo_objs = []
for todo in todos: for todo in todos:
...@@ -121,6 +122,8 @@ class EditCommand(MultiCommand): ...@@ -121,6 +122,8 @@ class EditCommand(MultiCommand):
else: else:
self.error(self.usage()) self.error(self.usage())
os.unlink(temp_todos.name)
def _execute_not_multi(self): def _execute_not_multi(self):
if self.edit_archive: if self.edit_archive:
archive = config().archive() archive = config().archive()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment