Files
dotfiles/.config/python/pythonrc
Julian Prein 2ac0e01766 python: Handle non-existing history file
Create a dummy entry in the history file if it does not exist yet or is
empty. Otherwise `~/.python_history` would be used by site.py.
2022-07-12 21:00:11 +02:00

34 lines
840 B
Python

# vim: ft=python
import os
from os.path import expanduser as expanduser
from os.path import join as pjoin
import atexit
import readline
# Use ${XDG_DATA_HOME:-~/.local/share}/python/python_history instead of
# ~/.python_history
xdg_data = os.getenv('XDG_DATA_HOME', expanduser(pjoin('~' '.local', 'share')))
history = pjoin(xdg_data, 'python')
os.makedirs(history, exist_ok=True)
history = pjoin(history, 'python_history')
try:
readline.read_history_file(history)
except OSError:
pass
# Create dummy entry if the file does not yet exist, as otherwise
# ~/.python_history is used by site.py
if not readline.get_current_history_length():
readline.add_history("# python history file")
def write_history():
try:
readline.write_history_file(history)
except OSError:
pass
atexit.register(write_history)