Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f19b93f

Browse files
ZackerySpytzLevi Sabahhugovk
authored
gh-73965: New environment variable PYTHON_HISTORY (#13208)
It can be used to set the location of a .python_history file --------- Co-authored-by: Levi Sabah <[email protected]> Co-authored-by: Hugo van Kemenade <[email protected]>
1 parent 541c5db commit f19b93f

File tree

7 files changed

+50
-3
lines changed

7 files changed

+50
-3
lines changed

Doc/using/cmdline.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,14 @@ conflict.
11391139

11401140
.. versionadded:: 3.13
11411141

1142+
.. envvar:: PYTHON_HISTORY
1143+
1144+
This environment variable can be used to set the location of a
1145+
``.python_history`` file (by default, it is ``.python_history`` in the
1146+
user's home directory).
1147+
1148+
.. versionadded:: 3.13
1149+
11421150
Debug-mode variables
11431151
~~~~~~~~~~~~~~~~~~~~
11441152

Doc/whatsnew/3.13.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ Other Language Changes
132132
equivalent of the :option:`-X frozen_modules <-X>` command-line option.
133133
(Contributed by Yilei Yang in :gh:`111374`.)
134134

135+
* The new :envvar:`PYTHON_HISTORY` environment variable can be used to change
136+
the location of a ``.python_history`` file.
137+
(Contributed by Levi Sabah, Zackery Spytz and Hugo van Kemenade in
138+
:gh:`73965`.)
139+
135140
New Modules
136141
===========
137142

Lib/site.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,20 @@ def setcopyright():
433433
def sethelper():
434434
builtins.help = _sitebuiltins._Helper()
435435

436+
437+
def gethistoryfile():
438+
"""Check if the PYTHON_HISTORY environment variable is set and define
439+
it as the .python_history file. If PYTHON_HISTORY is not set, use the
440+
default .python_history file.
441+
"""
442+
if not sys.flags.ignore_environment:
443+
history = os.environ.get("PYTHON_HISTORY")
444+
if history:
445+
return history
446+
return os.path.join(os.path.expanduser('~'),
447+
'.python_history')
448+
449+
436450
def enablerlcompleter():
437451
"""Enable default readline configuration on interactive prompts, by
438452
registering a sys.__interactivehook__.
@@ -467,13 +481,13 @@ def register_readline():
467481
pass
468482

469483
if readline.get_current_history_length() == 0:
470-
# If no history was loaded, default to .python_history.
484+
# If no history was loaded, default to .python_history,
485+
# or PYTHON_HISTORY.
471486
# The guard is necessary to avoid doubling history size at
472487
# each interpreter exit when readline was already configured
473488
# through a PYTHONSTARTUP hook, see:
474489
# http://bugs.python.org/issue5845#msg198636
475-
history = os.path.join(os.path.expanduser('~'),
476-
'.python_history')
490+
history = gethistoryfile()
477491
try:
478492
readline.read_history_file(history)
479493
except OSError:

Lib/test/test_site.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import unittest
88
import test.support
99
from test import support
10+
from test.support.script_helper import assert_python_ok
1011
from test.support import os_helper
1112
from test.support import socket_helper
1213
from test.support import captured_stderr
@@ -338,6 +339,19 @@ def test_no_home_directory(self):
338339
mock_addsitedir.assert_not_called()
339340
self.assertFalse(known_paths)
340341

342+
def test_gethistoryfile(self):
343+
filename = 'file'
344+
rc, out, err = assert_python_ok('-c',
345+
f'import site; assert site.gethistoryfile() == "{filename}"',
346+
PYTHON_HISTORY=filename)
347+
self.assertEqual(rc, 0)
348+
349+
# Check that PYTHON_HISTORY is ignored in isolated mode.
350+
rc, out, err = assert_python_ok('-I', '-c',
351+
f'import site; assert site.gethistoryfile() != "{filename}"',
352+
PYTHON_HISTORY=filename)
353+
self.assertEqual(rc, 0)
354+
341355
def test_trace(self):
342356
message = "bla-bla-bla"
343357
for verbose, out in (True, message + "\n"), (False, ""):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add a new :envvar:`PYTHON_HISTORY` environment variable to set the location
2+
of a ``.python_history`` file.

Misc/python.man

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,9 @@ can be set to the callable of your debugger of choice.
604604
.IP PYTHON_COLORS
605605
If this variable is set to 1, the interpreter will colorize various kinds of
606606
output. Setting it to 0 deactivates this behavior.
607+
.IP PYTHON_HISTORY
608+
This environment variable can be used to set the location of a history file
609+
(on Unix, it is \fI~/.python_history\fP by default).
607610
.SS Debug-mode variables
608611
Setting these variables only has an effect in a debug build of Python, that is,
609612
if Python was configured with the

Python/initconfig.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ static const char usage_envvars[] =
296296
"PYTHON_COLORS : If this variable is set to 1, the interpreter will"
297297
" colorize various kinds of output. Setting it to 0 deactivates this behavior.\n"
298298
"These variables have equivalent command-line parameters (see --help for details):\n"
299+
"PYTHON_HISTORY: the location of a .python_history file.\n"
299300
"PYTHONDEBUG : enable parser debug mode (-d)\n"
300301
"PYTHONDONTWRITEBYTECODE : don't write .pyc files (-B)\n"
301302
"PYTHONINSPECT : inspect interactively after running script (-i)\n"

0 commit comments

Comments
 (0)