Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -918,6 +918,14 @@ conflict.
.. versionadded:: 3.7
See :pep:`540` for more details.

.. envvar:: PYTHONHISTORY

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.8


Debug-mode variables
~~~~~~~~~~~~~~~~~~~~
Expand Down
3 changes: 3 additions & 0 deletions Doc/whatsnew/3.8.rst
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,9 @@ Other Language Changes
* Added new ``replace()`` method to the code type (:class:`types.CodeType`).
(Contributed by Victor Stinner in :issue:`37032`.)

* The new :envvar:`PYTHONHISTORY` environment variable can be used to change
the location of a ``.python_history`` file.
(Contributed by Levi Sabah and Zackery Spytz in :issue:`29779`.)

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

def gethistoryfile():
"""Check if the PYTHONHISTORY environment variable is set and define
it as the .python_history file. If PYTHONHISTORY is not set, use the
default .python_history file.
"""
if not sys.flags.ignore_environment:
history = os.environ.get("PYTHONHISTORY")
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 @@ -428,13 +440,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 PYTHONHISTORY.
# 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 @@ -9,6 +9,7 @@
from test import support
from test.support import (captured_stderr, TESTFN, EnvironmentVarGuard,
change_cwd)
from test.support.script_helper import assert_python_ok
import builtins
import os
import sys
Expand Down Expand Up @@ -311,6 +312,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}"',
PYTHONHISTORY=filename)
self.assertEqual(rc, 0)

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


class PthFile(object):
"""Helper class for handling testing of .pth files"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add a new :envvar:`PYTHONHISTORY` environment variable to set the location
of a ``.python_history`` file.
3 changes: 3 additions & 0 deletions Misc/python.man
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,9 @@ show how long each import takes. This is exactly equivalent to setting
.IP PYTHONBREAKPOINT
If this environment variable is set to 0, it disables the default debugger. It
can be set to the callable of your debugger of choice.
.IP PYTHONHISTORY
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
3 changes: 2 additions & 1 deletion Python/initconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ static const char usage_6[] =
"PYTHONBREAKPOINT: if this variable is set to 0, it disables the default\n"
" debugger. It can be set to the callable of your debugger of choice.\n"
"PYTHONDEVMODE: enable the development mode.\n"
"PYTHONPYCACHEPREFIX: root directory for bytecode cache (pyc) files.\n";
"PYTHONPYCACHEPREFIX: root directory for bytecode cache (pyc) files.\n"
"PYTHONHISTORY: the location of a .python_history file.\n";

#if defined(MS_WINDOWS)
# define PYTHONHOMEHELP "<prefix>\\python{major}{minor}"
Expand Down