From 373033a285faa6b21b67ff8d4fbf9f22352002a0 Mon Sep 17 00:00:00 2001 From: Micah Jerome Ellison Date: Thu, 9 Jan 2020 20:34:38 -0800 Subject: [PATCH] #790 - closing temp file before passing it to editor to prevent file locking issues in Windows --- jrnl/util.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/jrnl/util.py b/jrnl/util.py index 5918b14b..7af8d6c4 100644 --- a/jrnl/util.py +++ b/jrnl/util.py @@ -142,21 +142,26 @@ def scope_config(config, journal_name): def get_text_from_editor(config, template=""): filehandle, tmpfile = tempfile.mkstemp(prefix="jrnl", text=True, suffix=".txt") + os.close(filehandle) + with open(tmpfile, "w", encoding="utf-8") as f: if template: f.write(template) + try: subprocess.call( shlex.split(config["editor"], posix="win" not in sys.platform) + [tmpfile] ) except AttributeError: subprocess.call(config["editor"] + [tmpfile]) + with open(tmpfile, "r", encoding="utf-8") as f: raw = f.read() - os.close(filehandle) os.remove(tmpfile) + if not raw: print("[Nothing saved to file]", file=sys.stderr) + return raw