8000 gh-73965: New environment variable PYTHON_HISTORY by ZackerySpytz · Pull Request #13208 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-73965: New environment variable PYTHON_HISTORY #13208

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

Merged
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions Doc/using/cmdline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,14 @@ conflict.

.. versionadded:: 3.13

.. envvar:: PYTHON_HISTORY

This environment variable can be used to set the location of a
``.python_history`` file (by default, it is ``.python_history`` in the
user's home directory).

.. versionadded:: 3.13

Debug-mode variables
~~~~~~~~~~~~~~~~~~~~

Expand Down
5 changes: 5 additions & 0 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ Other Language Changes
equivalent of the :option:`-X frozen_modules <-X>` command-line option.
(Contributed by Yilei Yang in :gh:`111374`.)

* The new :envvar:`PYTHON_HISTORY` environment variable can be used to change
the location of a ``.python_history`` file.
(Contributed by Levi Sabah, Zackery Spytz and Hugo van Kemenade in
:gh:`73965`.)

New Modules
===========

Expand Down
20 changes: 17 additions & 3 deletions Lib/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,20 @@ def setcopyright():
def sethelper():
builtins.help = _sitebuiltins._Helper()


def gethistoryfile():
"""Check if the PYTHON_HIST 8000 ORY environment variable is set and define
it as the .python_history file. If PYTHON_HISTORY is not set, use the
default .python_history file.
"""
if not sys.flags.ignore_environment:
history = os.environ.get("PYTHON_HISTORY")
if history:
return history
return os.path.join(os.path.expanduser('~'),
'.python_history')


def enablerlcompleter():
"""Enable default readline configuration on interactive prompts, by
registering a sys.__interactivehook__.
Expand Down Expand Up @@ -467,13 +481,13 @@ 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, default to .python_history,
# or PYTHON_HISTORY.
# 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 = gethistoryfile()
try:
readline.read_history_file(history)
except OSError:
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import unittest
import test.support
from test import support
from test.support.script_helper import assert_python_ok
from test.support import os_helper
from test.support import socket_helper
from test.support import captured_stderr
Expand Down Expand Up @@ -338,6 +339,19 @@ def test_no_home_directory(self):
mock_addsitedir.assert_not_called()
self.assertFalse(known_paths)

def test_gethistoryfile(self):
filename = 'file'
rc, out, err = assert_python_ok('-c',
f'import site; assert site.gethistoryfile() == "{filename}"',
PYTHON_HISTORY=filename)
self.assertEqual(rc, 0)

# Check that PYTHON_HISTORY is ignored in isolated mode.
rc, out, err = assert_python_ok('-I', '-c',
f'import site; assert site.gethistoryfile() != "{filename}"',
PYTHON_HISTORY=filename)
self.assertEqual(rc, 0)

def test_trace(self):
message = "bla-bla-bla"
for verbose, out in (True, message + "\n"), (False, ""):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add a new :envvar:`PYTHON_HISTORY` environment variable to set the location
of a ``.python_history`` file.
3 changes: 3 additions & 0 deletions Misc/python.man
67E6
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,9 @@ can be set to the callable of your debugger of choice.
.IP PYTHON_COLORS
If this variable is set to 1, the interpreter will colorize various kinds of
output. Setting it to 0 deactivates this behavior.
.IP PYTHON_HISTORY
This environment variable can be used to set the location of a history file
(on Unix, it is \fI~/.python_history\fP by default).
.SS Debug-mode variables
Setting these variables only has an effect in a debug build of Python, that is,
if Python was configured with the
Expand Down
1 change: 1 addition & 0 deletions Python/initconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ static const char usage_envvars[] =
"PYTHON_COLORS : If this variable is set to 1, the interpreter will"
" colorize various kinds of output. Setting it to 0 deactivates this behavior.\n"
"These variables have equivalent command-line parameters (see --help for details):\n"
"PYTHON_HISTORY: the location of a .python_history file.\n"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What command line parameter sets it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't have one, I compared with PYTHON_COLORS which doesn't either (#112732). Should we add one?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it doesn't have one, then it is in the wrong section.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, please see PR #113798 to move it to the correct section.

"PYTHONDEBUG : enable parser debug mode (-d)\n"
"PYTHONDONTWRITEBYTECODE : don't write .pyc files (-B)\n"
"PYTHONINSPECT : inspect interactively after running script (-i)\n"
Expand Down
0