diff options
author | Benjamin Peterson <benjamin@python.org> | 2014-11-26 19:58:16 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2014-11-26 19:58:16 (GMT) |
commit | 33f8f15bdd313b1e2d1c99d0971659e983a35672 (patch) | |
tree | dbd199022545350d249c01d659ffcb2a9f55ee27 /Doc/library/readline.rst | |
parent | aacfcccdc39b074521d3e5d4b5a1b1e020662366 (diff) | |
download | cpython-33f8f15bdd313b1e2d1c99d0971659e983a35672.zip cpython-33f8f15bdd313b1e2d1c99d0971659e983a35672.tar.gz cpython-33f8f15bdd313b1e2d1c99d0971659e983a35672.tar.bz2 |
add readline.append_history_file (closes #22940)
patch by "bru"
Diffstat (limited to 'Doc/library/readline.rst')
-rw-r--r-- | Doc/library/readline.rst | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/Doc/library/readline.rst b/Doc/library/readline.rst index 692310b..3864f0d 100644 --- a/Doc/library/readline.rst +++ b/Doc/library/readline.rst @@ -59,6 +59,14 @@ The :mod:`readline` module defines the following functions: Save a readline history file. The default filename is :file:`~/.history`. +.. function:: append_history_file(nelements[, filename]) + + Append the last *nelements* of history to a file. The default filename is + :file:`~/.history`. The file must already exist. + + .. versionadded:: 3.5 + + .. function:: clear_history() Clear the current history. (Note: this function is not available if the @@ -209,6 +217,26 @@ from the user's :envvar:`PYTHONSTARTUP` file. :: This code is actually automatically run when Python is run in :ref:`interactive mode <tut-interactive>` (see :ref:`rlcompleter-config`). +The following example achieves the same goal but supports concurrent interactive +sessions, by only appending the new history. :: + + import atexit + import os + import realine + histfile = os.path.join(os.path.expanduser("~"), ".python_history") + + try: + readline.read_history_file(histfile) + h_len = readline.get_history_length() + except FileNotFoundError: + open(histfile, 'wb').close() + h_len = 0 + + def save(prev_h_len, histfile): + new_h_len = readline.get_history_length() + readline.append_history_file(new_h_len - prev_h_len, histfile) + atexit.register(save, h_len, histfile) + The following example extends the :class:`code.InteractiveConsole` class to support history save/restore. :: @@ -234,4 +262,3 @@ support history save/restore. :: def save_history(self, histfile): readline.write_history_file(histfile) - |