Update pythonrc

This commit is contained in:
ItsDrike 2023-10-10 21:32:38 +02:00
parent 18252755b8
commit 77a8cced84
Signed by: ItsDrike
GPG key ID: FA2745890B7048C0

View file

@ -1,7 +1,9 @@
import atexit
import os
import readline
from functools import partial
from pathlib import Path
from types import ModuleType
cache_xdg_dir = Path(os.environ.get("XDG_CACHE_HOME", str(Path("~/.cache"))))
cache_xdg_dir.mkdir(exist_ok=True, parents=True)
@ -11,8 +13,13 @@ history_file = cache_xdg_dir.joinpath("python_history")
readline.read_history_file(history_file)
def write_history() -> None:
def write_history(readline: ModuleType, history_file: Path) -> None:
"""
We need to get ``readline`` and ``history_file`` as arguments, as it
seems they get garbage collected when the function is registered and
the program ends, even though we refer to them here.
"""
readline.write_history_file(history_file)
atexit.register(write_history)
atexit.register(partial(write_history, readline, history_file))