changeset 1984:df7436f439a0

Improved ui.edit(): - Use descriptive name for temporary file: hg-editor-*.txt - Don't import tempfile in the method, but use demandload() - Remove tempfile file even if editor aborts.
author Thomas Arendsen Hein <thomas@intevation.de>
date Tue, 21 Mar 2006 12:54:32 +0100
parents ae12a81549a7
children c577689006fa
files mercurial/ui.py
diffstat 1 files changed, 19 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/ui.py	Tue Mar 21 12:45:27 2006 +0100
+++ b/mercurial/ui.py	Tue Mar 21 12:54:32 2006 +0100
@@ -8,7 +8,7 @@
 import ConfigParser
 from i18n import gettext as _
 from demandload import *
-demandload(globals(), "os re socket sys util")
+demandload(globals(), "os re socket sys util tempfile")
 
 class ui(object):
     def __init__(self, verbose=False, debug=False, quiet=False,
@@ -195,23 +195,25 @@
     def debug(self, *msg):
         if self.debugflag: self.write(*msg)
     def edit(self, text, user):
-        import tempfile
-        (fd, name) = tempfile.mkstemp("hg")
-        f = os.fdopen(fd, "w")
-        f.write(text)
-        f.close()
+        (fd, name) = tempfile.mkstemp(prefix="hg-editor-", suffix=".txt")
+        try:
+            f = os.fdopen(fd, "w")
+            f.write(text)
+            f.close()
+
+            editor = (os.environ.get("HGEDITOR") or
+                    self.config("ui", "editor") or
+                    os.environ.get("EDITOR", "vi"))
 
-        editor = (os.environ.get("HGEDITOR") or
-                  self.config("ui", "editor") or
-                  os.environ.get("EDITOR", "vi"))
+            util.system("%s \"%s\"" % (editor, name),
+                        environ={'HGUSER': user},
+                        onerr=util.Abort, errprefix=_("edit failed"))
 
-        util.system("%s \"%s\"" % (editor, name),
-                    environ={'HGUSER': user},
-                    onerr=util.Abort, errprefix=_("edit failed"))
-
-        t = open(name).read()
-        t = re.sub("(?m)^HG:.*\n", "", t)
-
-        os.unlink(name)
+            f = open(name)
+            t = f.read()
+            f.close()
+            t = re.sub("(?m)^HG:.*\n", "", t)
+        finally:
+            os.unlink(name)
 
         return t