Skip to content

Commit 4238a97

Browse files
gh-48752: Add readline.get_pre_input_hook() function (#141586)
Add readline.get_pre_input_hook() to retrieve the current pre-input hook. This allows applications to save and restore the hook without overwriting user settings.
1 parent 53ec7c8 commit 4238a97

File tree

5 files changed

+78
-1
lines changed

5 files changed

+78
-1
lines changed

Doc/library/readline.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,15 @@ Startup hooks
246246
if Python was compiled for a version of the library that supports it.
247247

248248

249+
.. function:: get_pre_input_hook()
250+
251+
Get the current pre-input hook function, or ``None`` if no pre-input hook
252+
function has been set. This function only exists if Python was compiled
253+
for a version of the library that supports it.
254+
255+
.. versionadded:: next
256+
257+
249258
.. _readline-completion:
250259

251260
Completion

Lib/test/test_readline.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,24 @@ def test_write_read_limited_history(self):
413413
# So, we've only tested that the read did not fail.
414414
# See TestHistoryManipulation for the full test.
415415

416+
@unittest.skipUnless(hasattr(readline, "get_pre_input_hook"),
417+
"get_pre_input_hook not available")
418+
def test_get_pre_input_hook(self):
419+
# Save and restore the original hook to avoid side effects
420+
original_hook = readline.get_pre_input_hook()
421+
self.addCleanup(readline.set_pre_input_hook, original_hook)
422+
423+
# Test that get_pre_input_hook returns None when no hook is set
424+
readline.set_pre_input_hook(None)
425+
self.assertIsNone(readline.get_pre_input_hook())
426+
427+
# Set a hook and verify we can retrieve it
428+
def my_hook():
429+
pass
430+
431+
readline.set_pre_input_hook(my_hook)
432+
self.assertIs(readline.get_pre_input_hook(), my_hook)
433+
416434

417435
@unittest.skipUnless(support.Py_GIL_DISABLED, 'these tests can only possibly fail with GIL disabled')
418436
class FreeThreadingTest(unittest.TestCase):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add :func:`readline.get_pre_input_hook` function to retrieve the current
2+
pre-input hook. This allows applications to save and restore the hook
3+
without overwriting user settings. Patch by Sanyam Khurana.

Modules/clinic/readline.c.h

Lines changed: 27 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/readline.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,26 @@ readline_set_pre_input_hook_impl(PyObject *module, PyObject *function)
572572
return set_hook("pre_input_hook", &state->pre_input_hook,
573573
function);
574574
}
575+
576+
/* Get pre-input hook */
577+
578+
/*[clinic input]
579+
readline.get_pre_input_hook
580+
581+
Get the current pre-input hook function.
582+
[clinic start generated code]*/
583+
584+
static PyObject *
585+
readline_get_pre_input_hook_impl(PyObject *module)
586+
/*[clinic end generated code: output=ad56b77a8e8981ca input=fb1e1b1fbd94e4e5]*/
587+
{
588+
readlinestate *state = get_readline_state(module);
589+
if (state->pre_input_hook == NULL) {
590+
Py_RETURN_NONE;
591+
}
592+
return Py_NewRef(state->pre_input_hook);
593+
}
594+
575595
#endif
576596

577597

@@ -1074,6 +1094,7 @@ static struct PyMethodDef readline_methods[] =
10741094
READLINE_SET_STARTUP_HOOK_METHODDEF
10751095
#ifdef HAVE_RL_PRE_INPUT_HOOK
10761096
READLINE_SET_PRE_INPUT_HOOK_METHODDEF
1097+
READLINE_GET_PRE_INPUT_HOOK_METHODDEF
10771098
#endif
10781099
#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
10791100
READLINE_CLEAR_HISTORY_METHODDEF

0 commit comments

Comments
 (0)