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 1 commit
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
7 changes: 7 additions & 0 deletions Doc/using/cmdline.rst
8000
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,13 @@ 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 (it is ``~/.python_history`` by default).

.. 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 @@ -206,6 +206,9 @@ Other Language Changes
and Windows use this to properly terminate scripts in interactive sessions.
(Contributed by Google via Gregory P. Smith in :issue:`1054041`.)

* 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
8000
21 changes: 14 additions & 7 deletions Lib/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,17 @@ 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.
"""
h = os.environ.get("PYTHONHISTORY")
if h:
return h
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 +439,9 @@ def register_readline():
pass

if readline.get_current_history_length() == 0:
# If no history was loaded, default to .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')
# If no history was loaded, default to .python_history,
# or PYTHONHISTORY.
history = gethistoryfile()
try:
readline.read_history_file(history)
except OSError:
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,11 @@ def test_no_home_directory(self):
mock_addsitedir.assert_not_called()
self.assertFalse(known_paths)

def test_gethistoryfile(self):
with EnvironmentVarGuard() as environ:
environ['PYTHONHISTORY'] = 'xoxo'
self.assertEqual(site.gethistoryfile(), "xoxo")


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
(it is \fI~/.python_history\fP by default).
Copy link
Member
@pablogsal pablogsal May 10, 2019

Choose a reason for hiding this comment

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

Please, add the windows default as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To be frank, I'm not sure if the Windows default should be mentioned in the man page...

Copy link

Choose a reason for hiding this comment

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

There is nothing wrong with mentioning Windows ops in man pages.

.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/coreconfig.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
0