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

Make PYTHONSTARTUP script respect PYTHON_HISTORY on Python <3.13 #433

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion programs/python.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"files": [
{
"help": "If you're running python v3.13.0a3 or later, you can simply set the `PYTHON_HISTORY` environment variable.\n\nOtherwise, export the following environment variable:\n\n```bash\nexport PYTHONSTARTUP=\"$XDG_CONFIG_HOME\"/python/pythonrc\n```\n\nThen create the file _$XDG_CONFIG_HOME/python/pythonrc_, and put the following code into it:\n\n```python\n#!/usr/bin/env python3\n# This entire thing is unnecessary post v3.13.0a3\n# https://github.com/python/cpython/issues/73965\n\ndef is_vanilla() -> bool:\n \"\"\" :return: whether running \"vanilla\" Python \"\"\"\n import sys\n return not hasattr(__builtins__, '__IPYTHON__') and 'bpython' not in sys.argv[0]\n\n\ndef setup_history():\n \"\"\" read and write history from state file \"\"\"\n import os\n import atexit\n import readline\n from pathlib import Path\n\n # https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables\n if state_home := os.environ.get('XDG_STATE_HOME'):\n state_home = Path(state_home)\n else:\n state_home = Path.home() / '.local' / 'state'\n if not state_home.is_dir():\n print(\"Error: XDG_STATE_HOME does not exist at\", state_home)\n\n history: Path = state_home / 'python_history'\n\n # https://github.com/python/cpython/issues/105694\n if not history.is_file():\n readline.write_history_file(str(history)) # breaks on macos + python3 without this.\n\n readline.read_history_file(history)\n atexit.register(readline.write_history_file, history)\n\n\nif is_vanilla():\n setup_history()\n```\n",
"help": "If you're running python v3.13.0a3 or later, you can simply set the `PYTHON_HISTORY` environment variable.\n\nOtherwise, export the following environment variable:\n\n```bash\nexport PYTHONSTARTUP=\"$XDG_CONFIG_HOME\"/python/pythonrc\n```\n\nThen create the file _$XDG_CONFIG_HOME/python/pythonrc_, and put the following code into it:\n\n```python\n#!/usr/bin/env python3\n# This entire thing is unnecessary post v3.13.0a3\n# https://github.com/python/cpython/issues/73965\n\ndef is_vanilla() -> bool:\n \"\"\" :return: whether running \"vanilla\" Python <3.13 \"\"\"\n import sys\n return not hasattr(__builtins__, '__IPYTHON__') and 'bpython' not in sys.argv[0] and\nsys.version_info < (3, 13)\n\n\ndef setup_history():\n \"\"\" read and write history from state file \"\"\"\n import os\n import atexit\n import readline\n from pathlib import Path\n\n # Check PYTHON_HISTORY for future-compatibility with Python 3.13\n if history := os.environ.get('PYTHON_HISTORY'):\n history = Path(history)\n # https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables\n elif state_home := os.environ.get('XDG_STATE_HOME'):\n state_home = Path(state_home)\n else:\n state_home = Path.home() / '.local' / 'state'\n\n history: Path = history or state_home / 'python_history'\n\n # https://github.com/python/cpython/issues/105694\n if not history.is_file():\n readline.write_history_file(str(history)) # breaks on macos + python3 without this.\n\n readline.read_history_file(history)\n atexit.register(readline.write_history_file, history)\n\n\nif is_vanilla():\n setup_history()\n```\n",
"movable": true,
"path": "$HOME/.python_history"
}
Expand Down
Loading