diff -r 9da300ad8255 -r d1cbf0347eb4 Doc/library/readline.rst --- a/Doc/library/readline.rst Fri Mar 25 14:26:56 2011 +0200 +++ b/Doc/library/readline.rst Fri Mar 25 22:06:04 2011 +0100 @@ -190,28 +190,31 @@ 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. :: +file named :file:`.python_history` in the user's home directory:: import os + import atexit import readline - histfile = os.path.join(os.environ["HOME"], ".pyhist") + + histfile = os.path.join(os.expanduser("~"), ".python_history") + try: readline.read_history_file(histfile) except IOError: pass - import atexit + atexit.register(readline.write_history_file, histfile) - del os, histfile + +This code is actually automatically run when Python is run in +:ref:`interactive mode ` (see :ref:`rlcompleter-config`). The following example extends the :class:`code.InteractiveConsole` class to support history save/restore. :: + import os import code + import atexit import readline - import atexit - import os class HistoryConsole(code.InteractiveConsole): def __init__(self, locals=None, filename="", diff -r 9da300ad8255 -r d1cbf0347eb4 Doc/library/rlcompleter.rst --- a/Doc/library/rlcompleter.rst Fri Mar 25 14:26:56 2011 +0200 +++ b/Doc/library/rlcompleter.rst Fri Mar 25 22:06:04 2011 +0100 @@ -27,18 +27,10 @@ readline.__name__ readline.parse_and_bind( >>> readline. -The :mod:`rlcompleter` module is designed for use with Python's interactive -mode. A user can add the following lines to his or her initialization file -(identified by the :envvar:`PYTHONSTARTUP` environment variable) to get -automatic :kbd:`Tab` completion:: - - try: - import readline - except ImportError: - print("Module readline not available.") - else: - import rlcompleter - readline.parse_and_bind("tab: complete") +The :mod:`rlcompleter` module is designed for use with Python's +:ref:`interactive mode `. Unless Python is run with the +:option:`-S` option, the module is automatically imported and configured +(see :ref:`rlcompleter-config`). On platforms without :mod:`readline`, the :class:`Completer` class defined by this module can still be used for custom purposes. diff -r 9da300ad8255 -r d1cbf0347eb4 Doc/library/site.rst --- a/Doc/library/site.rst Fri Mar 25 14:26:56 2011 +0200 +++ b/Doc/library/site.rst Fri Mar 25 22:06:04 2011 +0100 @@ -90,6 +90,29 @@ :mod:`sitecustomize` is still attempted. +.. _rlcompleter-config: + +Readline configuration +---------------------- + +On systems that support :mod:`readline`, this module will also import and +configure the :mod:`rlcompleter` module, if Python is started in +:ref:`interactive mode ` and without the :option:`-S` option. +The default behavior is to call :func:`readline.read_init_file` to let +readline use the appropriate configuration, and to use +:file:`~/.python_history` as save file. To disable it, use code like this in +your :mod:`sitecustomize` or :mod:`usercustomize` module or +:envvar:`PYTHONSTARTUP` file:: + + import atexit + import readline + + atexit.unregister(readline.write_history_file) + +You may then :func:`~atexit.register` another location to use with +:func:`~readline.write_history_file` if you wish so. + + .. data:: PREFIXES A list of prefixes for site package directories @@ -122,8 +145,7 @@ Adds all the standard site-specific directories to the module search path. This function is called automatically when this module is imported, - unless the :program:`python` interpreter was started with the :option:`-S` - flag. + unless the Python interpreter was started with the :option:`-S` flag. .. function:: addsitedir(sitedir, known_paths=None) diff -r 9da300ad8255 -r d1cbf0347eb4 Doc/tutorial/interactive.rst --- a/Doc/tutorial/interactive.rst Fri Mar 25 14:26:56 2011 +0200 +++ b/Doc/tutorial/interactive.rst Fri Mar 25 22:06:04 2011 +0100 @@ -95,20 +95,17 @@ module: rlcompleter module: readline -Automatic completion of variable and module names is optionally available. To -enable it in the interpreter's interactive mode, add the following to your -startup file: [#]_ :: - - import rlcompleter, readline - readline.parse_and_bind('tab: complete') - -This binds the :kbd:`Tab` key to the completion function, so hitting the +Automatic completion of variable and module names is optionally available. It +is :ref:`automatically enabled ` at interpreter startup to +let you use the :kbd:`Tab` key to the completion function, so hitting the :kbd:`Tab` key twice suggests completions; it looks at Python statement names, the current local variables, and the available module names. For dotted expressions such as ``string.a``, it will evaluate the expression up to the final ``'.'`` and then suggest completions from the attributes of the resulting object. Note that this may execute application-defined code if an object with a -:meth:`__getattr__` method is part of the expression. +:meth:`__getattr__` method is part of the expression. The default +configuration also saves your history into a file named +:file:`.python_history` in your user directory. A more capable startup file might look like this example. Note that this deletes the names it creates once they are no longer needed; this is done since @@ -123,27 +120,35 @@ # bound to the Esc key by default (you can change it - see readline docs). # # Store the file in ~/.pystartup, and set an environment variable to point - # to it: "export PYTHONSTARTUP=/home/user/.pystartup" in bash. + # to it: "export PYTHONSTARTUP=/home/user/.pystartup" in bash. Python will + # automatically execute the contents of this file when started in + # interactive mode. # # Note that PYTHONSTARTUP does *not* expand "~", so you have to put in the # full path to your home directory. import atexit - import os import readline import rlcompleter - historyPath = os.path.expanduser("~/.pyhistory") + history_path = os.path.expanduser("~/.pyhistory") - def save_history(historyPath=historyPath): - import readline - readline.write_history_file(historyPath) + def save_history(history_path=history_path): - if os.path.exists(historyPath): - readline.read_history_file(historyPath) + if os.path.exists(history_path): + try: + readline.read_history_file(history_path) + except IOError: + # ignore errors, for example if the file doesn't exist + pass - atexit.register(save_history) - del os, atexit, readline, rlcompleter, save_history, historyPath + # undo default configuration + atexit.unregister(readline.write_history_file) + + atexit.register(readline.write_history_file, history_path) + + # remove those names from the interactive session + del atexit, readline, rlcompleter, history_path .. _tut-commentary: @@ -165,13 +170,6 @@ `bpython`_. -.. rubric:: Footnotes - -.. [#] Python will execute the contents of a file identified by the - :envvar:`PYTHONSTARTUP` environment variable when you start an interactive - interpreter. - - .. _GNU Readline: http://tiswww.case.edu/php/chet/readline/rltop.html .. _IPython: http://ipython.scipy.org/ .. _bpython: http://www.bpython-interpreter.org/ diff -r 9da300ad8255 -r d1cbf0347eb4 Lib/site.py --- a/Lib/site.py Fri Mar 25 14:26:56 2011 +0200 +++ b/Lib/site.py Fri Mar 25 22:06:04 2011 +0100 @@ -50,10 +50,13 @@ site-specific customizations. If this import fails with an ImportError exception, it is silently ignored. +The readline module is also automatically configured to enable +completion for systems that support it. This can be overriden in +sitecustomize, usercustomize or PYTHONSTARTUP. """ +import os import sys -import os import builtins import traceback @@ -459,6 +462,29 @@ def sethelper(): builtins.help = _Helper() +def enablerlcompleter(): + """Enable default readline configuration. + + If the readline module can be imported, this function will set tab as + completion key and register ~/.python_history as history file. This + can be overriden in the sitecustomize or usercustomize module, or + PYTHONSTARTUP file (TODO test this!) + """ + try: + import readline + import rlcompleter + except ImportError: + return + + import atexit + readline.read_init_file() + history = os.path.join(os.path.expanduser('~'), '.python_history') + try: + readline.read_history_file(history) + except IOError: + pass + atexit.register(readline.write_history_file, history) + def aliasmbcs(): """On Windows, some default encodings are not provided by Python, while they are always available as "mbcs" in each locale. Make @@ -526,6 +552,8 @@ setquit() setcopyright() sethelper() + if sys.flags.interactive: + enablerlcompleter() aliasmbcs() execsitecustomize() if ENABLE_USER_SITE: diff -r 9da300ad8255 -r d1cbf0347eb4 Misc/NEWS --- a/Misc/NEWS Fri Mar 25 14:26:56 2011 +0200 +++ b/Misc/NEWS Fri Mar 25 22:06:04 2011 +0100 @@ -84,6 +84,9 @@ Library ------- +- Issue #5845: Automatically read readline configuration to enable completion + in interactive mode. + - Issue #6811: Allow importlib to change a code object's co_filename attribute to match the path to where the source code currently is, not where the code object originally came from.