f63228
f63228
# HG changeset patch
f63228
# User Victor Stinner <victor.stinner@gmail.com>
f63228
# Date 1406197344 -7200
f63228
# Node ID 0177d8a4e82a613de0c64e747656c1d0b63e49b3
f63228
# Parent  e70ab72286b470b7209b91d3aa8a21953aafb78f
f63228
Issue #19884: readline: Disable the meta modifier key if stdout is not a
f63228
terminal to not write the ANSI sequence "\033[1034h" into stdout. This sequence
f63228
is used on some terminal (ex: TERM=xterm-256color") to enable support of 8 bit
f63228
characters.
f63228
f63228
diff --git a/Lib/test/test_readline.py b/Lib/test/test_readline.py
f63228
--- a/Lib/test/test_readline.py
f63228
+++ b/Lib/test/test_readline.py
f63228
@@ -1,17 +1,19 @@
f63228
 """
f63228
 Very minimal unittests for parts of the readline module.
f63228
-
f63228
-These tests were added to check that the libedit emulation on OSX and
f63228
-the "real" readline have the same interface for history manipulation. That's
f63228
-why the tests cover only a small subset of the interface.
f63228
 """
f63228
+import os
f63228
 import unittest
f63228
 from test.test_support import run_unittest, import_module
f63228
+from test.script_helper import assert_python_ok
f63228
 
f63228
 # Skip tests if there is no readline module
f63228
 readline = import_module('readline')
f63228
 
f63228
 class TestHistoryManipulation (unittest.TestCase):
f63228
+    """These tests were added to check that the libedit emulation on OSX and
f63228
+    the "real" readline have the same interface for history manipulation.
f63228
+    That's why the tests cover only a small subset of the interface.
f63228
+    """
f63228
 
f63228
     @unittest.skipIf(not hasattr(readline, 'clear_history'),
f63228
                      "The history update test cannot be run because the "
f63228
@@ -40,8 +42,18 @@ class TestHistoryManipulation (unittest.
f63228
         self.assertEqual(readline.get_current_history_length(), 1)
f63228
 
f63228
 
f63228
+class TestReadline(unittest.TestCase):
f63228
+    def test_init(self):
f63228
+        # Issue #19884: Ensure that the ANSI sequence "\033[1034h" is not
f63228
+        # written into stdout when the readline module is imported and stdout
f63228
+        # is redirected to a pipe.
f63228
+        rc, stdout, stderr = assert_python_ok('-c', 'import readline',
f63228
+                                              TERM='xterm-256color')
f63228
+        self.assertEqual(stdout, b'')
f63228
+
f63228
+
f63228
 def test_main():
f63228
-    run_unittest(TestHistoryManipulation)
f63228
+    run_unittest(TestHistoryManipulation, TestReadline)
f63228
 
f63228
 if __name__ == "__main__":
f63228
     test_main()
f63228
diff --git a/Modules/readline.c b/Modules/readline.c
f63228
--- a/Modules/readline.c
f63228
+++ b/Modules/readline.c
f63228
@@ -887,7 +887,7 @@ setup_readline(void)
f63228
 #endif
f63228
 
f63228
 #ifdef __APPLE__
f63228
-    /* the libedit readline emulation resets key bindings etc 
f63228
+    /* the libedit readline emulation resets key bindings etc
f63228
      * when calling rl_initialize.  So call it upfront
f63228
      */
f63228
     if (using_libedit_emulation)
f63228
@@ -932,6 +932,17 @@ setup_readline(void)
f63228
 
f63228
     begidx = PyInt_FromLong(0L);
f63228
     endidx = PyInt_FromLong(0L);
f63228
+
f63228
+    if (!isatty(STDOUT_FILENO)) {
f63228
+        /* Issue #19884: stdout is no a terminal. Disable meta modifier
f63228
+           keys to not write the ANSI sequence "\033[1034h" into stdout. On
f63228
+           terminals supporting 8 bit characters like TERM=xterm-256color
f63228
+           (which is now the default Fedora since Fedora 18), the meta key is
f63228
+           used to enable support of 8 bit characters (ANSI sequence
f63228
+           "\033[1034h"). */
f63228
+        rl_variable_bind ("enable-meta-key", "off");
f63228
+    }
f63228
+
f63228
     /* Initialize (allows .inputrc to override)
f63228
      *
f63228
      * XXX: A bug in the readline-2.2 library causes a memory leak
f63228
@@ -943,7 +954,7 @@ setup_readline(void)
f63228
     else
f63228
 #endif /* __APPLE__ */
f63228
         rl_initialize();
f63228
-    
f63228
+
f63228
     RESTORE_LOCALE(saved_locale)
f63228
 }
f63228
 
f63228
f63228
f63228
# HG changeset patch
f63228
# User Victor Stinner <victor.stinner@gmail.com>
f63228
# Date 1406232681 -7200
f63228
# Node ID f0ab6f9f06036dfacff09f22f86464840b50eb0a
f63228
# Parent  d422062d7d366386acdb81851b0f2ec3a6f6750c
f63228
Issue #19884, readline: calling rl_variable_bind ("enable-meta-key", "off")
f63228
does crash on Mac OS X which uses libedit instead of readline.
f63228
f63228
diff --git a/Modules/readline.c b/Modules/readline.c
f63228
--- a/Modules/readline.c
f63228
+++ b/Modules/readline.c
f63228
@@ -933,15 +933,19 @@ setup_readline(void)
f63228
     begidx = PyInt_FromLong(0L);
f63228
     endidx = PyInt_FromLong(0L);
f63228
 
f63228
+#ifndef __APPLE__
f63228
     if (!isatty(STDOUT_FILENO)) {
f63228
         /* Issue #19884: stdout is no a terminal. Disable meta modifier
f63228
            keys to not write the ANSI sequence "\033[1034h" into stdout. On
f63228
            terminals supporting 8 bit characters like TERM=xterm-256color
f63228
            (which is now the default Fedora since Fedora 18), the meta key is
f63228
            used to enable support of 8 bit characters (ANSI sequence
f63228
-           "\033[1034h"). */
f63228
+           "\033[1034h").
f63228
+
f63228
+           With libedit, this call makes readline() crash. */
f63228
         rl_variable_bind ("enable-meta-key", "off");
f63228
     }
f63228
+#endif
f63228
 
f63228
     /* Initialize (allows .inputrc to override)
f63228
      *
f63228
f63228
f63228
# HG changeset patch
f63228
# User Antoine Pitrou <solipsis@pitrou.net>
f63228
# Date 1415109130 -3600
f63228
# Node ID eba6e68e818c694e499dfc4b22dde095d2557ab1
f63228
# Parent  e54d0b197c8245bd29ea09f421e2f1da47370f41
f63228
Issue #22773: fix failing test with old readline versions due to issue #19884.
f63228
f63228
diff --git a/Lib/test/test_readline.py b/Lib/test/test_readline.py
f63228
--- a/Lib/test/test_readline.py
f63228
+++ b/Lib/test/test_readline.py
f63228
@@ -43,6 +43,10 @@ class TestHistoryManipulation (unittest.
f63228
 
f63228
 
f63228
 class TestReadline(unittest.TestCase):
f63228
+
f63228
+    @unittest.skipIf(readline._READLINE_VERSION < 0x0600
f63228
+                     and "libedit" not in readline.__doc__,
f63228
+                     "not supported in this library version")
f63228
     def test_init(self):
f63228
         # Issue #19884: Ensure that the ANSI sequence "\033[1034h" is not
f63228
         # written into stdout when the readline module is imported and stdout
f63228
diff --git a/Modules/readline.c b/Modules/readline.c
f63228
--- a/Modules/readline.c
f63228
+++ b/Modules/readline.c
f63228
@@ -1184,4 +1184,7 @@ initreadline(void)
f63228
 
f63228
     PyOS_ReadlineFunctionPointer = call_readline;
f63228
     setup_readline();
f63228
+
f63228
+    PyModule_AddIntConstant(m, "_READLINE_VERSION", RL_READLINE_VERSION);
f63228
+    PyModule_AddIntConstant(m, "_READLINE_RUNTIME_VERSION", rl_readline_version);
f63228
 }
f63228