Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The New REPL Does Not Load My Command History #120766

Open
ericsnowcurrently opened this issue Jun 19, 2024 · 5 comments
Open

The New REPL Does Not Load My Command History #120766

ericsnowcurrently opened this issue Jun 19, 2024 · 5 comments
Assignees
Labels
3.13 bugs and security fixes 3.14 new features, bugs and security fixes topic-repl Related to the interactive shell type-bug An unexpected behavior, bug, or error

Comments

@ericsnowcurrently
Copy link
Member

ericsnowcurrently commented Jun 19, 2024

Bug report

Bug description:

In my ~/.pystartup I have the following code:

import atexit
import os
import readline
import rlcompleter

historyPath = os.path.expanduser("~/.pyhistory")

def save_history(historyPath=historyPath):
    import readline
    readline.write_history_file(historyPath)

if os.path.exists(historyPath):
    readline.read_history_file(historyPath)

atexit.register(save_history)
del os, atexit, readline, rlcompleter, save_history, historyPath

This is something I've been using for years. With this startup file, I can enter the REPL and immediately up-arrow to scroll through the commands I used the last time I used the REPL.

With the new REPL there are two related things going wrong:

  • my command history is not loaded (when I immediately up-arrow, it does nothing)
  • my command history is not saved in my .pyhistory file (the file is touched but not otherwise modified)

CC @ambv, @pablogsal

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

@ericsnowcurrently ericsnowcurrently added type-bug An unexpected behavior, bug, or error 3.13 bugs and security fixes 3.14 new features, bugs and security fixes topic-repl Related to the interactive shell labels Jun 19, 2024
@pablogsal
Copy link
Member

Hummm, it looks like this code it's using readline to handle the story but the new REPL doesn't use readline at all so this will never work. Notice that this will also not work the same if you compile Python against libedit instead of readline because it has different history handling (to the point I know).

@ambv what do you think? Maybe we can monkey patch readline.read_history_file somehow in the new REPL but I am concerned of the side effects.

@whitequark
Copy link

I have made a workaround for this issue: #121160 (comment).

@encukou
Copy link
Member

encukou commented Jul 1, 2024

@whitequark, AFAIK that's a different issue.
The new REPL noes not use the readline Python module at all. readline.read_history_file won't affect it, regardless of the backend.

@domdfcoding
Copy link
Contributor

I'm seeing a possibly related issue but without a startup file. The first time Python 3.13 runs the history is there, but after exiting and running again the history has gone. Running an older Python populates the history with new entries but they're lost the next time Python 3.13 exits.

Git bisect tracked the issue to 38cfa92. The actual issue is in site.py, where it tries to import CAN_USE_PYREPL from __main__, which it's no longer located in as of that commit.

cpython/Lib/site.py

Lines 527 to 532 in c7991cc

def write_history():
try:
# _pyrepl.__main__ is executed as the __main__ module
from __main__ import CAN_USE_PYREPL
except ImportError:
CAN_USE_PYREPL = False

Changing this to from _pyrepl.main import CAN_USE_PYREPL or adding from .main import CAN_USE_PYREPL to the top of _pyrepl.__main__.py seems to fix it. (I couldn't make it work reliably at first but I think that was me forgetting that site.py is frozen)

@hroncok
Copy link
Contributor

hroncok commented Jul 1, 2024

@domdfcoding As this does not require a startup file, I belive it's a different (and more severe issue). I opened #121245

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes 3.14 new features, bugs and security fixes topic-repl Related to the interactive shell type-bug An unexpected behavior, bug, or error
7 participants