python / cpython

The Python programming language
https://www.python.org/
Other
60.78k stars 29.34k forks source link

The New REPL Does Not Load My Command History #120766

Open ericsnowcurrently opened 1 week ago

ericsnowcurrently commented 1 week ago

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:

CC @ambv, @pablogsal

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

pablogsal commented 1 week ago

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 commented 1 day ago

I have made a workaround for this issue: https://github.com/python/cpython/issues/121160#issuecomment-2198246116.

encukou commented 5 minutes ago

@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.