summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_readline.py
diff options
context:
space:
mode:
authorNir Soffer <nirsof@gmail.com>2017-07-07 06:10:46 (GMT)
committerBerker Peksag <berker.peksag@gmail.com>2017-07-07 06:10:46 (GMT)
commitfae8f4a9cb88a68eb14750cbb8ddf8740fd67b8b (patch)
treee94fb9176f0309f41cb51fa3f16ba616d94d7ebd /Lib/test/test_readline.py
parent25a4206c243e3b1fa6f5b1c72a11b409b007694d (diff)
downloadcpython-fae8f4a9cb88a68eb14750cbb8ddf8740fd67b8b.zip
cpython-fae8f4a9cb88a68eb14750cbb8ddf8740fd67b8b.tar.gz
cpython-fae8f4a9cb88a68eb14750cbb8ddf8740fd67b8b.tar.bz2
bpo-29854: Fix segfault in call_readline() (GH-728)
If history-length is set in .inputrc, and the history file is double the history size (or more), history_get(N) returns NULL, and python segfaults. Fix that by checking for NULL return value. It seems that the root cause is incorrect handling of bigger history in readline, but Python should not segfault even if readline returns unexpected value. This issue affects only GNU readline. When using libedit emulation system history size option does not work.
Diffstat (limited to 'Lib/test/test_readline.py')
-rw-r--r--Lib/test/test_readline.py43
1 files changed, 40 insertions, 3 deletions
diff --git a/Lib/test/test_readline.py b/Lib/test/test_readline.py
index 06a9149..cc3001a 100644
--- a/Lib/test/test_readline.py
+++ b/Lib/test/test_readline.py
@@ -9,7 +9,7 @@ import subprocess
import sys
import tempfile
import unittest
-from test.support import import_module, unlink, TESTFN
+from test.support import import_module, unlink, temp_dir, TESTFN
from test.support.script_helper import assert_python_ok
# Skip tests if there is no readline module
@@ -210,13 +210,50 @@ print("history", ascii(readline.get_history_item(1)))
self.assertIn(b"result " + expected + b"\r\n", output)
self.assertIn(b"history " + expected + b"\r\n", output)
+ @unittest.skipIf(is_editline,
+ "editline history size configuration is broken")
+ def test_history_size(self):
+ history_size = 10
+ with temp_dir() as test_dir:
+ inputrc = os.path.join(test_dir, "inputrc")
+ with open(inputrc, "wb") as f:
+ f.write(b"set history-size %d\n" % history_size)
+
+ history_file = os.path.join(test_dir, "history")
+ with open(history_file, "wb") as f:
+ # history_size * 2 items crashes readline
+ data = b"".join(b"item %d\n" % i
+ for i in range(history_size * 2))
+ f.write(data)
+
+ script = """
+import os
+import readline
+
+history_file = os.environ["HISTORY_FILE"]
+readline.read_history_file(history_file)
+input()
+readline.write_history_file(history_file)
+"""
+
+ env = dict(os.environ)
+ env["INPUTRC"] = inputrc
+ env["HISTORY_FILE"] = history_file
+
+ run_pty(script, input=b"last input\r", env=env)
+
+ with open(history_file, "rb") as f:
+ lines = f.readlines()
+ self.assertEqual(len(lines), history_size)
+ self.assertEqual(lines[-1].strip(), b"last input")
+
-def run_pty(script, input=b"dummy input\r"):
+def run_pty(script, input=b"dummy input\r", env=None):
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)
+ proc = subprocess.Popen(args, stdin=slave, stdout=slave, stderr=slave, env=env)
os.close(slave)
with ExitStack() as cleanup:
cleanup.enter_context(proc)