Update pythonrc

This commit is contained in:
Peter Vacho 2025-09-22 09:29:17 +02:00
parent 4d14ebe072
commit c127f593b4
No known key found for this signature in database
GPG key ID: 00ACA0D6AF712EC9

View file

@ -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: def is_vanilla() -> bool:
import sys """Check whether this is a vanilla Python interpreter below 3.13."""
return (
return not hasattr(__builtins__, "__IPYTHON__") and "bpython" not in sys.argv[0] not hasattr(__builtins__, "__IPYTHON__")
and "bpython" not in sys.argv[0]
and sys.version_info < (3, 13)
)
def setup_history(): def setup_history() -> None:
import os """Read and write history from state file."""
import atexit # Check PYTHON_HISTORY for future-compatibility with Python 3.13
import readline if history := os.environ.get("PYTHON_HISTORY"):
from pathlib import Path 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) state_home = Path(state_home)
else: else:
state_home = Path.home() / ".local" / "state" 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 # https://github.com/python/cpython/issues/105694
if not history.is_file(): if not history.is_file():
# breaks on macos + python3 without this. # breaks on macos + python3 without this.
readline.write_history_file(str(history)) readline.write_history_file(str(history))
readline.read_history_file(str(history)) readline.read_history_file(history)
atexit.register(readline.write_history_file, str(history)) atexit.register(readline.write_history_file, history)
if is_vanilla(): if is_vanilla():