[Python-ideas] History stepping in interactive session?

Steven D'Aprano steve at pearwood.info
Fri Oct 5 16:24:39 CEST 2012

On 05/10/12 22:09, Antoine Pitrou wrote:
> On Fri, 05 Oct 2012 11:27:28 +0200
> Andy Buckley<andy at insectnation.org>  wrote:
>> A couple of weeks ago I posted a question on superuser.com about whether
>> there is a way to get the same *very* convenient
>> stepping-through-command-history behaviour in an interactive Python
>> interpreter session as is possible in (at least) the bash shell with the
>> Ctrl-o keybinding:
> The interactive interpreter (and I mean the default one, not
> third-party choices like IPython) uses libreadline for its
> editing and history functionality, so it's really a question about
> libreadline you're asking.

I don't think so. I'm not an expert on readline, but it seems to me to be a
Python bug.

In bash, I check for the existence of the "operate-and-get-next" command,
and sure enough it is bound to C-o (Ctrl-o) as expected:

[steve at ando ~]$ bind -p | grep operate
"\C-o": operate-and-get-next

I don't believe that there is any direct mechanism for querying the current
readline bindings in Python, but I can fake it with the "dump-functions"

import readline
readline.parse_and_bind(r'"\C-xd": dump-functions')

If I then type Ctrl-x d at the interactive interpreter, readline dumps the
function bindings to screen:

py> readline.parse_and_bind(r'"\C-xd": dump-functions')

abort can be found on "\C-g", "\C-x\C-g", "\M-\C-g".
accept-line can be found on "\C-j", "\C-m".
arrow-key-prefix is not bound to any keys
backward-byte is not bound to any keys
backward-char can be found on "\C-b", "\M-OD", "\M-[D".

operate-and-get-next is absent from the list. I don't mean that it is
not bound. It just isn't there at all.

If I nevertheless try to use it:

readline.parse_and_bind(r'"\C-o": operate-and-get-next')

it does *not* enable Ctrl-o as expected, operate-and-get-next remains
absent from the list of bindings.

I have checked this on both Python 2.7 and 3.3.0rc3 under Centos 5,
and on 3.3.0rc3 under Debian Squeeze.


More information about the Python-ideas mailing list