classification
Title: Disabling logging to ~/.python_history is not simple enough
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.4
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Arfrever, Sworddragon, eric.araujo, jesse.p.ogle, jwilk, martin.panter, pitrou, r.david.murray
Priority: normal Keywords:

Created on 2014-03-10 20:30 by Sworddragon, last changed 2018-06-01 17:25 by jesse.p.ogle.

Messages (9)
msg213079 - (view) Author: (Sworddragon) Date: 2014-03-10 20:30
I have noticed that since Python 3.4 the interactive mode does log all commands to ~/.python_history. This caused me to switch into "normal user mode" and look for a solution. With Google I have found the related entry in the documentation:

On systems that support readline, this module will also import and configure the rlcompleter module, if Python is started in interactive mode and without the -S option. The default behavior is enable tab-completion and to use ~/.python_history as the history save file. To disable it, delete (or override) the sys.__interactivehook__ attribute in your sitecustomize or usercustomize module or your PYTHONSTARTUP file.


On my system PYTHONSTARTUP is empty and if I would still pretending that I would be a normal user I would now get in panic. I think either the documentation needs an enhancement or disabling logging should be more simplified. For example the shell controls this with the HISTFILESIZE environment variable. Maybe Python can use for this case an environment variable too that the user can store in his ~/.bashrc.
msg213082 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-03-10 20:49
readline (all uses of readline, not just by python) can be controlled via the '.inputrc' file, which is documented in the readline man page, which should be available if readline is available :)  Perhaps this should be mentioned in the paragraph(s) where we talk about disabling readline.
msg213084 - (view) Author: Arfrever Frehtes Taifersar Arahesis (Arfrever) * (Python triager) Date: 2014-03-10 21:00
I would suggest to support PYTHONHISTFILE variable (similar to HISTFILE and LESSHISTFILE) and to not replace file pointed by this variable.

This shows that file is being currently replaced:
# cp /dev/null .python_history
# LC_ALL="C" stat -c "%F" .python_history
character special file
# python3.4 -q
>>> ^D
# LC_ALL="C" stat -c "%F" .python_history
regular empty file
msg213107 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-03-10 23:52
What sure not the "panic mode" is about, but in any case you can simply use e.g. "chmod 111 .python_history" (which will forbid both reading and writing the file).
msg213114 - (view) Author: Arfrever Frehtes Taifersar Arahesis (Arfrever) * (Python triager) Date: 2014-03-11 02:38
Antoine Pitrou:
> What sure not the "panic mode" is about, but in any case you can simply
> use e.g. "chmod 111 .python_history" (which will forbid both reading and writing the file).

It surely does not work:

$ rm -f .python_history
$ touch .python_history
$ chmod 111 .python_history
mode of ‘.python_history’ changed from 0640 (rw-r-----) to 0111 (--x--x--x)
$ python3.4 -q
>>> 123
123
>>> ^D
$ cat .python_history
123
$
msg213127 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-03-11 08:03
On mar., 2014-03-11 at 02:38 +0000, Arfrever Frehtes Taifersar Arahesis
wrote:
> Arfrever Frehtes Taifersar Arahesis added the comment:
> 
> Antoine Pitrou:
> > What sure not the "panic mode" is about, but in any case you can simply
> > use e.g. "chmod 111 .python_history" (which will forbid both reading and writing the file).
> 
> It surely does not work:

It probably depends on the OS. It worked here.
msg265568 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2016-05-15 02:17
Thanks to Issue 26870, in Python 3.6 you should be able to call readline.set_auto_history(False) to stop entries being added to the history list. Does that change anything here?
msg265628 - (view) Author: (Sworddragon) Date: 2016-05-15 16:16
I'm assuming this means this must be called on every interactive python instance. If not just correct me.

While this might be an enhancement it would still be too inconvenient. Basically something that can be configured permanently for example with ~/.bashrc would be the preferred solution.
msg318437 - (view) Author: Jesse Paul Ogle (jesse.p.ogle) Date: 2018-06-01 17:25
Greetings, I came across this issue while looking into XDG Base Directory Specification. This issue is only tagged with version 3.4, but the underlying issue (not being able to change the history file / size through environment variables) appears to still exist in the definiton of enablerlcompleter() in Lib/site.py for master. I may be wrong about that.

Tag 3.4:
https://github.com/python/cpython/blob/3.4/Lib/site.py#L423

Tag 3.7.0b5 (latest as of writing)
https://github.com/python/cpython/blob/v3.7.0b5/Lib/site.py#L436


This seems to me like a fairly straight forward fix. I made the change by copying enablercompleter() to a startup.py file and changing that line to the following:

        if 'PYTHONHISTFILE' in os.environ:
            history = os.path.expanduser(os.environ['PYTHONHISTFILE'])
        elif 'XDG_DATA_HOME' in os.environ:
            history = os.path.join(os.path.expanduser(os.environ['XDG_DATA_HOME']),
                                   'python', 'python_history')
        else:
            history = os.path.join(os.path.expanduser('~'),
                                   '.python_history')

        history = os.path.abspath(history)
        _dir, _ = os.path.split(history)
        os.makedirs(_dir, exist_ok=True)

This enables specifying the history file via PYTHONHISTFILE or using XDG_DATA_HOME. XDG_DATA_HOME is convenient but not necessary as PYTHONHISTFILE would at least provide a work around. One could disable logging by setting PYTHONHISTFILE=/dev/null or whatever it is on Windows.

I apologize for not providing a patch but I have literally no idea where to start for that. I assume addition of environment variables requires iterating on the mailing list first?
History
Date User Action Args
2018-06-01 17:25:59jesse.p.oglesetnosy: + jesse.p.ogle
messages: + msg318437
2016-05-15 16:16:50Sworddragonsetmessages: + msg265628
2016-05-15 02:17:14martin.pantersetnosy: + martin.panter
messages: + msg265568
2014-06-01 20:08:35jwilksetnosy: + jwilk
2014-03-12 05:37:39eric.araujosetnosy: + eric.araujo
2014-03-11 08:03:42pitrousetmessages: + msg213127
2014-03-11 02:38:56Arfreversetmessages: + msg213114
2014-03-10 23:52:47pitrousetnosy: + pitrou
messages: + msg213107
2014-03-10 21:00:50Arfreversetnosy: + Arfrever
messages: + msg213084
2014-03-10 20:49:46r.david.murraysetnosy: + r.david.murray
messages: + msg213082
2014-03-10 20:30:35Sworddragoncreate