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

readline error when saving interactive command history when history file path is a symlink to a relative path #129453

Open
kailizcatman opened this issue Jan 29, 2025 · 2 comments
Labels
extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error

Comments

@kailizcatman
Copy link

kailizcatman commented Jan 29, 2025

Bug report

Bug description:

To reproduce the issue

  1. Set up home directory like following:
$HOME/
  .python_history --> foo/history_file
  foo/
    history_file
  1. Launch a Python interactive session with CWD different from $HOME
  2. Execute the following
import readline
import os
history_path=os.path.expanduser("~/.python_history")
assert os.path.exists(history_path)
readline.write_history_file(history_path)
  1. Observe the following error
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory

Possible cause

This is likely due to how GNU readline commits history information to file. As of GNU readline v8.2 (used in Python 3.10 and 3.12 among others), it creates a temporary file in the parent directory of the history file, write to that temporary file, and then rename the temporary file to the history file. See history_tempfile in histfile.c of GNU readline.

GNU readline determines the parent directory from the readlink output of the user-supplied history file path. In this case, since the history file is a symlink with a relative path, the parent directory is resolved to the relative path foo/. If foo/ doesn't exist in the CWD, history saving fails.

CPython versions tested on:

3.10, 3.12

Operating systems tested on:

Linux

@kailizcatman kailizcatman added the type-bug An unexpected behavior, bug, or error label Jan 29, 2025
@ZeroIntensity
Copy link
Member

This looks like a problem in GNU upstream. Is there any conceivable workaround?

@kailizcatman
Copy link
Author

What do you think about passing the realpath of the user-supplied history file path to GNU readline?

@encukou encukou added the extension-modules C modules in the Modules dir label Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants