This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: readline: pre_input_hook not getting called
Type: behavior Stage:
Components: macOS Versions: Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: ronaldoussoren Nosy List: martin.panter, ned.deily, ronaldoussoren, scates
Priority: normal Keywords:

Created on 2011-11-09 07:59 by scates, last changed 2022-04-11 14:57 by admin.

Messages (5)
msg147336 - (view) Author: Sam Cates (scates) Date: 2011-11-09 07:59
OS: Mac 10.7.2
Python: 2.7.1

Setting a pre input hook in readline has no effect.  This simple example illustrates the problem:

#!/usr/bin/python

import readline

def hook():
        readline.insert_text(' from pre_input_hook')
        readline.redisplay()

readline.set_pre_input_hook(hook)

while True:
        line = raw_input('Prompt ("stop" to quit): ')
        if line == 'stop':
                break
        print 'ENTERED: "%s"' % line


The hook never gets called, thus "from pre_input_hook" is never displayed.
msg147337 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2011-11-09 08:36
Since you mention 2.7.1, presumably you are using the Apple-suppled Python 2.7.1 in OS X.  That Python is not linked with GNU readline, rather the BSD libedit library.  As a workaround you could try installing the third-party readline package from PyPI which replaces the Python redline module with one that is statically linked with GNU readline (see  http://pypi.python.org/pypi/readline).  On the other hand, if there are more differences in behavior between Python readline linked with GNU readline and with BSD libedit, they should be documented.
msg173519 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2012-10-22 13:54
1) Issue is still present with libedit on OSX 10.8

2) Libedit on OSX doesn't call rl_pre_input_hook when using the '#if defined(HAVE_RL_CALLBACK) && defined(HAVE_SELECT)' branch in the readline extension (that is, not calling readline(3), but using rl_callback_read_char).

3) When I patch pyconfig.h to enable the code patch that does use readline the callback gets called, but that doesn't affect the readline buffer.

Manually ensuring that the pre_input_hook is called at the right time is easy enough, but I don't have time to research why readline.insert_text (and/or readline.redisplay) doesn't work. The source code at <http://www.opensource.apple.com/source/libedit/libedit-31/src/readline.c> looks fine.
msg268970 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2016-06-21 05:43
Surely this should be fixed in Editline, not Python?
msg270874 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2016-07-20 14:15
The problem with fixing in editline is that Apple ships a copy of editline and that is used when build CPython by default.

IMHO it would be acceptable to work around this in the readline extension if that can be done in a clean way.
History
Date User Action Args
2022-04-11 14:57:23adminsetgithub: 57585
2016-07-20 14:15:39ronaldoussorensetmessages: + msg270874
2016-06-21 05:43:38martin.pantersetnosy: + martin.panter
messages: + msg268970
2012-10-22 13:54:06ronaldoussorensetmessages: + msg173519
2011-11-09 08:36:51ned.deilysetnosy: + ned.deily
messages: + msg147337
2011-11-09 07:59:14scatescreate