mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
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.
This commit is contained in:
parent
53ec7c8fc0
commit
4238a975d7
5 changed files with 78 additions and 1 deletions
|
|
@ -246,6 +246,15 @@ Startup hooks
|
||||||
if Python was compiled for a version of the library that supports it.
|
if Python was compiled for a version of the library that supports it.
|
||||||
|
|
||||||
|
|
||||||
|
.. function:: get_pre_input_hook()
|
||||||
|
|
||||||
|
Get the current pre-input hook function, or ``None`` if no pre-input hook
|
||||||
|
function has been set. This function only exists if Python was compiled
|
||||||
|
for a version of the library that supports it.
|
||||||
|
|
||||||
|
.. versionadded:: next
|
||||||
|
|
||||||
|
|
||||||
.. _readline-completion:
|
.. _readline-completion:
|
||||||
|
|
||||||
Completion
|
Completion
|
||||||
|
|
|
||||||
|
|
@ -413,6 +413,24 @@ def test_write_read_limited_history(self):
|
||||||
# So, we've only tested that the read did not fail.
|
# So, we've only tested that the read did not fail.
|
||||||
# See TestHistoryManipulation for the full test.
|
# See TestHistoryManipulation for the full test.
|
||||||
|
|
||||||
|
@unittest.skipUnless(hasattr(readline, "get_pre_input_hook"),
|
||||||
|
"get_pre_input_hook not available")
|
||||||
|
def test_get_pre_input_hook(self):
|
||||||
|
# Save and restore the original hook to avoid side effects
|
||||||
|
original_hook = readline.get_pre_input_hook()
|
||||||
|
self.addCleanup(readline.set_pre_input_hook, original_hook)
|
||||||
|
|
||||||
|
# Test that get_pre_input_hook returns None when no hook is set
|
||||||
|
readline.set_pre_input_hook(None)
|
||||||
|
self.assertIsNone(readline.get_pre_input_hook())
|
||||||
|
|
||||||
|
# Set a hook and verify we can retrieve it
|
||||||
|
def my_hook():
|
||||||
|
pass
|
||||||
|
|
||||||
|
readline.set_pre_input_hook(my_hook)
|
||||||
|
self.assertIs(readline.get_pre_input_hook(), my_hook)
|
||||||
|
|
||||||
|
|
||||||
@unittest.skipUnless(support.Py_GIL_DISABLED, 'these tests can only possibly fail with GIL disabled')
|
@unittest.skipUnless(support.Py_GIL_DISABLED, 'these tests can only possibly fail with GIL disabled')
|
||||||
class FreeThreadingTest(unittest.TestCase):
|
class FreeThreadingTest(unittest.TestCase):
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
Add :func:`readline.get_pre_input_hook` function to retrieve the current
|
||||||
|
pre-input hook. This allows applications to save and restore the hook
|
||||||
|
without overwriting user settings. Patch by Sanyam Khurana.
|
||||||
28
Modules/clinic/readline.c.h
generated
28
Modules/clinic/readline.c.h
generated
|
|
@ -349,6 +349,28 @@ exit:
|
||||||
|
|
||||||
#endif /* defined(HAVE_RL_PRE_INPUT_HOOK) */
|
#endif /* defined(HAVE_RL_PRE_INPUT_HOOK) */
|
||||||
|
|
||||||
|
#if defined(HAVE_RL_PRE_INPUT_HOOK)
|
||||||
|
|
||||||
|
PyDoc_STRVAR(readline_get_pre_input_hook__doc__,
|
||||||
|
"get_pre_input_hook($module, /)\n"
|
||||||
|
"--\n"
|
||||||
|
"\n"
|
||||||
|
"Get the current pre-input hook function.");
|
||||||
|
|
||||||
|
#define READLINE_GET_PRE_INPUT_HOOK_METHODDEF \
|
||||||
|
{"get_pre_input_hook", (PyCFunction)readline_get_pre_input_hook, METH_NOARGS, readline_get_pre_input_hook__doc__},
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
readline_get_pre_input_hook_impl(PyObject *module);
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
readline_get_pre_input_hook(PyObject *module, PyObject *Py_UNUSED(ignored))
|
||||||
|
{
|
||||||
|
return readline_get_pre_input_hook_impl(module);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* defined(HAVE_RL_PRE_INPUT_HOOK) */
|
||||||
|
|
||||||
PyDoc_STRVAR(readline_get_completion_type__doc__,
|
PyDoc_STRVAR(readline_get_completion_type__doc__,
|
||||||
"get_completion_type($module, /)\n"
|
"get_completion_type($module, /)\n"
|
||||||
"--\n"
|
"--\n"
|
||||||
|
|
@ -794,7 +816,11 @@ readline_redisplay(PyObject *module, PyObject *Py_UNUSED(ignored))
|
||||||
#define READLINE_SET_PRE_INPUT_HOOK_METHODDEF
|
#define READLINE_SET_PRE_INPUT_HOOK_METHODDEF
|
||||||
#endif /* !defined(READLINE_SET_PRE_INPUT_HOOK_METHODDEF) */
|
#endif /* !defined(READLINE_SET_PRE_INPUT_HOOK_METHODDEF) */
|
||||||
|
|
||||||
|
#ifndef READLINE_GET_PRE_INPUT_HOOK_METHODDEF
|
||||||
|
#define READLINE_GET_PRE_INPUT_HOOK_METHODDEF
|
||||||
|
#endif /* !defined(READLINE_GET_PRE_INPUT_HOOK_METHODDEF) */
|
||||||
|
|
||||||
#ifndef READLINE_CLEAR_HISTORY_METHODDEF
|
#ifndef READLINE_CLEAR_HISTORY_METHODDEF
|
||||||
#define READLINE_CLEAR_HISTORY_METHODDEF
|
#define READLINE_CLEAR_HISTORY_METHODDEF
|
||||||
#endif /* !defined(READLINE_CLEAR_HISTORY_METHODDEF) */
|
#endif /* !defined(READLINE_CLEAR_HISTORY_METHODDEF) */
|
||||||
/*[clinic end generated code: output=88d9812b6caa2102 input=a9049054013a1b77]*/
|
/*[clinic end generated code: output=4bd95070973cd0e2 input=a9049054013a1b77]*/
|
||||||
|
|
|
||||||
|
|
@ -572,6 +572,26 @@ readline_set_pre_input_hook_impl(PyObject *module, PyObject *function)
|
||||||
return set_hook("pre_input_hook", &state->pre_input_hook,
|
return set_hook("pre_input_hook", &state->pre_input_hook,
|
||||||
function);
|
function);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Get pre-input hook */
|
||||||
|
|
||||||
|
/*[clinic input]
|
||||||
|
readline.get_pre_input_hook
|
||||||
|
|
||||||
|
Get the current pre-input hook function.
|
||||||
|
[clinic start generated code]*/
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
readline_get_pre_input_hook_impl(PyObject *module)
|
||||||
|
/*[clinic end generated code: output=ad56b77a8e8981ca input=fb1e1b1fbd94e4e5]*/
|
||||||
|
{
|
||||||
|
readlinestate *state = get_readline_state(module);
|
||||||
|
if (state->pre_input_hook == NULL) {
|
||||||
|
Py_RETURN_NONE;
|
||||||
|
}
|
||||||
|
return Py_NewRef(state->pre_input_hook);
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1074,6 +1094,7 @@ static struct PyMethodDef readline_methods[] =
|
||||||
READLINE_SET_STARTUP_HOOK_METHODDEF
|
READLINE_SET_STARTUP_HOOK_METHODDEF
|
||||||
#ifdef HAVE_RL_PRE_INPUT_HOOK
|
#ifdef HAVE_RL_PRE_INPUT_HOOK
|
||||||
READLINE_SET_PRE_INPUT_HOOK_METHODDEF
|
READLINE_SET_PRE_INPUT_HOOK_METHODDEF
|
||||||
|
READLINE_GET_PRE_INPUT_HOOK_METHODDEF
|
||||||
#endif
|
#endif
|
||||||
#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
|
#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
|
||||||
READLINE_CLEAR_HISTORY_METHODDEF
|
READLINE_CLEAR_HISTORY_METHODDEF
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue