diff --git a/Doc/using/cmdline.rst b/Doc/using/cmdline.rst index 08dc311d837be9..cc2b3787ab566a 100644 --- a/Doc/using/cmdline.rst +++ b/Doc/using/cmdline.rst @@ -713,6 +713,13 @@ conflict. .. versionadded:: 3.6 +.. envvar:: PYTHONHISTORY + + If set to a non-empty string, you can change the location of a python_history + file, by default it will be in ~/.python_history. + + .. versionadded:: 3.7 + Debug-mode variables ~~~~~~~~~~~~~~~~~~~~ diff --git a/Lib/site.py b/Lib/site.py index 4f96ca91170c54..6ca3676910a3be 100644 --- a/Lib/site.py +++ b/Lib/site.py @@ -372,6 +372,17 @@ def setcopyright(): def sethelper(): builtins.help = _sitebuiltins._Helper() +def gethistoryfile(): + """Check if PYTHONHISTORY environment variable + is set and define it as python_history file, + if not 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__. @@ -407,13 +418,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 IOError: diff --git a/Lib/test/test_site.py b/Lib/test/test_site.py index 342ec9e43d5de3..d535cf9815b0c3 100644 --- a/Lib/test/test_site.py +++ b/Lib/test/test_site.py @@ -259,6 +259,10 @@ def test_getsitepackages(self): wanted = os.path.join('xoxo', 'lib', 'site-packages') self.assertEqual(dirs[1], wanted) + def test_gethistoryfile(self): + os.environ['PYTHONHISTORY'] = "xoxo" + self.assertEqual(site.gethistoryfile(), "xoxo") + class PthFile(object): """Helper class for handling testing of .pth files""" diff --git a/Misc/NEWS b/Misc/NEWS index 2f7bd7ac7b20d4..3576be53b61d25 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,9 @@ What's New in Python 3.7.0 alpha 1? Core and Builtins ----------------- +- bpo-29779: New environment variable PYTHONHISTORY if + this is set you can change the location of a python_history file. + - bpo-28856: Fix an oversight that %b format for bytes should support objects follow the buffer protocol. diff --git a/Misc/python.man b/Misc/python.man index 385b6546c8a52b..8c5f781556b6d6 100644 --- a/Misc/python.man +++ b/Misc/python.man @@ -431,6 +431,10 @@ values. The integer must be a decimal number in the range [0,4294967295]. Specifying the value 0 will disable hash randomization. +.IP PYTHONHISTORY +If this is set you can change the location of a +python_history file, by default it will be ~/.python_history. + .SH AUTHOR The Python Software Foundation: https://www.python.org/psf/ .SH INTERNET RESOURCES diff --git a/Modules/main.c b/Modules/main.c index 42fe2a012ad800..1e1931cbae5adc 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -105,7 +105,8 @@ static const char usage_6[] = " predictable seed.\n" "PYTHONMALLOC: set the Python memory allocators and/or install debug hooks\n" " on Python memory allocators. Use PYTHONMALLOC=debug to install debug\n" -" hooks.\n"; +" hooks.\n" +"PYTHONHISTORY: If this is set, you can change the location of a python_history file.\n"; static int usage(int exitcode, const wchar_t* program)