commit-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Commit-gnuradio] [gnuradio] 01/03: grc: fix file monitor on windows (#1


From: git
Subject: [Commit-gnuradio] [gnuradio] 01/03: grc: fix file monitor on windows (#1169)
Date: Wed, 25 Jan 2017 18:27:22 +0000 (UTC)

This is an automated email from the git hooks/post-receive script.

jcorgan pushed a commit to branch maint
in repository gnuradio.

commit 0002525abeff6f80e7deb35849f2fc47ce9982a4
Author: Sebastian Koslowski <address@hidden>
Date:   Tue Jan 17 09:45:36 2017 +0100

    grc: fix file monitor on windows (#1169)
    
    I seems tempfile.NamedTemporaryFile() can not be used under windows, as 
keeping the fp around locks the file for writing. So, we close the the handle 
after the initial write and delete the file manually.
---
 grc/gui/external_editor.py | 28 +++++++++++++---------------
 1 file changed, 13 insertions(+), 15 deletions(-)

diff --git a/grc/gui/external_editor.py b/grc/gui/external_editor.py
index 76f2141..010bd71 100644
--- a/grc/gui/external_editor.py
+++ b/grc/gui/external_editor.py
@@ -34,18 +34,14 @@ class ExternalEditor(threading.Thread):
 
         self.editor = editor
         self.callback = callback
-        self.tempfile = self._create_tempfile(name, value)
+        self.filename = self._create_tempfile(name, value)
 
     def _create_tempfile(self, name, value):
-        fp = tempfile.NamedTemporaryFile(mode='w', suffix='.py',
-                                         prefix=name + '_')
-        fp.write(value)
-        fp.flush()
-        return fp
-
-    @property
-    def filename(self):
-        return self.tempfile.name
+        with tempfile.NamedTemporaryFile(
+            mode='wb', prefix=name + '_', suffix='.py', delete=False,
+        ) as fp:
+            fp.write(value.encode('utf-8'))
+            return fp.name
 
     def open_editor(self):
         proc = subprocess.Popen(args=(self.editor, self.filename))
@@ -65,16 +61,18 @@ class ExternalEditor(threading.Thread):
                 if mtime > last_change:
                     # print "file monitor: reload trigger for", filename
                     last_change = mtime
-                    with open(filename) as fp:
-                        data = fp.read()
+                    with open(filename, 'rb') as fp:
+                        data = fp.read().decode('utf-8')
                     self.callback(data)
                 time.sleep(1)
 
         except Exception as e:
             print >> sys.stderr, "file monitor crashed:", str(e)
-        else:
-            # print "file monitor: done with", filename
-            pass
+        finally:
+            try:
+                os.remove(self.filename)
+            except OSError:
+                pass
 
 
 if __name__ == '__main__':



reply via email to

[Prev in Thread] Current Thread [Next in Thread]