From c127f593b4c0ee51968c14b9a5f99ab5185f0633 Mon Sep 17 00:00:00 2001 From: Peter Vacho Date: Mon, 22 Sep 2025 09:29:17 +0200 Subject: [PATCH] Update pythonrc --- home/.config/python/pythonrc.py | 39 +++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/home/.config/python/pythonrc.py b/home/.config/python/pythonrc.py index bf6051f..3c6cf66 100644 --- a/home/.config/python/pythonrc.py +++ b/home/.config/python/pythonrc.py @@ -1,29 +1,44 @@ +#!/usr/bin/env python3 +# This entire thing is unnecessary post v3.13.0a3 +# https://github.com/python/cpython/issues/73965 + +import os +import sys +import atexit +import readline +from pathlib import Path + + def is_vanilla() -> bool: - import sys - - return not hasattr(__builtins__, "__IPYTHON__") and "bpython" not in sys.argv[0] + """Check whether this is a vanilla Python interpreter below 3.13.""" + return ( + not hasattr(__builtins__, "__IPYTHON__") + and "bpython" not in sys.argv[0] + and sys.version_info < (3, 13) + ) -def setup_history(): - import os - import atexit - import readline - from pathlib import Path +def setup_history() -> None: + """Read and write history from state file.""" + # Check PYTHON_HISTORY for future-compatibility with Python 3.13 + if history := os.environ.get("PYTHON_HISTORY"): + history = Path(history) - if state_home := os.environ.get("XDG_STATE_HOME"): + # https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables + elif state_home := os.environ.get("XDG_STATE_HOME"): state_home = Path(state_home) else: state_home = Path.home() / ".local" / "state" - history: Path = state_home / "python_history" + history: Path = history or state_home / "python_history" # https://github.com/python/cpython/issues/105694 if not history.is_file(): # breaks on macos + python3 without this. readline.write_history_file(str(history)) - readline.read_history_file(str(history)) - atexit.register(readline.write_history_file, str(history)) + readline.read_history_file(history) + atexit.register(readline.write_history_file, history) if is_vanilla():