| 1 |
""" |
| 2 |
Very minimal unittests for parts of the readline module. |
| 3 |
""" |
| 4 |
import codecs |
| 5 |
import locale |
| 6 |
import os |
| 7 |
import sys |
| 8 |
import tempfile |
| 9 |
import textwrap |
| 10 |
import threading |
| 11 |
import unittest |
| 12 |
from test import support |
| 13 |
from test.support import threading_helper |
| 14 |
from test.support import verbose |
| 15 |
from test.support.import_helper import import_module |
| 16 |
from test.support.os_helper import unlink, temp_dir, TESTFN |
| 17 |
from test.support.pty_helper import run_pty |
| 18 |
from test.support.script_helper import assert_python_ok |
| 19 |
from test.support.threading_helper import requires_working_threading |
| 20 |
|
| 21 |
# Skip tests if there is no readline module |
| 22 |
readline = import_module('readline') |
| 23 |
|
| 24 |
if hasattr(readline, "_READLINE_LIBRARY_VERSION"): |
| 25 |
is_editline = ("EditLine wrapper" in readline._READLINE_LIBRARY_VERSION) |
| 26 |
else: |
| 27 |
is_editline = readline.backend == "editline" |
| 28 |
|
| 29 |
|
| 30 |
def setUpModule(): |
| 31 |
if verbose: |
| 32 |
# Python implementations other than CPython may not have |
| 33 |
# these private attributes |
| 34 |
if hasattr(readline, "_READLINE_VERSION"): |
| 35 |
print(f"readline version: {readline._READLINE_VERSION:#x}") |
| 36 |
print(f"readline runtime version: {readline._READLINE_RUNTIME_VERSION:#x}") |
| 37 |
if hasattr(readline, "_READLINE_LIBRARY_VERSION"): |
| 38 |
print(f"readline library version: {readline._READLINE_LIBRARY_VERSION!r}") |
| 39 |
print(f"use libedit emulation? {is_editline}") |
| 40 |
|
| 41 |
|
| 42 |
@unittest.skipUnless(hasattr(readline, "clear_history"), |
| 43 |
"The history update test cannot be run because the " |
| 44 |
"clear_history method is not available.") |
| 45 |
class TestHistoryManipulation (unittest.TestCase): |
| 46 |
""" |
| 47 |
These tests were added to check that the libedit emulation on OSX and the |
| 48 |
"real" readline have the same interface for history manipulation. That's |
| 49 |
why the tests cover only a small subset of the interface. |
| 50 |
""" |
| 51 |
|
| 52 |
def testHistoryUpdates(self): |
| 53 |
readline.clear_history() |
| 54 |
|
| 55 |
readline.add_history("first line") |
| 56 |
readline.add_history("second line") |
| 57 |
|
| 58 |
self.assertEqual(readline.get_history_item(0), None) |
| 59 |
self.assertEqual(readline.get_history_item(1), "first line") |
| 60 |
self.assertEqual(readline.get_history_item(2), "second line") |
| 61 |
|
| 62 |
readline.replace_history_item(0, "replaced line") |
| 63 |
self.assertEqual(readline.get_history_item(0), None) |
| 64 |
self.assertEqual(readline.get_history_item(1), "replaced line") |
| 65 |
self.assertEqual(readline.get_history_item(2), "second line") |
| 66 |
|
| 67 |
self.assertEqual(readline.get_current_history_length(), 2) |
| 68 |
|
| 69 |
readline.remove_history_item(0) |
| 70 |
self.assertEqual(readline.get_history_item(0), None) |
| 71 |
self.assertEqual(readline.get_history_item(1), "second line") |
| 72 |
|
| 73 |
self.assertEqual(readline.get_current_history_length(), 1) |
| 74 |
|
| 75 |
@unittest.skipUnless(hasattr(readline, "append_history_file"), |
| 76 |
"append_history not available") |
| 77 |
def test_write_read_append(self): |
| 78 |
hfile = tempfile.NamedTemporaryFile(delete=False) |
| 79 |
hfile.close() |
| 80 |
hfilename = hfile.name |
| 81 |
self.addCleanup(unlink, hfilename) |
| 82 |
|
| 83 |
# test write-clear-read == nop |
| 84 |
readline.clear_history() |
| 85 |
readline.add_history("first line") |
| 86 |
readline.add_history("second line") |
| 87 |
readline.write_history_file(hfilename) |
| 88 |
|
| 89 |
readline.clear_history() |
| 90 |
self.assertEqual(readline.get_current_history_length(), 0) |
| 91 |
|
| 92 |
readline.read_history_file(hfilename) |
| 93 |
self.assertEqual(readline.get_current_history_length(), 2) |
| 94 |
self.assertEqual(readline.get_history_item(1), "first line") |
| 95 |
self.assertEqual(readline.get_history_item(2), "second line") |
| 96 |
|
| 97 |
# test append |
| 98 |
readline.append_history_file(1, hfilename) |
| 99 |
readline.clear_history() |
| 100 |
readline.read_history_file(hfilename) |
| 101 |
self.assertEqual(readline.get_current_history_length(), 3) |
| 102 |
self.assertEqual(readline.get_history_item(1), "first line") |
| 103 |
self.assertEqual(readline.get_history_item(2), "second line") |
| 104 |
self.assertEqual(readline.get_history_item(3), "second line") |
| 105 |
|
| 106 |
# test 'no such file' behaviour |
| 107 |
os.unlink(hfilename) |
| 108 |
try: |
| 109 |
readline.append_history_file(1, hfilename) |
| 110 |
except FileNotFoundError: |
| 111 |
pass # Some implementations return this error (libreadline). |
| 112 |
else: |
| 113 |
os.unlink(hfilename) # Some create it anyways (libedit). |
| 114 |
# If the file wasn't created, unlink will fail. |
| 115 |
# We're just testing that one of the two expected behaviors happens |
| 116 |
# instead of an incorrect error. |
| 117 |
|
| 118 |
# write_history_file can create the target |
| 119 |
readline.write_history_file(hfilename) |
| 120 |
|
| 121 |
# Negative values should be disallowed |
| 122 |
with self.assertRaises(ValueError): |
| 123 |
readline.append_history_file(-42, hfilename) |
| 124 |
|
| 125 |
# See gh-122431, using the minimum signed integer value caused a segfault |
| 126 |
with self.assertRaises(ValueError): |
| 127 |
readline.append_history_file(-2147483648, hfilename) |
| 128 |
|
| 129 |
def test_nonascii_history(self): |
| 130 |
readline.clear_history() |
| 131 |
try: |
| 132 |
readline.add_history("entrée 1") |
| 133 |
except UnicodeEncodeError as err: |
| 134 |
self.skipTest("Locale cannot encode test data: " + format(err)) |
| 135 |
readline.add_history("entrée 2") |
| 136 |
readline.replace_history_item(1, "entrée 22") |
| 137 |
readline.write_history_file(TESTFN) |
| 138 |
self.addCleanup(os.remove, TESTFN) |
| 139 |
readline.clear_history() |
| 140 |
readline.read_history_file(TESTFN) |
| 141 |
if is_editline: |
| 142 |
# An add_history() call seems to be required for get_history_ |
| 143 |
# item() to register items from the file |
| 144 |
readline.add_history("dummy") |
| 145 |
self.assertEqual(readline.get_history_item(1), "entrée 1") |
| 146 |
self.assertEqual(readline.get_history_item(2), "entrée 22") |
| 147 |
|
| 148 |
def test_write_read_limited_history(self): |
| 149 |
previous_length = readline.get_history_length() |
| 150 |
self.addCleanup(readline.set_history_length, previous_length) |
| 151 |
|
| 152 |
readline.clear_history() |
| 153 |
readline.add_history("first line") |
| 154 |
readline.add_history("second line") |
| 155 |
readline.add_history("third line") |
| 156 |
|
| 157 |
readline.set_history_length(2) |
| 158 |
self.assertEqual(readline.get_history_length(), 2) |
| 159 |
readline.write_history_file(TESTFN) |
| 160 |
self.addCleanup(os.remove, TESTFN) |
| 161 |
|
| 162 |
readline.clear_history() |
| 163 |
self.assertEqual(readline.get_current_history_length(), 0) |
| 164 |
self.assertEqual(readline.get_history_length(), 2) |
| 165 |
|
| 166 |
readline.read_history_file(TESTFN) |
| 167 |
self.assertEqual(readline.get_history_item(1), "second line") |
| 168 |
self.assertEqual(readline.get_history_item(2), "third line") |
| 169 |
self.assertEqual(readline.get_history_item(3), None) |
| 170 |
|
| 171 |
# Readline seems to report an additional history element. |
| 172 |
self.assertIn(readline.get_current_history_length(), (2, 3)) |
| 173 |
|
| 174 |
|
| 175 |
class TestReadline(unittest.TestCase): |
| 176 |
|
| 177 |
@unittest.skipIf(readline._READLINE_VERSION < 0x0601 and not is_editline, |
| 178 |
"not supported in this library version") |
| 179 |
def test_init(self): |
| 180 |
# Issue #19884: Ensure that the ANSI sequence "\033[1034h" is not |
| 181 |
# written into stdout when the readline module is imported and stdout |
| 182 |
# is redirected to a pipe. |
| 183 |
rc, stdout, stderr = assert_python_ok('-c', 'import readline', |
| 184 |
TERM='xterm-256color') |
| 185 |
self.assertEqual(stdout, b'') |
| 186 |
|
| 187 |
def test_backend(self): |
| 188 |
self.assertIn(readline.backend, ("readline", "editline")) |
| 189 |
|
| 190 |
auto_history_script = """\ |
| 191 |
import readline |
| 192 |
readline.set_auto_history({}) |
| 193 |
input() |
| 194 |
print("History length:", readline.get_current_history_length()) |
| 195 |
""" |
| 196 |
|
| 197 |
def test_auto_history_enabled(self): |
| 198 |
output = run_pty(self.auto_history_script.format(True)) |
| 199 |
# bpo-44949: Sometimes, the newline character is not written at the |
| 200 |
# end, so don't expect it in the output. |
| 201 |
self.assertIn(b"History length: 1", output) |
| 202 |
|
| 203 |
def test_auto_history_disabled(self): |
| 204 |
output = run_pty(self.auto_history_script.format(False)) |
| 205 |
# bpo-44949: Sometimes, the newline character is not written at the |
| 206 |
# end, so don't expect it in the output. |
| 207 |
self.assertIn(b"History length: 0", output) |
| 208 |
|
| 209 |
def test_set_complete_delims(self): |
| 210 |
script = textwrap.dedent(""" |
| 211 |
import readline |
| 212 |
def complete(text, state): |
| 213 |
if state == 0 and text == "$": |
| 214 |
return "$complete" |
| 215 |
return None |
| 216 |
if readline.backend == "editline": |
| 217 |
readline.parse_and_bind(r'bind "\\t" rl_complete') |
| 218 |
else: |
| 219 |
readline.parse_and_bind(r'"\\t": complete') |
| 220 |
readline.set_completer_delims(" \\t\\n") |
| 221 |
readline.set_completer(complete) |
| 222 |
print(input()) |
| 223 |
""") |
| 224 |
|
| 225 |
output = run_pty(script, input=b"$\t\n") |
| 226 |
self.assertIn(b"$complete", output) |
| 227 |
|
| 228 |
def test_nonascii(self): |
| 229 |
loc = locale.setlocale(locale.LC_CTYPE, None) |
| 230 |
if loc in ('C', 'POSIX'): |
| 231 |
# bpo-29240: On FreeBSD, if the LC_CTYPE locale is C or POSIX, |
| 232 |
# writing and reading non-ASCII bytes into/from a TTY works, but |
| 233 |
# readline or ncurses ignores non-ASCII bytes on read. |
| 234 |
self.skipTest(f"the LC_CTYPE locale is {loc!r}") |
| 235 |
if sys.flags.utf8_mode: |
| 236 |
encoding = locale.getencoding() |
| 237 |
encoding = codecs.lookup(encoding).name # normalize the name |
| 238 |
if encoding != "utf-8": |
| 239 |
# gh-133711: The Python UTF-8 Mode ignores the LC_CTYPE locale |
| 240 |
# and always use the UTF-8 encoding. |
| 241 |
self.skipTest(f"the LC_CTYPE encoding is {encoding!r}") |
| 242 |
|
| 243 |
try: |
| 244 |
readline.add_history("\xEB\xEF") |
| 245 |
except UnicodeEncodeError as err: |
| 246 |
self.skipTest("Locale cannot encode test data: " + format(err)) |
| 247 |
|
| 248 |
script = r"""import readline |
| 249 |
|
| 250 |
is_editline = readline.backend == "editline" |
| 251 |
inserted = "[\xEFnserted]" |
| 252 |
macro = "|t\xEB[after]" |
| 253 |
set_pre_input_hook = getattr(readline, "set_pre_input_hook", None) |
| 254 |
if is_editline or not set_pre_input_hook: |
| 255 |
# The insert_line() call via pre_input_hook() does nothing with Editline, |
| 256 |
# so include the extra text that would have been inserted here |
| 257 |
macro = inserted + macro |
| 258 |
|
| 259 |
if is_editline: |
| 260 |
readline.parse_and_bind(r'bind ^B ed-prev-char') |
| 261 |
readline.parse_and_bind(r'bind "\t" rl_complete') |
| 262 |
readline.parse_and_bind(r'bind -s ^A "{}"'.format(macro)) |
| 263 |
else: |
| 264 |
readline.parse_and_bind(r'Control-b: backward-char') |
| 265 |
readline.parse_and_bind(r'"\t": complete') |
| 266 |
readline.parse_and_bind(r'set disable-completion off') |
| 267 |
readline.parse_and_bind(r'set show-all-if-ambiguous off') |
| 268 |
readline.parse_and_bind(r'set show-all-if-unmodified off') |
| 269 |
readline.parse_and_bind(r'Control-a: "{}"'.format(macro)) |
| 270 |
|
| 271 |
def pre_input_hook(): |
| 272 |
readline.insert_text(inserted) |
| 273 |
readline.redisplay() |
| 274 |
if set_pre_input_hook: |
| 275 |
set_pre_input_hook(pre_input_hook) |
| 276 |
|
| 277 |
def completer(text, state): |
| 278 |
if text == "t\xEB": |
| 279 |
if state == 0: |
| 280 |
print("text", ascii(text)) |
| 281 |
print("line", ascii(readline.get_line_buffer())) |
| 282 |
print("indexes", readline.get_begidx(), readline.get_endidx()) |
| 283 |
return "t\xEBnt" |
| 284 |
if state == 1: |
| 285 |
return "t\xEBxt" |
| 286 |
if text == "t\xEBx" and state == 0: |
| 287 |
return "t\xEBxt" |
| 288 |
return None |
| 289 |
readline.set_completer(completer) |
| 290 |
|
| 291 |
def display(substitution, matches, longest_match_length): |
| 292 |
print("substitution", ascii(substitution)) |
| 293 |
print("matches", ascii(matches)) |
| 294 |
readline.set_completion_display_matches_hook(display) |
| 295 |
|
| 296 |
print("result", ascii(input())) |
| 297 |
print("history", ascii(readline.get_history_item(1))) |
| 298 |
""" |
| 299 |
|
| 300 |
input = b"\x01" # Ctrl-A, expands to "|t\xEB[after]" |
| 301 |
input += b"\x02" * len("[after]") # Move cursor back |
| 302 |
input += b"\t\t" # Display possible completions |
| 303 |
input += b"x\t" # Complete "t\xEBx" -> "t\xEBxt" |
| 304 |
input += b"\r" |
| 305 |
output = run_pty(script, input) |
| 306 |
self.assertIn(b"text 't\\xeb'\r\n", output) |
| 307 |
self.assertIn(b"line '[\\xefnserted]|t\\xeb[after]'\r\n", output) |
| 308 |
if sys.platform == "darwin" or not is_editline: |
| 309 |
self.assertIn(b"indexes 11 13\r\n", output) |
| 310 |
# Non-macOS libedit does not handle non-ASCII bytes |
| 311 |
# the same way and generates character indices |
| 312 |
# rather than byte indices via get_begidx() and |
| 313 |
# get_endidx(). Ex: libedit2 3.1-20191231-2 on Debian |
| 314 |
# winds up with "indexes 10 12". Stemming from the |
| 315 |
# start and end values calls back into readline.c's |
| 316 |
# rl_attempted_completion_function = flex_complete with: |
| 317 |
# (11, 13) instead of libreadline's (12, 15). |
| 318 |
|
| 319 |
if not is_editline and hasattr(readline, "set_pre_input_hook"): |
| 320 |
self.assertIn(b"substitution 't\\xeb'\r\n", output) |
| 321 |
self.assertIn(b"matches ['t\\xebnt', 't\\xebxt']\r\n", output) |
| 322 |
expected = br"'[\xefnserted]|t\xebxt[after]'" |
| 323 |
self.assertIn(b"result " + expected + b"\r\n", output) |
| 324 |
# bpo-45195: Sometimes, the newline character is not written at the |
| 325 |
# end, so don't expect it in the output. |
| 326 |
self.assertIn(b"history " + expected, output) |
| 327 |
|
| 328 |
# We have 2 reasons to skip this test: |
| 329 |
# - readline: history size was added in 6.0 |
| 330 |
# See https://cnswww.cns.cwru.edu/php/chet/readline/CHANGES |
| 331 |
# - editline: history size is broken on OS X 10.11.6. |
| 332 |
# Newer versions were not tested yet. |
| 333 |
@unittest.skipIf(readline._READLINE_VERSION < 0x600, |
| 334 |
"this readline version does not support history-size") |
| 335 |
@unittest.skipIf(is_editline, |
| 336 |
"editline history size configuration is broken") |
| 337 |
def test_history_size(self): |
| 338 |
history_size = 10 |
| 339 |
with temp_dir() as test_dir: |
| 340 |
inputrc = os.path.join(test_dir, "inputrc") |
| 341 |
with open(inputrc, "wb") as f: |
| 342 |
f.write(b"set history-size %d\n" % history_size) |
| 343 |
|
| 344 |
history_file = os.path.join(test_dir, "history") |
| 345 |
with open(history_file, "wb") as f: |
| 346 |
# history_size * 2 items crashes readline |
| 347 |
data = b"".join(b"item %d\n" % i |
| 348 |
for i in range(history_size * 2)) |
| 349 |
f.write(data) |
| 350 |
|
| 351 |
script = """ |
| 352 |
import os |
| 353 |
import readline |
| 354 |
|
| 355 |
history_file = os.environ["HISTORY_FILE"] |
| 356 |
readline.read_history_file(history_file) |
| 357 |
input() |
| 358 |
readline.write_history_file(history_file) |
| 359 |
""" |
| 360 |
|
| 361 |
env = dict(os.environ) |
| 362 |
env["INPUTRC"] = inputrc |
| 363 |
env["HISTORY_FILE"] = history_file |
| 364 |
|
| 365 |
run_pty(script, input=b"last input\r", env=env) |
| 366 |
|
| 367 |
with open(history_file, "rb") as f: |
| 368 |
lines = f.readlines() |
| 369 |
self.assertEqual(len(lines), history_size) |
| 370 |
self.assertEqual(lines[-1].strip(), b"last input") |
| 371 |
|
| 372 |
@requires_working_threading() |
| 373 |
def test_gh123321_threadsafe(self): |
| 374 |
"""gh-123321: readline should be thread-safe and not crash""" |
| 375 |
script = textwrap.dedent(r""" |
| 376 |
import threading |
| 377 |
from test.support.threading_helper import join_thread |
| 378 |
|
| 379 |
def func(): |
| 380 |
input() |
| 381 |
|
| 382 |
thread1 = threading.Thread(target=func) |
| 383 |
thread2 = threading.Thread(target=func) |
| 384 |
thread1.start() |
| 385 |
thread2.start() |
| 386 |
join_thread(thread1) |
| 387 |
join_thread(thread2) |
| 388 |
print("done") |
| 389 |
""") |
| 390 |
|
| 391 |
output = run_pty(script, input=b"input1\rinput2\r") |
| 392 |
|
| 393 |
self.assertIn(b"done", output) |
| 394 |
|
| 395 |
|
| 396 |
def test_write_read_limited_history(self): |
| 397 |
previous_length = readline.get_history_length() |
| 398 |
self.addCleanup(readline.set_history_length, previous_length) |
| 399 |
|
| 400 |
readline.add_history("first line") |
| 401 |
readline.add_history("second line") |
| 402 |
readline.add_history("third line") |
| 403 |
|
| 404 |
readline.set_history_length(2) |
| 405 |
self.assertEqual(readline.get_history_length(), 2) |
| 406 |
readline.write_history_file(TESTFN) |
| 407 |
self.addCleanup(os.remove, TESTFN) |
| 408 |
|
| 409 |
readline.read_history_file(TESTFN) |
| 410 |
# Without clear_history() there's no good way to test if |
| 411 |
# the correct entries are present (we're combining history limiting and |
| 412 |
# possible deduplication with arbitrary previous content). |
| 413 |
# So, we've only tested that the read did not fail. |
| 414 |
# See TestHistoryManipulation for the full test. |
| 415 |
|
| 416 |
|
| 417 |
@unittest.skipUnless(support.Py_GIL_DISABLED, 'these tests can only possibly fail with GIL disabled') |
| 418 |
class FreeThreadingTest(unittest.TestCase): |
| 419 |
@threading_helper.reap_threads |
| 420 |
@threading_helper.requires_working_threading() |
| 421 |
def test_free_threading(self): |
| 422 |
def completer_delims(b): |
| 423 |
b.wait() |
| 424 |
for _ in range(100): |
| 425 |
readline.get_completer_delims() |
| 426 |
readline.set_completer_delims(' \t\n`@#%^&*()=+[{]}\\|;:\'",<>?') |
| 427 |
readline.set_completer_delims(' \t\n`@#%^&*()=+[{]}\\|;:\'",<>?') |
| 428 |
readline.get_completer_delims() |
| 429 |
|
| 430 |
count = 40 |
| 431 |
barrier = threading.Barrier(count) |
| 432 |
threads = [threading.Thread(target=completer_delims, args=(barrier,)) for _ in range(count)] |
| 433 |
|
| 434 |
with threading_helper.start_threads(threads): |
| 435 |
pass |
| 436 |
|
| 437 |
|
| 438 |
if __name__ == "__main__": |
| 439 |
unittest.main() |