| 
									
										
										
										
											2009-09-20 14:53:22 +00:00
										 |  |  | """
 | 
					
						
							|  |  |  | Very minimal unittests for parts of the readline module. | 
					
						
							|  |  |  | """
 | 
					
						
							| 
									
										
										
										
											2016-05-15 03:05:36 +00:00
										 |  |  | from contextlib import ExitStack | 
					
						
							| 
									
										
										
										
											2016-05-15 01:26:25 +00:00
										 |  |  | from errno import EIO | 
					
						
							| 
									
										
										
										
											2014-07-24 12:23:56 +02:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2016-05-15 01:26:25 +00:00
										 |  |  | import selectors | 
					
						
							|  |  |  | import subprocess | 
					
						
							|  |  |  | import sys | 
					
						
							| 
									
										
										
										
											2014-11-26 13:58:16 -06:00
										 |  |  | import tempfile | 
					
						
							| 
									
										
										
										
											2009-09-20 14:53:22 +00:00
										 |  |  | import unittest | 
					
						
							| 
									
										
										
										
											2016-06-14 01:16:16 +00:00
										 |  |  | from test.support import import_module, unlink, TESTFN | 
					
						
							| 
									
										
										
										
											2015-05-06 06:33:17 +03:00
										 |  |  | from test.support.script_helper import assert_python_ok | 
					
						
							| 
									
										
										
										
											2009-09-20 14:53:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-26 12:01:06 +00:00
										 |  |  | # Skip tests if there is no readline module | 
					
						
							|  |  |  | readline = import_module('readline') | 
					
						
							| 
									
										
										
										
											2009-09-20 14:53:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-14 05:45:31 +00:00
										 |  |  | is_editline = readline.__doc__ and "libedit" in readline.__doc__ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-14 01:16:16 +00:00
										 |  |  | @unittest.skipUnless(hasattr(readline, "clear_history"), | 
					
						
							|  |  |  |                      "The history update test cannot be run because the " | 
					
						
							|  |  |  |                      "clear_history method is not available.") | 
					
						
							| 
									
										
										
										
											2009-09-20 14:53:22 +00:00
										 |  |  | class TestHistoryManipulation (unittest.TestCase): | 
					
						
							| 
									
										
										
										
											2014-07-24 12:23:56 +02:00
										 |  |  |     """
 | 
					
						
							|  |  |  |     These tests were added to check that the libedit emulation on OSX and the | 
					
						
							|  |  |  |     "real" readline have the same interface for history manipulation. That's | 
					
						
							|  |  |  |     why the tests cover only a small subset of the interface. | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2011-03-14 17:10:22 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-20 14:53:22 +00:00
										 |  |  |     def testHistoryUpdates(self): | 
					
						
							| 
									
										
										
										
											2012-08-11 11:02:14 +02:00
										 |  |  |         readline.clear_history() | 
					
						
							| 
									
										
										
										
											2009-09-20 14:53:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         readline.add_history("first line") | 
					
						
							|  |  |  |         readline.add_history("second line") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertEqual(readline.get_history_item(0), None) | 
					
						
							|  |  |  |         self.assertEqual(readline.get_history_item(1), "first line") | 
					
						
							|  |  |  |         self.assertEqual(readline.get_history_item(2), "second line") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         readline.replace_history_item(0, "replaced line") | 
					
						
							|  |  |  |         self.assertEqual(readline.get_history_item(0), None) | 
					
						
							|  |  |  |         self.assertEqual(readline.get_history_item(1), "replaced line") | 
					
						
							|  |  |  |         self.assertEqual(readline.get_history_item(2), "second line") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertEqual(readline.get_current_history_length(), 2) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         readline.remove_history_item(0) | 
					
						
							|  |  |  |         self.assertEqual(readline.get_history_item(0), None) | 
					
						
							|  |  |  |         self.assertEqual(readline.get_history_item(1), "second line") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertEqual(readline.get_current_history_length(), 1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-26 13:02:33 -08:00
										 |  |  |     @unittest.skipUnless(hasattr(readline, "append_history_file"), | 
					
						
							| 
									
										
										
										
											2014-11-26 14:35:12 -06:00
										 |  |  |                          "append_history not available") | 
					
						
							| 
									
										
										
										
											2014-11-26 13:58:16 -06:00
										 |  |  |     def test_write_read_append(self): | 
					
						
							|  |  |  |         hfile = tempfile.NamedTemporaryFile(delete=False) | 
					
						
							|  |  |  |         hfile.close() | 
					
						
							|  |  |  |         hfilename = hfile.name | 
					
						
							|  |  |  |         self.addCleanup(unlink, hfilename) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # test write-clear-read == nop | 
					
						
							|  |  |  |         readline.clear_history() | 
					
						
							|  |  |  |         readline.add_history("first line") | 
					
						
							|  |  |  |         readline.add_history("second line") | 
					
						
							|  |  |  |         readline.write_history_file(hfilename) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         readline.clear_history() | 
					
						
							|  |  |  |         self.assertEqual(readline.get_current_history_length(), 0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         readline.read_history_file(hfilename) | 
					
						
							|  |  |  |         self.assertEqual(readline.get_current_history_length(), 2) | 
					
						
							|  |  |  |         self.assertEqual(readline.get_history_item(1), "first line") | 
					
						
							|  |  |  |         self.assertEqual(readline.get_history_item(2), "second line") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # test append | 
					
						
							|  |  |  |         readline.append_history_file(1, hfilename) | 
					
						
							|  |  |  |         readline.clear_history() | 
					
						
							|  |  |  |         readline.read_history_file(hfilename) | 
					
						
							|  |  |  |         self.assertEqual(readline.get_current_history_length(), 3) | 
					
						
							|  |  |  |         self.assertEqual(readline.get_history_item(1), "first line") | 
					
						
							|  |  |  |         self.assertEqual(readline.get_history_item(2), "second line") | 
					
						
							|  |  |  |         self.assertEqual(readline.get_history_item(3), "second line") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # test 'no such file' behaviour | 
					
						
							|  |  |  |         os.unlink(hfilename) | 
					
						
							|  |  |  |         with self.assertRaises(FileNotFoundError): | 
					
						
							|  |  |  |             readline.append_history_file(1, hfilename) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # write_history_file can create the target | 
					
						
							|  |  |  |         readline.write_history_file(hfilename) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-14 01:16:16 +00:00
										 |  |  |     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) | 
					
						
							| 
									
										
										
										
											2016-06-14 05:45:31 +00:00
										 |  |  |         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") | 
					
						
							| 
									
										
										
										
											2016-06-14 01:16:16 +00:00
										 |  |  |         self.assertEqual(readline.get_history_item(1), "entrée 1") | 
					
						
							|  |  |  |         self.assertEqual(readline.get_history_item(2), "entrée 22") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-20 14:53:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-24 12:23:56 +02:00
										 |  |  | class TestReadline(unittest.TestCase): | 
					
						
							| 
									
										
										
										
											2014-11-04 14:52:10 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-27 03:23:11 +00:00
										 |  |  |     @unittest.skipIf(readline._READLINE_VERSION < 0x0601 and not is_editline, | 
					
						
							| 
									
										
										
										
											2014-11-04 14:52:10 +01:00
										 |  |  |                      "not supported in this library version") | 
					
						
							| 
									
										
										
										
											2014-07-24 12:23:56 +02:00
										 |  |  |     def test_init(self): | 
					
						
							|  |  |  |         # Issue #19884: Ensure that the ANSI sequence "\033[1034h" is not | 
					
						
							|  |  |  |         # written into stdout when the readline module is imported and stdout | 
					
						
							|  |  |  |         # is redirected to a pipe. | 
					
						
							|  |  |  |         rc, stdout, stderr = assert_python_ok('-c', 'import readline', | 
					
						
							|  |  |  |                                               TERM='xterm-256color') | 
					
						
							|  |  |  |         self.assertEqual(stdout, b'') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-15 01:26:25 +00:00
										 |  |  |     auto_history_script = """\
 | 
					
						
							|  |  |  | import readline | 
					
						
							|  |  |  | readline.set_auto_history({}) | 
					
						
							|  |  |  | input() | 
					
						
							|  |  |  | print("History length:", readline.get_current_history_length()) | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_auto_history_enabled(self): | 
					
						
							|  |  |  |         output = run_pty(self.auto_history_script.format(True)) | 
					
						
							|  |  |  |         self.assertIn(b"History length: 1\r\n", output) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_auto_history_disabled(self): | 
					
						
							|  |  |  |         output = run_pty(self.auto_history_script.format(False)) | 
					
						
							|  |  |  |         self.assertIn(b"History length: 0\r\n", output) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-14 01:16:16 +00:00
										 |  |  |     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
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-14 08:45:43 +00:00
										 |  |  | 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: | 
					
						
							| 
									
										
										
										
											2016-06-14 05:45:31 +00:00
										 |  |  |     # The insert_line() call via pre_input_hook() does nothing with Editline, | 
					
						
							|  |  |  |     # so include the extra text that would have been inserted here | 
					
						
							| 
									
										
										
										
											2016-06-14 08:45:43 +00:00
										 |  |  |     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)) | 
					
						
							| 
									
										
										
										
											2016-06-14 01:16:16 +00:00
										 |  |  | 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') | 
					
						
							| 
									
										
										
										
											2016-06-14 08:45:43 +00:00
										 |  |  |     readline.parse_and_bind(r'Control-a: "{}"'.format(macro)) | 
					
						
							| 
									
										
										
										
											2016-06-14 01:16:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def pre_input_hook(): | 
					
						
							| 
									
										
										
										
											2016-06-14 08:45:43 +00:00
										 |  |  |     readline.insert_text(inserted) | 
					
						
							| 
									
										
										
										
											2016-06-14 01:16:16 +00:00
										 |  |  |     readline.redisplay() | 
					
						
							| 
									
										
										
										
											2016-06-14 08:45:43 +00:00
										 |  |  | if set_pre_input_hook: | 
					
						
							|  |  |  |     set_pre_input_hook(pre_input_hook) | 
					
						
							| 
									
										
										
										
											2016-06-14 01:16:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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) | 
					
						
							| 
									
										
										
										
											2016-06-14 11:29:31 +00:00
										 |  |  |         if not is_editline and hasattr(readline, "set_pre_input_hook"): | 
					
						
							| 
									
										
										
										
											2016-06-14 05:45:31 +00:00
										 |  |  |             self.assertIn(b"substitution 't\\xeb'\r\n", output) | 
					
						
							|  |  |  |             self.assertIn(b"matches ['t\\xebnt', 't\\xebxt']\r\n", output) | 
					
						
							| 
									
										
										
										
											2016-06-14 01:16:16 +00:00
										 |  |  |         expected = br"'[\xefnserted]|t\xebxt[after]'" | 
					
						
							|  |  |  |         self.assertIn(b"result " + expected + b"\r\n", output) | 
					
						
							|  |  |  |         self.assertIn(b"history " + expected + b"\r\n", output) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-15 01:26:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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) | 
					
						
							| 
									
										
										
										
											2016-05-15 03:05:36 +00:00
										 |  |  |     with ExitStack() as cleanup: | 
					
						
							|  |  |  |         cleanup.enter_context(proc) | 
					
						
							| 
									
										
										
										
											2016-05-15 15:04:58 +00:00
										 |  |  |         def terminate(proc): | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 proc.terminate() | 
					
						
							|  |  |  |             except ProcessLookupError: | 
					
						
							|  |  |  |                 # Workaround for Open/Net BSD bug (Issue 16762) | 
					
						
							|  |  |  |                 pass | 
					
						
							|  |  |  |         cleanup.callback(terminate, proc) | 
					
						
							| 
									
										
										
										
											2016-05-15 03:05:36 +00:00
										 |  |  |         cleanup.callback(os.close, master) | 
					
						
							| 
									
										
										
										
											2016-05-15 15:04:58 +00:00
										 |  |  |         # 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()) | 
					
						
							| 
									
										
										
										
											2016-05-15 01:26:25 +00:00
										 |  |  |         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: | 
					
						
							| 
									
										
										
										
											2016-06-14 01:16:16 +00:00
										 |  |  |                         # Linux raises EIO when slave is closed (Issue 5380) | 
					
						
							| 
									
										
										
										
											2016-05-15 01:26:25 +00:00
										 |  |  |                         if err.errno != EIO: | 
					
						
							|  |  |  |                             raise | 
					
						
							|  |  |  |                         chunk = b"" | 
					
						
							|  |  |  |                     if not chunk: | 
					
						
							|  |  |  |                         return output | 
					
						
							|  |  |  |                     output.extend(chunk) | 
					
						
							|  |  |  |                 if events & selectors.EVENT_WRITE: | 
					
						
							| 
									
										
										
										
											2016-06-14 01:16:16 +00:00
										 |  |  |                     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 | 
					
						
							| 
									
										
										
										
											2016-05-15 01:26:25 +00:00
										 |  |  |                     if not input: | 
					
						
							|  |  |  |                         sel.modify(master, selectors.EVENT_READ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-24 12:23:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-20 14:53:22 +00:00
										 |  |  | if __name__ == "__main__": | 
					
						
							| 
									
										
										
										
											2015-04-13 15:00:43 -05:00
										 |  |  |     unittest.main() |