Patchwork [5,of,8] ui: do not try readline support if fin/fout aren't standard streams

login
register
mail settings
Submitter Yuya Nishihara
Date March 9, 2018, 12:35 p.m.
Message ID <63a13b91e1ab4d9fa0a7.1520598938@mimosa>
Download mbox | patch
Permalink /patch/29165/
State Accepted
Headers show

Comments

Yuya Nishihara - March 9, 2018, 12:35 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1520325146 21600
#      Tue Mar 06 02:32:26 2018 -0600
# Node ID 63a13b91e1ab4d9fa0a713935be58794b9cadab5
# Parent  10e3ea3d8b1dc671f6302ebed4489773c7d79458
ui: do not try readline support if fin/fout aren't standard streams

It's unlikely for a non-stdio stream to be a tty. Minimizing readline support
makes it much simpler to work around the unicode input() function of Python 3.

This also works on chg which duplicates client's tty to stdio fds.

Patch

diff --git a/mercurial/ui.py b/mercurial/ui.py
--- a/mercurial/ui.py
+++ b/mercurial/ui.py
@@ -1264,7 +1264,9 @@  class ui(object):
         return i
 
     def _readline(self):
-        if self._isatty(self.fin):
+        usereadline = (self._isatty(self.fin) and self._isatty(self.fout)
+                       and util.isstdin(self.fin) and util.isstdout(self.fout))
+        if usereadline:
             try:
                 # magically add command line editing support, where
                 # available
@@ -1273,7 +1275,7 @@  class ui(object):
                 readline.read_history_file
                 # windows sometimes raises something other than ImportError
             except Exception:
-                pass
+                usereadline = False
 
         # prompt ' ' must exist; otherwise readline may delete entire line
         # - http://bugs.python.org/issue12833