From dc505f9e9a7a1c4863f0193156bdde9bcfdf8c36 Mon Sep 17 00:00:00 2001 From: oxalica Date: Wed, 26 May 2021 19:10:52 +0800 Subject: [PATCH 1/3] bpo-44239: Use platform defined data directories instead of ~/.python_history --- Doc/library/site.rst | 16 +++++++-- Doc/tutorial/interactive.rst | 6 +++- Lib/site.py | 36 ++++++++++++++++--- .../2021-05-26-19-13-49.bpo-44239.AN0IcD.rst | 1 + 4 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2021-05-26-19-13-49.bpo-44239.AN0IcD.rst diff --git a/Doc/library/site.rst b/Doc/library/site.rst index e2ad3c48f9754e..a0b581d0ba8393 100644 --- a/Doc/library/site.rst +++ b/Doc/library/site.rst @@ -144,15 +144,25 @@ Readline configuration On systems that support :mod:`readline`, this module will also import and configure the :mod:`rlcompleter` module, if Python is started in :ref:`interactive mode ` and without the :option:`-S` option. -The default behavior is enable tab-completion and to use -:file:`~/.python_history` as the history save file. To disable it, delete (or -override) the :data:`sys.__interactivehook__` attribute in your +The default behavior is enable tab-completion and to use history file from +platform defined directory. +For Windows, it is :file:`%APPDATA%\\Python\\history`; +for Mac OS, it is :file:`~/Library/Application Support/Python/history`; +for other POSIX platforms, it is :file:`$XDG_STATE_HOME/python/history` or +:file:`~/.local/state/python/history`; +otherwise, it is :file:`~/.python_history`. +Note that for compatibility, if :file:`~/.python_history` is readable, it will +always be taken as default and other paths are ignored. To disable it, delete +(or override) the :data:`sys.__interactivehook__` attribute in your :mod:`sitecustomize` or :mod:`usercustomize` module or your :envvar:`PYTHONSTARTUP` file. .. versionchanged:: 3.4 Activation of rlcompleter and history was made automatic. +.. versionchanged:: 3.11 + Prefer platform defined data directory instead of `~/.python_history`. + Module contents --------------- diff --git a/Doc/tutorial/interactive.rst b/Doc/tutorial/interactive.rst index c0eb1feec4eb4d..208421a8c303d2 100644 --- a/Doc/tutorial/interactive.rst +++ b/Doc/tutorial/interactive.rst @@ -25,7 +25,11 @@ the expression up to the final ``'.'`` and then suggest completions from the attributes of the resulting object. Note that this may execute application-defined code if an object with a :meth:`__getattr__` method is part of the expression. The default configuration also saves your -history into a file named :file:`.python_history` in your user directory. +history into a file under platform defined data directory ( +:file:`%APPDATA%\\Python\\history` for Windows; +:file:`~/Library/Application Support/Python/history` for Mac OS; +:file:`$XDG_STATE_HOME/python/history` or +:file:`~/.local/state/python/history` for other POSIX platforms like Linux). The history will be available again during the next interactive interpreter session. diff --git a/Lib/site.py b/Lib/site.py index 939893eb5ee93b..7bdc2333f00642 100644 --- a/Lib/site.py +++ b/Lib/site.py @@ -436,7 +436,7 @@ def enablerlcompleter(): registering a sys.__interactivehook__. If the readline module can be imported, the hook will set the Tab key - as completion key and register ~/.python_history as history file. + as completion key and register history file. This can be overridden in the sitecustomize or usercustomize module, or in a PYTHONSTARTUP file. """ @@ -466,14 +466,18 @@ def register_readline(): pass if readline.get_current_history_length() == 0: - # If no history was loaded, default to .python_history. + # If no history was loaded, load history file from + # platform defined directories. # The guard is necessary to avoid doubling history size at # each interpreter exit when readline was already configured # through a PYTHONSTARTUP hook, see: # http://bugs.python.org/issue5845#msg198636 - history = os.path.join(os.path.expanduser('~'), - '.python_history') + + history = get_readline_history_path() + history = os.path.abspath(history) try: + _dir, _ = os.path.split(history) + os.makedirs(_dir, exist_ok=True) readline.read_history_file(history) except OSError: pass @@ -488,6 +492,30 @@ def write_history(): atexit.register(write_history) + def get_readline_history_path(): + def joinuser(*args): + return os.path.expanduser(os.path.join(*args)) + + # If the legacy path "~/.python_history" is readable, always use it. + legacy = joinuser('~', '.python_history') + if os.access(legacy, os.R_OK): + return legacy + + # Otherwise, use platform defined data directory. + if os.name == 'nt': + base = os.environ.get('APPDATA') or '~' + return joinuser(base, 'Python', 'history') + + if sys.platform == 'darwin': + return joinuser('~', 'Library', 'Application Support', 'Python', 'history') + + if os.name == 'posix': + base = os.environ.get('XDG_STATE_HOME') or joinuser('~', '.local', 'state') + return joinuser(base, 'python', 'history') + + # Unknown platform, use the legacy path. + return legacy + sys.__interactivehook__ = register_readline def venv(known_paths): diff --git a/Misc/NEWS.d/next/Library/2021-05-26-19-13-49.bpo-44239.AN0IcD.rst b/Misc/NEWS.d/next/Library/2021-05-26-19-13-49.bpo-44239.AN0IcD.rst new file mode 100644 index 00000000000000..966aab117a2829 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-05-26-19-13-49.bpo-44239.AN0IcD.rst @@ -0,0 +1 @@ +Use platform defined data directories instead of :file:`~/.python_history` From 9240a0bfdcd537924d4076a1676263497873185a Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Mon, 5 Jun 2023 10:09:57 +0400 Subject: [PATCH 2/3] 3.11 -> 3.13 --- Doc/library/site.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/site.rst b/Doc/library/site.rst index 5d4cb5be288ff6..4bc5d79f3357aa 100644 --- a/Doc/library/site.rst +++ b/Doc/library/site.rst @@ -160,7 +160,7 @@ always be taken as default and other paths are ignored. To disable it, delete .. versionchanged:: 3.4 Activation of rlcompleter and history was made automatic. -.. versionchanged:: 3.11 +.. versionchanged:: 3.13 Prefer platform defined data directory instead of `~/.python_history`. From dbb6ccdcb079efcf3e0e566323e4858b81b66c8b Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Mon, 5 Jun 2023 10:14:46 +0400 Subject: [PATCH 3/3] Fix the Check Documentation failed check --- Doc/library/site.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/site.rst b/Doc/library/site.rst index 4bc5d79f3357aa..0996f6f5f2ee96 100644 --- a/Doc/library/site.rst +++ b/Doc/library/site.rst @@ -161,7 +161,7 @@ always be taken as default and other paths are ignored. To disable it, delete Activation of rlcompleter and history was made automatic. .. versionchanged:: 3.13 - Prefer platform defined data directory instead of `~/.python_history`. + Prefer platform defined data directory instead of :file:`~/.python_history`. Module contents pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy