Eryk Sun <eryk...@gmail.com> added the comment:

This issue is due to a bug in GNU Readline (actually GNU History). It's 
documented that write_history [1] returns an errno value. But the internal 
history_do_write [2] function in this case returns the value from a rename() 
system call, via the value from histfile_restore [3]. rename() returns -1 on 
failure, which is being mishandled here as an errno value.

Actually, write permission to the original history file isn't required. GNU 
History writes to a temp file and then replaces the original via rename(). 
Normally this just requires write access to the directory. But if the directory 
or target file has the immutable file attribute set, then rename() fails with 
errno set to EPERM (at least in Linux; maybe it's a different error in BSD or 
macOS).

If not for the GNU History bug, this failed call would raise a PermissionError, 
which is already handled. Maybe it could also write an error message to stderr 
that write_history failed. For example:

            def write_history():
                try:
                    readline.write_history_file(history)
                except OSError as e:
                    # bpo-19891: home directory does not exist or is not
                    # writable
                    if not (isinstance(e, (FileNotFoundError, PermissionError))
                    # bpo-39468: GNU History may return -1 as an errno value
                            or e.errno == -1):
                        raise
                    print('write_history failed: {!r}'.format(history),
                        file=sys.stderr)

I agree with Steven that the handler should not presume to modify file 
permissions or attributes.

[1] https://tiswww.case.edu/php/chet/readline/history.html#IDX29
[2] http://git.savannah.gnu.org/cgit/readline.git/tree/histfile.c#n630
[3] http://git.savannah.gnu.org/cgit/readline.git/tree/histfile.c#n458

----------
nosy: +eryksun

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue39468>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to