[PATCH 5 of 5] ui: use bytes IO and convert EOL manually in ui.editor()

Yuya Nishihara yuya at tcha.org
Wed Mar 29 10:46:41 EDT 2017


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1490791418 -32400
#      Wed Mar 29 21:43:38 2017 +0900
# Node ID 81aa3738f703e4354b0f853e87548a139b129bdd
# Parent  c43fe15a4a4bec542b7411087a1ad0233cb5614f
ui: use bytes IO and convert EOL manually in ui.editor()

Text IO sucks on Python 3 as it must be a unicode stream. We could introduce
a wrapper that converts unicode back to bytes, but it wouldn't be simple to
handle offsets transparently from/to underlying IOBase API.

Fortunately, we don't need to process huge text files, so let's stick to
bytes IO and convert EOL in memory.

diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -1232,11 +1232,11 @@ class ui(object):
         if self.configbool('experimental', 'editortmpinhg'):
             rdir = repopath
         (fd, name) = tempfile.mkstemp(prefix='hg-' + extra['prefix'] + '-',
-                                      suffix=extra['suffix'], text=True,
+                                      suffix=extra['suffix'],
                                       dir=rdir)
         try:
-            f = os.fdopen(fd, pycompat.sysstr("w"))
-            f.write(encoding.strfromlocal(text))
+            f = os.fdopen(fd, r'wb')
+            f.write(util.tonativeeol(text))
             f.close()
 
             environ = {'HGUSER': user}
@@ -1258,8 +1258,8 @@ class ui(object):
                         onerr=error.Abort, errprefix=_("edit failed"),
                         blockedtag='editor')
 
-            f = open(name)
-            t = encoding.strtolocal(f.read())
+            f = open(name, r'rb')
+            t = util.fromnativeeol(f.read())
             f.close()
         finally:
             os.unlink(name)


More information about the Mercurial-devel mailing list