commit 462e41f95d6c1dd3641d4da39f443fecca42cd56
parent a1ab124a3fc38e0b36dfa1f7ea30346374fbfa38
Author: Samuel Walladge <samuel@swalladge.id.au>
Date: Wed, 1 Jun 2016 14:54:23 +0930
fix encoding errors when loading contents of tempfile
Diffstat:
2 files changed, 12 insertions(+), 5 deletions(-)
diff --git a/simplenote_cli/sncli.py b/simplenote_cli/sncli.py
@@ -101,7 +101,7 @@ def exec_cmd_on_note(self, note, cmd=None, raw=False):
content = None
if not raw:
- content = ''.join(temp.tempfile_content(tf))
+ content = temp.tempfile_content(tf)
if not content or content == '\n':
content = None
diff --git a/simplenote_cli/temp.py b/simplenote_cli/temp.py
@@ -26,6 +26,7 @@ def tempfile_create(note, raw=False):
def tempfile_delete(tf):
if tf:
+ tf.close()
os.unlink(tf.name)
def tempfile_name(tf):
@@ -34,7 +35,13 @@ def tempfile_name(tf):
return ''
def tempfile_content(tf):
- # This seems like a hack. When editing with Gedit, tf file contents weren't getting
- updated_tf_contents = open(tf.name, 'r').read()
- tf.write(updated_tf_contents.encode('utf-8'))
- return updated_tf_contents
+ # This 'hack' is needed because some editors use an intermediate temporary
+ # file, and rename it to that of the correct file, overwriting it. This
+ # means that the tf file handle won't be updated with the new contents, and
+ # the tempfile must be re-opened and read
+ if not tf:
+ return None
+
+ with open(tf.name, 'rb') as f:
+ updated_tf_contents = f.read()
+ return updated_tf_contents.decode('utf-8')