Skip to content

Python 3.13.0b1 REPL changes behavior wrt PgUp, inserts first line of ~/.python_history #119034

Description

@hroncok

Bug report

Bug description:

When I use the Python REPL, I am used to PgUp browsing my prompt history based on the partial command I already typed.

Consider:

>>> import sys
>>> import os
>>> ...
>>> im[PgUp]

Python 3.12 REPL inserts import os with my cursor between m and p. I can keep pressing PgUp to get import sys and older commands from my history. I don't know if this is Fedora's configuration of readline, or the default. However, Bash and older Python REPLs behave that way, as well as IPython/Jupyter console.

Python 3.130b1 REPL changes my prompt to print("a") when I press PgUp. The particular command is my first line of ~/.python_history. Pressing PgUp again changes nothing.

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Linked PRs

Activity

  1. changed the title [-]Python 3.13.0b1 REPL chnages behavior wrt PgUp, inserts first line of ~/.python_history[/-] [+]Python 3.13.0b1 REPL changes behavior wrt PgUp, inserts first line of ~/.python_history[/+] on May 14, 2024
  2. added
    topic-replRelated to the interactive shell
    3.13only security fixes
    3.14bugs and security fixes
    on May 14, 2024
  3. treyhunner commented on May 20, 2024

    @treyhunner
    Member

    Was talking to @ambv about this and decided to do some research on Ubuntu and Fedora's settings.

    I did some research on the default /etc/inputrc settings on Ubuntu and Fedora and found these files as defaults.

    Digging into the details of those, here's what's missing...

    Consistencies

    Here are some seemingly missing key bindings that seem to be consistently mapped between Ubuntu and Fedora's default /etc/inputrc:

    "\e[1;5C": forward-word
    "\e[1;5D": backward-word
    "\e[5C": forward-word
    "\e[5D": backward-word
    "\e\e[C": forward-word
    "\e\e[D": backward-word
    
    • Ctrl + Right Arrow should go forward one word
    • Alt + Right Arrow should go forward one word
    • Ctrl + Left Arrow should go backward one word
    • Alt + Left Arrow should go backward one word

    Both Ubuntu and Fedora map those arrow shortcuts.

    Only Fedora maps these ones:

    "\e[5~": history-search-backward
    "\e[6~": history-search-forward
    
    • Page Up should search search backward through command history
    • Page Down should search search forward through command history

    Since Ubuntu doesn't map these at all, it seems safe to enable these.

    Inconsistent

    Ubuntu has:

    "\e[2~": quoted-insert
    

    That maps Insert and Ctrl-V to allow a literal interpretation of the next key/character (e.g. Ctrl-V Tab should insert a literal Tab character).

    Fedora has:

    "\e[2~": overwrite-mode
    

    That maps Insert and Ctrl-V to enable overwrite mode instead.

    Summary

    The missing keybindings that should probably be added are:

    • Ctrl-Left and Alt-Left: back one word
    • Ctrl-Right and Alt-Left: forward one word
    • Page Up: search history backward
    • Page Down: search history forward
  4. hroncok commented on May 21, 2024

    @hroncok
    ContributorAuthor

    I wonder if the REPL could somehow inherit/use the inputrc settings. That way, the experience will be the same with other REPLs on the same system.

    (I am not saying that is a requirement. I would gladly accept a fix that makes my environment consistent.)

  5. ambv commented on May 21, 2024

    @ambv
    Contributor

    Thanks Trey for the research, we will be implementing that.

    Miro, we won't be parsing inputrc, it is too advanced of a configuration format to accommodate. And it entails out-of-scope things like an vi edit mode that I already rejected. And on macOS we would have to also take editrc into account, which is yet another format, and also overly complex.

    We will implement the safe defaults and keep it at that. People very invested in their inputrc can keep using PYTHON_BASIC_REPL as this one is not getting removed.

  6. hroncok commented on May 21, 2024

    @hroncok
    ContributorAuthor

    For the record, I found Fedora's inputr source at https://pagure.io/setup/blob/master/f/inputrc -- in case somebody wants to take a look without spinning up a container/VM.

  7. hroncok commented on May 21, 2024

    @hroncok
    ContributorAuthor

    The quoted-insert vs overwrite-mode inconsistency is explained in the commit message of https://pagure.io/setup/c/b8ba4814b55da50c0946dbb5b1f72e8ba452c49e

  8. treyhunner commented on May 28, 2024

    @treyhunner
    Member

    I think Ctrl-V should also be specially handled/ignored, since it didn't cause issues in the old Python REPL but does in the new REPL currently.

    If I type print(n^V) into the new REPL, where ^V is Ctrl-V, an invalid character will be input, which will result in a SyntaxError.

    Here's an example (I pressed Ctrl-V just before the final ) in the print line below:

    >>> numbers = [2, 1, 3, 4, 7, 11]
    >>> for n in numbers:
    ...     print(n)
      File "<unknown>", line 2
        print(n)
               ^
    SyntaxError: invalid non-printable character U+0016

    I also managed to insert a null byte while teaching in the new Python REPL today.
    I assume I hit a keyboard combination that did this, but I'm not sure which one.

    >>> names = ['Nellie', 'Ronald', 'Judith', 'Lavonda']
    >>> for i in range(len(name)):
    SyntaxError: source code string cannot contain null bytes

    I'll comment here or open a separate issue if I manage to reproduce the null byte I inserted.

  9. hroncok commented on Jul 21, 2024

    @hroncok
    Author
  10. hroncok commented on Aug 2, 2024

    @hroncok
    ContributorAuthor

    Thanks Trey for the research, we will be implementing that.

    @ambv Is this something I should explore to implement myself if I want this fixed?

  11. ambv commented on Aug 2, 2024

    @ambv
    Contributor

    There's an open PR about this in the duplicate issue gh-120767.

  12. ambv commented on Aug 2, 2024

    @ambv
    Contributor

    We haven't gotten to that for RC1 but should be there in RC2.

  13. hroncok commented on Aug 6, 2024

    @hroncok
    ContributorAuthor

    #120767 does not seem like a duplicate to me. It speaks about arrows up and down, not PageUp/PageDown. As does #121859

  14. vstinner commented on Sep 2, 2024

    @vstinner
    Member

    Workaround for that: use CTRL+R.

    Consider:

    >>> import sys
    >>> import os
    >>> [CTRL+R] im
    

    I'm using a different shortcut for that which works on bash and all Python versions:

    • CTRL+R to search in the history
    • Type im
    • Press CTRL+R again to iterate in results starting with im

    There is also CTRL+S to search forward in the history.

  15. hroncok commented on Sep 2, 2024

    @hroncok
    ContributorAuthor

    Thanks, I am aware of Ctrl+r. The PageUp thing is better when I already strated to type the command. It's really convenient and not having it makes my REPL experience in 3.13 seriously degraded.

    Omce again, should I submit a PR? I don't want to step on other people toes, but it's been a while and the window to get this into 3.13 is closing.

  16. added 3 commits that reference this issue on Sep 2, 2024
  17. vstinner commented on Sep 2, 2024

    @vstinner
    Member

    Omce again, should I submit a PR? I don't want to step on other people toes, but it's been a while and the window to get this into 3.13 is closing.

    I just wrote PR gh-123607 to implement these commands (history search backward/forward). Can you please test it and tell me if it behaves as expected?

  18. added a commit that references this issue on Sep 6, 2024
  19. added a commit that references this issue on Sep 6, 2024
  20. added a commit that references this issue on Sep 6, 2024
  21. added a commit that references this issue on Jul 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13only security fixes3.14bugs and security fixestopic-replRelated to the interactive shelltype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions