Baffled by readline module

Grant Edwards grant.b.edwards at gmail.com
Thu Mar 9 19:46:36 EST 2023


On 2023-03-10, Greg Ewing via Python-list <python-list at python.org> wrote:
> On 10/03/23 10:08 am, Grant Edwards wrote:
>> It finally dawned on me after seeing an example I found elsewhere that
>> you don't call some module method to fetch the next user-entered line.
>> 
>> You call the input() built-in.
>> 
>> Having a module modify the behavior of a built-in makes me cringe.
>
> Importing the module is not modifying the built-in.
>
> If your Python has been compiled with gnu readline support,
> input() *already* provides recall and editing facilities.

That's not how Python 3.10.10 works for me. When I run the code below,
I do not get command recall and editing. If I hit arrow keys, I just
see the escape sequence echoed and returned by input(). Likewise for
things like ctrl-P, ctrl-N, etc. I have to uncomment the import
statement to get command line recall and editing to work. Without the
import, the escape sequences from arrow keys just end up in the input
data.

#!/usr/bin/python

# import readline
# readline.parse_and_bind('set editing-mode emacs')

while True:
    try:
        line = input('enter something (EOF to quit): ')
    except EOFError:
        print()
        break
    print('ENTERED: "%s"' % line)


> You only need to import the readline module if you want to
> change the configuration.

That's not how it acts for me. I have to "import readline" to get
command line recall and editing.  The parse_and_bind, doesn't seem to
do anything -- emacs mode seems to be the default.

> Yes, it would be helpful if the docs for the readline module
> explained this. At present they seem to assume that you already
> know what the readline module is for and just want a summary
> of the API.
>
> It *is* mentioned briefly in the docs for input(), but again
> somebody wanting line editing functionality wouldn't necessarily
> think of looking there.





More information about the Python-list mailing list