The Wayback Machine - https://web.archive.org/web/20220513114948/https://github.com/python/cpython/pull/13208/files
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

bpo-29779: New environment variable PYTHONHISTORY #13208

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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
~~~~~~~~~~~~~~~~~~~~
@@ -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
===========
@@ -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__.
@@ -428,13 +440,13 @@ def register_readline():
pass

if readline.get_current_history_length() == 0:
# If no history was loaded, default to .python_history.
ZackerySpytz marked this conversation as resolved.
Show resolved Hide resolved
# 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:
@@ -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
@@ -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"""
@@ -0,0 +1,2 @@
Add a new :envvar:`PYTHONHISTORY` environment variable to set the location
of a ``.python_history`` file.
@@ -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
@@ -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}"