Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6afbc65

Browse files
committed
Issue #16182: set_pre_input_hook() may not exist; document, and update test
1 parent 11a693d commit 6afbc65

2 files changed

Lines changed: 20 additions & 9 deletions

File tree

Doc/library/readline.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ The following functions operate on a history file:
104104

105105
Append the last *nelements* items of history to a file. The default filename is
106106
:file:`~/.history`. The file must already exist. This calls
107-
:c:func:`append_history` in the underlying library.
107+
:c:func:`append_history` in the underlying library. This function
108+
only exists if Python was compiled for a version of the library
109+
that supports it.
108110

109111
.. versionadded:: 3.5
110112

@@ -185,7 +187,8 @@ Startup hooks
185187
be used as the new hook function; if omitted or ``None``, any
186188
function already installed is removed. The hook is called
187189
with no arguments after the first prompt has been printed and just before
188-
readline starts reading input characters.
190+
readline starts reading input characters. This function only exists
191+
if Python was compiled for a version of the library that supports it.
189192

190193

191194
Completion

Lib/test/test_readline.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,24 +129,32 @@ def test_nonascii(self):
129129

130130
script = r"""import readline
131131
132-
if readline.__doc__ and "libedit" in readline.__doc__:
133-
readline.parse_and_bind(r'bind ^B ed-prev-char')
134-
readline.parse_and_bind(r'bind "\t" rl_complete')
132+
is_editline = readline.__doc__ and "libedit" in readline.__doc__
133+
inserted = "[\xEFnserted]"
134+
macro = "|t\xEB[after]"
135+
set_pre_input_hook = getattr(readline, "set_pre_input_hook", None)
136+
if is_editline or not set_pre_input_hook:
135137
# The insert_line() call via pre_input_hook() does nothing with Editline,
136138
# so include the extra text that would have been inserted here
137-
readline.parse_and_bind('bind -s ^A "[\xEFnserted]|t\xEB[after]"')
139+
macro = inserted + macro
140+
141+
if is_editline:
142+
readline.parse_and_bind(r'bind ^B ed-prev-char')
143+
readline.parse_and_bind(r'bind "\t" rl_complete')
144+
readline.parse_and_bind(r'bind -s ^A "{}"'.format(macro))
138145
else:
139146
readline.parse_and_bind(r'Control-b: backward-char')
140147
readline.parse_and_bind(r'"\t": complete')
141148
readline.parse_and_bind(r'set disable-completion off')
142149
readline.parse_and_bind(r'set show-all-if-ambiguous off')
143150
readline.parse_and_bind(r'set show-all-if-unmodified off')
144-
readline.parse_and_bind('Control-a: "|t\xEB[after]"')
151+
readline.parse_and_bind(r'Control-a: "{}"'.format(macro))
145152
146153
def pre_input_hook():
147-
readline.insert_text("[\xEFnserted]")
154+
readline.insert_text(inserted)
148155
readline.redisplay()
149-
readline.set_pre_input_hook(pre_input_hook)
156+
if set_pre_input_hook:
157+
set_pre_input_hook(pre_input_hook)
150158
151159
def completer(text, state):
152160
if text == "t\xEB":

0 commit comments

Comments
 (0)