summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_readline.py
diff options
context:
space:
mode:
authorLarry Hastings <larry@hastings.org>2016-06-27 02:53:18 (GMT)
committerLarry Hastings <larry@hastings.org>2016-06-27 02:53:18 (GMT)
commit1b329e791ae3a3a2989f05e8c2019b67b4e1a7df (patch)
tree91e137c00f35f21a2c17b385f9746492b8347558 /Lib/test/test_readline.py
parent9bb200545970bb920c83124658cb89c7d295166d (diff)
parent1e957d145fa1fc05ca1fbb9f135ab162c939ae14 (diff)
downloadcpython-1b329e791ae3a3a2989f05e8c2019b67b4e1a7df.zip
cpython-1b329e791ae3a3a2989f05e8c2019b67b4e1a7df.tar.gz
cpython-1b329e791ae3a3a2989f05e8c2019b67b4e1a7df.tar.bz2
Merge.
Diffstat (limited to 'Lib/test/test_readline.py')
-rw-r--r--Lib/test/test_readline.py161
1 files changed, 155 insertions, 6 deletions
diff --git a/Lib/test/test_readline.py b/Lib/test/test_readline.py
index 35330ab..8c2ad85 100644
--- a/Lib/test/test_readline.py
+++ b/Lib/test/test_readline.py
@@ -1,15 +1,25 @@
"""
Very minimal unittests for parts of the readline module.
"""
+from contextlib import ExitStack
+from errno import EIO
import os
+import selectors
+import subprocess
+import sys
import tempfile
import unittest
-from test.support import import_module, unlink
+from test.support import import_module, unlink, TESTFN
from test.support.script_helper import assert_python_ok
# Skip tests if there is no readline module
readline = import_module('readline')
+is_editline = readline.__doc__ and "libedit" in readline.__doc__
+
+@unittest.skipUnless(hasattr(readline, "clear_history"),
+ "The history update test cannot be run because the "
+ "clear_history method is not available.")
class TestHistoryManipulation (unittest.TestCase):
"""
These tests were added to check that the libedit emulation on OSX and the
@@ -17,9 +27,6 @@ class TestHistoryManipulation (unittest.TestCase):
why the tests cover only a small subset of the interface.
"""
- @unittest.skipUnless(hasattr(readline, "clear_history"),
- "The history update test cannot be run because the "
- "clear_history method is not available.")
def testHistoryUpdates(self):
readline.clear_history()
@@ -82,11 +89,29 @@ class TestHistoryManipulation (unittest.TestCase):
# write_history_file can create the target
readline.write_history_file(hfilename)
+ def test_nonascii_history(self):
+ readline.clear_history()
+ try:
+ readline.add_history("entrée 1")
+ except UnicodeEncodeError as err:
+ self.skipTest("Locale cannot encode test data: " + format(err))
+ readline.add_history("entrée 2")
+ readline.replace_history_item(1, "entrée 22")
+ readline.write_history_file(TESTFN)
+ self.addCleanup(os.remove, TESTFN)
+ readline.clear_history()
+ readline.read_history_file(TESTFN)
+ if is_editline:
+ # An add_history() call seems to be required for get_history_
+ # item() to register items from the file
+ readline.add_history("dummy")
+ self.assertEqual(readline.get_history_item(1), "entrée 1")
+ self.assertEqual(readline.get_history_item(2), "entrée 22")
+
class TestReadline(unittest.TestCase):
- @unittest.skipIf(readline._READLINE_VERSION < 0x0600
- and "libedit" not in readline.__doc__,
+ @unittest.skipIf(readline._READLINE_VERSION < 0x0600 and not is_editline,
"not supported in this library version")
def test_init(self):
# Issue #19884: Ensure that the ANSI sequence "\033[1034h" is not
@@ -96,6 +121,130 @@ class TestReadline(unittest.TestCase):
TERM='xterm-256color')
self.assertEqual(stdout, b'')
+ def test_nonascii(self):
+ try:
+ readline.add_history("\xEB\xEF")
+ except UnicodeEncodeError as err:
+ self.skipTest("Locale cannot encode test data: " + format(err))
+
+ script = r"""import readline
+
+is_editline = readline.__doc__ and "libedit" in readline.__doc__
+inserted = "[\xEFnserted]"
+macro = "|t\xEB[after]"
+set_pre_input_hook = getattr(readline, "set_pre_input_hook", None)
+if is_editline or not set_pre_input_hook:
+ # The insert_line() call via pre_input_hook() does nothing with Editline,
+ # so include the extra text that would have been inserted here
+ macro = inserted + macro
+
+if is_editline:
+ readline.parse_and_bind(r'bind ^B ed-prev-char')
+ readline.parse_and_bind(r'bind "\t" rl_complete')
+ readline.parse_and_bind(r'bind -s ^A "{}"'.format(macro))
+else:
+ readline.parse_and_bind(r'Control-b: backward-char')
+ readline.parse_and_bind(r'"\t": complete')
+ readline.parse_and_bind(r'set disable-completion off')
+ readline.parse_and_bind(r'set show-all-if-ambiguous off')
+ readline.parse_and_bind(r'set show-all-if-unmodified off')
+ readline.parse_and_bind(r'Control-a: "{}"'.format(macro))
+
+def pre_input_hook():
+ readline.insert_text(inserted)
+ readline.redisplay()
+if set_pre_input_hook:
+ set_pre_input_hook(pre_input_hook)
+
+def completer(text, state):
+ if text == "t\xEB":
+ if state == 0:
+ print("text", ascii(text))
+ print("line", ascii(readline.get_line_buffer()))
+ print("indexes", readline.get_begidx(), readline.get_endidx())
+ return "t\xEBnt"
+ if state == 1:
+ return "t\xEBxt"
+ if text == "t\xEBx" and state == 0:
+ return "t\xEBxt"
+ return None
+readline.set_completer(completer)
+
+def display(substitution, matches, longest_match_length):
+ print("substitution", ascii(substitution))
+ print("matches", ascii(matches))
+readline.set_completion_display_matches_hook(display)
+
+print("result", ascii(input()))
+print("history", ascii(readline.get_history_item(1)))
+"""
+
+ input = b"\x01" # Ctrl-A, expands to "|t\xEB[after]"
+ input += b"\x02" * len("[after]") # Move cursor back
+ input += b"\t\t" # Display possible completions
+ input += b"x\t" # Complete "t\xEBx" -> "t\xEBxt"
+ input += b"\r"
+ output = run_pty(script, input)
+ self.assertIn(b"text 't\\xeb'\r\n", output)
+ self.assertIn(b"line '[\\xefnserted]|t\\xeb[after]'\r\n", output)
+ self.assertIn(b"indexes 11 13\r\n", output)
+ if not is_editline and hasattr(readline, "set_pre_input_hook"):
+ self.assertIn(b"substitution 't\\xeb'\r\n", output)
+ self.assertIn(b"matches ['t\\xebnt', 't\\xebxt']\r\n", output)
+ expected = br"'[\xefnserted]|t\xebxt[after]'"
+ self.assertIn(b"result " + expected + b"\r\n", output)
+ self.assertIn(b"history " + expected + b"\r\n", output)
+
+
+def run_pty(script, input=b"dummy input\r"):
+ pty = import_module('pty')
+ output = bytearray()
+ [master, slave] = pty.openpty()
+ args = (sys.executable, '-c', script)
+ proc = subprocess.Popen(args, stdin=slave, stdout=slave, stderr=slave)
+ os.close(slave)
+ with ExitStack() as cleanup:
+ cleanup.enter_context(proc)
+ def terminate(proc):
+ try:
+ proc.terminate()
+ except ProcessLookupError:
+ # Workaround for Open/Net BSD bug (Issue 16762)
+ pass
+ cleanup.callback(terminate, proc)
+ cleanup.callback(os.close, master)
+ # Avoid using DefaultSelector and PollSelector. Kqueue() does not
+ # work with pseudo-terminals on OS X < 10.9 (Issue 20365) and Open
+ # BSD (Issue 20667). Poll() does not work with OS X 10.6 or 10.4
+ # either (Issue 20472). Hopefully the file descriptor is low enough
+ # to use with select().
+ sel = cleanup.enter_context(selectors.SelectSelector())
+ sel.register(master, selectors.EVENT_READ | selectors.EVENT_WRITE)
+ os.set_blocking(master, False)
+ while True:
+ for [_, events] in sel.select():
+ if events & selectors.EVENT_READ:
+ try:
+ chunk = os.read(master, 0x10000)
+ except OSError as err:
+ # Linux raises EIO when slave is closed (Issue 5380)
+ if err.errno != EIO:
+ raise
+ chunk = b""
+ if not chunk:
+ return output
+ output.extend(chunk)
+ if events & selectors.EVENT_WRITE:
+ try:
+ input = input[os.write(master, input):]
+ except OSError as err:
+ # Apparently EIO means the slave was closed
+ if err.errno != EIO:
+ raise
+ input = b"" # Stop writing
+ if not input:
+ sel.modify(master, selectors.EVENT_READ)
+
if __name__ == "__main__":
unittest.main()