mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 02:15:10 +00:00 
			
		
		
		
	 b044b2a701
			
		
	
	
		b044b2a701
		
	
	
	
	
		
			
			svn+ssh://svn.python.org/python/branches/py3k ................ r74821 | georg.brandl | 2009-09-16 11:42:19 +0200 (Mi, 16 Sep 2009) | 1 line #6885: run python 3 as python3. ................ r74828 | georg.brandl | 2009-09-16 16:23:20 +0200 (Mi, 16 Sep 2009) | 1 line Use true booleans. ................ r74829 | georg.brandl | 2009-09-16 16:24:29 +0200 (Mi, 16 Sep 2009) | 1 line Small PEP8 correction. ................ r74830 | georg.brandl | 2009-09-16 16:36:22 +0200 (Mi, 16 Sep 2009) | 1 line Use true booleans. ................ r74831 | georg.brandl | 2009-09-16 17:54:04 +0200 (Mi, 16 Sep 2009) | 1 line Use true booleans and PEP8 for argdefaults. ................ r74833 | georg.brandl | 2009-09-16 17:58:14 +0200 (Mi, 16 Sep 2009) | 1 line Last round of adapting style of documenting argument default values. ................ r74835 | georg.brandl | 2009-09-16 18:00:31 +0200 (Mi, 16 Sep 2009) | 33 lines Merged revisions 74817-74820,74822-74824 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r74817 | georg.brandl | 2009-09-16 11:05:11 +0200 (Mi, 16 Sep 2009) | 1 line Make deprecation notices as visible as warnings are right now. ........ r74818 | georg.brandl | 2009-09-16 11:23:04 +0200 (Mi, 16 Sep 2009) | 1 line #6880: add reference to classes section in exceptions section, which comes earlier. ........ r74819 | georg.brandl | 2009-09-16 11:24:57 +0200 (Mi, 16 Sep 2009) | 1 line #6876: fix base class constructor invocation in example. ........ r74820 | georg.brandl | 2009-09-16 11:30:48 +0200 (Mi, 16 Sep 2009) | 1 line #6891: comment out dead link to Unicode article. ........ r74822 | georg.brandl | 2009-09-16 12:12:06 +0200 (Mi, 16 Sep 2009) | 1 line #5621: refactor description of how class/instance attributes interact on a.x=a.x+1 or augassign. ........ r74823 | georg.brandl | 2009-09-16 15:06:22 +0200 (Mi, 16 Sep 2009) | 1 line Remove strange trailing commas. ........ r74824 | georg.brandl | 2009-09-16 15:11:06 +0200 (Mi, 16 Sep 2009) | 1 line #6892: fix optparse example involving help option. ........ ................
		
			
				
	
	
		
			223 lines
		
	
	
	
		
			6.4 KiB
		
	
	
	
		
			ReStructuredText
		
	
	
	
	
	
			
		
		
	
	
			223 lines
		
	
	
	
		
			6.4 KiB
		
	
	
	
		
			ReStructuredText
		
	
	
	
	
	
| 
 | |
| :mod:`readline` --- GNU readline interface
 | |
| ==========================================
 | |
| 
 | |
| .. module:: readline
 | |
|    :platform: Unix
 | |
|    :synopsis: GNU readline support for Python.
 | |
| .. sectionauthor:: Skip Montanaro <skip@pobox.com>
 | |
| 
 | |
| 
 | |
| The :mod:`readline` module defines a number of functions to facilitate
 | |
| completion and reading/writing of history files from the Python interpreter.
 | |
| This module can be used directly or via the :mod:`rlcompleter` module.  Settings
 | |
| made using  this module affect the behaviour of both the interpreter's
 | |
| interactive prompt  and the prompts offered by the built-in :func:`input`
 | |
| function.
 | |
| 
 | |
| The :mod:`readline` module defines the following functions:
 | |
| 
 | |
| 
 | |
| .. function:: parse_and_bind(string)
 | |
| 
 | |
|    Parse and execute single line of a readline init file.
 | |
| 
 | |
| 
 | |
| .. function:: get_line_buffer()
 | |
| 
 | |
|    Return the current contents of the line buffer.
 | |
| 
 | |
| 
 | |
| .. function:: insert_text(string)
 | |
| 
 | |
|    Insert text into the command line.
 | |
| 
 | |
| 
 | |
| .. function:: read_init_file([filename])
 | |
| 
 | |
|    Parse a readline initialization file. The default filename is the last filename
 | |
|    used.
 | |
| 
 | |
| 
 | |
| .. function:: read_history_file([filename])
 | |
| 
 | |
|    Load a readline history file. The default filename is :file:`~/.history`.
 | |
| 
 | |
| 
 | |
| .. function:: write_history_file([filename])
 | |
| 
 | |
|    Save a readline history file. The default filename is :file:`~/.history`.
 | |
| 
 | |
| 
 | |
| .. function:: clear_history()
 | |
| 
 | |
|    Clear the current history.  (Note: this function is not available if the
 | |
|    installed version of GNU readline doesn't support it.)
 | |
| 
 | |
| 
 | |
| .. function:: get_history_length()
 | |
| 
 | |
|    Return the desired length of the history file.  Negative values imply unlimited
 | |
|    history file size.
 | |
| 
 | |
| 
 | |
| .. function:: set_history_length(length)
 | |
| 
 | |
|    Set the number of lines to save in the history file. :func:`write_history_file`
 | |
|    uses this value to truncate the history file when saving.  Negative values imply
 | |
|    unlimited history file size.
 | |
| 
 | |
| 
 | |
| .. function:: get_current_history_length()
 | |
| 
 | |
|    Return the number of lines currently in the history.  (This is different from
 | |
|    :func:`get_history_length`, which returns the maximum number of lines that will
 | |
|    be written to a history file.)
 | |
| 
 | |
| 
 | |
| .. function:: get_history_item(index)
 | |
| 
 | |
|    Return the current contents of history item at *index*.
 | |
| 
 | |
| 
 | |
| .. function:: remove_history_item(pos)
 | |
| 
 | |
|    Remove history item specified by its position from the history.
 | |
| 
 | |
| 
 | |
| .. function:: replace_history_item(pos, line)
 | |
| 
 | |
|    Replace history item specified by its position with the given line.
 | |
| 
 | |
| 
 | |
| .. function:: redisplay()
 | |
| 
 | |
|    Change what's displayed on the screen to reflect the current contents of the
 | |
|    line buffer.
 | |
| 
 | |
| 
 | |
| .. function:: set_startup_hook([function])
 | |
| 
 | |
|    Set or remove the startup_hook function.  If *function* is specified, it will be
 | |
|    used as the new startup_hook function; if omitted or ``None``, any hook function
 | |
|    already installed is removed.  The startup_hook function is called with no
 | |
|    arguments just before readline prints the first prompt.
 | |
| 
 | |
| 
 | |
| .. function:: set_pre_input_hook([function])
 | |
| 
 | |
|    Set or remove the pre_input_hook function.  If *function* is specified, it will
 | |
|    be used as the new pre_input_hook function; if omitted or ``None``, any hook
 | |
|    function already installed is removed.  The pre_input_hook function is called
 | |
|    with no arguments after the first prompt has been printed and just before
 | |
|    readline starts reading input characters.
 | |
| 
 | |
| 
 | |
| .. function:: set_completer([function])
 | |
| 
 | |
|    Set or remove the completer function.  If *function* is specified, it will be
 | |
|    used as the new completer function; if omitted or ``None``, any completer
 | |
|    function already installed is removed.  The completer function is called as
 | |
|    ``function(text, state)``, for *state* in ``0``, ``1``, ``2``, ..., until it
 | |
|    returns a non-string value.  It should return the next possible completion
 | |
|    starting with *text*.
 | |
| 
 | |
| 
 | |
| .. function:: get_completer()
 | |
| 
 | |
|    Get the completer function, or ``None`` if no completer function has been set.
 | |
| 
 | |
| 
 | |
| .. function:: get_completion_type()
 | |
| 
 | |
|    Get the type of completion being attempted.
 | |
| 
 | |
| 
 | |
| .. function:: get_begidx()
 | |
| 
 | |
|    Get the beginning index of the readline tab-completion scope.
 | |
| 
 | |
| 
 | |
| .. function:: get_endidx()
 | |
| 
 | |
|    Get the ending index of the readline tab-completion scope.
 | |
| 
 | |
| 
 | |
| .. function:: set_completer_delims(string)
 | |
| 
 | |
|    Set the readline word delimiters for tab-completion.
 | |
| 
 | |
| 
 | |
| .. function:: get_completer_delims()
 | |
| 
 | |
|    Get the readline word delimiters for tab-completion.
 | |
| 
 | |
| 
 | |
| .. function:: set_completion_display_matches_hook([function])
 | |
| 
 | |
|    Set or remove the completion display function.  If *function* is
 | |
|    specified, it will be used as the new completion display function;
 | |
|    if omitted or ``None``, any completion display function already
 | |
|    installed is removed.  The completion display function is called as
 | |
|    ``function(substitution, [matches], longest_match_length)`` once
 | |
|    each time matches need to be displayed.
 | |
| 
 | |
| 
 | |
| .. function:: add_history(line)
 | |
| 
 | |
|    Append a line to the history buffer, as if it was the last line typed.
 | |
| 
 | |
| 
 | |
| .. seealso::
 | |
| 
 | |
|    Module :mod:`rlcompleter`
 | |
|       Completion of Python identifiers at the interactive prompt.
 | |
| 
 | |
| 
 | |
| .. _readline-example:
 | |
| 
 | |
| Example
 | |
| -------
 | |
| 
 | |
| The following example demonstrates how to use the :mod:`readline` module's
 | |
| history reading and writing functions to automatically load and save a history
 | |
| file named :file:`.pyhist` from the user's home directory.  The code below would
 | |
| normally be executed automatically during interactive sessions from the user's
 | |
| :envvar:`PYTHONSTARTUP` file. ::
 | |
| 
 | |
|    import os
 | |
|    histfile = os.path.join(os.environ["HOME"], ".pyhist")
 | |
|    try:
 | |
|        readline.read_history_file(histfile)
 | |
|    except IOError:
 | |
|        pass
 | |
|    import atexit
 | |
|    atexit.register(readline.write_history_file, histfile)
 | |
|    del os, histfile
 | |
| 
 | |
| The following example extends the :class:`code.InteractiveConsole` class to
 | |
| support history save/restore. ::
 | |
| 
 | |
|    import code
 | |
|    import readline
 | |
|    import atexit
 | |
|    import os
 | |
| 
 | |
|    class HistoryConsole(code.InteractiveConsole):
 | |
|        def __init__(self, locals=None, filename="<console>",
 | |
|                     histfile=os.path.expanduser("~/.console-history")):
 | |
|            code.InteractiveConsole.__init__(self, locals, filename)
 | |
|            self.init_history(histfile)
 | |
| 
 | |
|        def init_history(self, histfile):
 | |
|            readline.parse_and_bind("tab: complete")
 | |
|            if hasattr(readline, "read_history_file"):
 | |
|                try:
 | |
|                    readline.read_history_file(histfile)
 | |
|                except IOError:
 | |
|                    pass
 | |
|                atexit.register(self.save_history, histfile)
 | |
| 
 | |
|        def save_history(self, histfile):
 | |
|            readline.write_history_file(histfile)
 | |
| 
 |