Baffled by readline module

Grant Edwards grant.b.edwards at gmail.com
Fri Mar 10 12:12:41 EST 2023


On 2023-03-10, Weatherby,Gerard <gweatherby at uchc.edu> wrote:

> On our Linux systems, I can up-arrow to go back to prior commands
> and use the left and right arrows to navigate a line. The
> functionality may be provided internally by readline. I’ve never had
> to dig into it because it meets my needs out of the box.

Apparently the cmd.Cmd docs are wrong. It says:

      If the readline module is loaded, input will automatically
      inherit bash-like history-list editing (e.g. Control-P scrolls
      back to the last command, Control-N forward to the next one,
      Control-F moves the cursor to the right non-destructively,
      Control-B moves the cursor to the left non-destructively, etc.).

On my Python 3.10.10 Linux system, cmd.Com itself is importing the
readline module unconditionally when I call cmdloop(). There's no 'if'
about it.

[It's moot for my current usage, because my application allows
multiple commands per input line, and I don't see how cmd.Cmd can
support that.]

Here's my application that seem to show that cmd.Cmd.cmdloop() is
importing the readline module:

-------------------------------testit.py--------------------------------
import sys
print('readline' in sys.modules)
import cmd
print('readline' in sys.modules)

class MyShell(cmd.Cmd):
    intro = 'Welcome to my shell.   Type help or ? to list commands.\n'
    prompt = '(what) '
    file = None

    def do_what(self,arg):
        print('readline' in sys.modules)
    def do_bye(self, arg):
        print('Good bye')
        return True

print('readline' in sys.modules)

if __name__ == '__main__':
    print('readline' in sys.modules)
    MyShell().cmdloop()
------------------------------------------------------------------------

And here's what I see when I run it:

------------------------------------------------------------------------
$ python testit.py
False
False
False
False
Welcome to my shell.   Type help or ? to list commands.

(what) what
True
(what) bye
Good bye
$
------------------------------------------------------------------------




More information about the Python-list mailing list