summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2013-05-04 18:08:35 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2013-05-04 18:08:35 (GMT)
commit1a6cb30a346ba8812d6abc77fddee636ae06ccff (patch)
tree3ba3f519e8e29fd616365fec41803795ba406bf3 /Lib
parent4c14b5de1cf372e963a4378fc6cb2bca36d24eb8 (diff)
downloadcpython-1a6cb30a346ba8812d6abc77fddee636ae06ccff.zip
cpython-1a6cb30a346ba8812d6abc77fddee636ae06ccff.tar.gz
cpython-1a6cb30a346ba8812d6abc77fddee636ae06ccff.tar.bz2
Issue #5845: Enable tab-completion in the interactive interpreter by default, thanks to a new sys.__interactivehook__.
(original patch by Éric Araujo)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/site.py42
1 files changed, 40 insertions, 2 deletions
diff --git a/Lib/site.py b/Lib/site.py
index 9b1d5bb..f13d4b4 100644
--- a/Lib/site.py
+++ b/Lib/site.py
@@ -58,11 +58,14 @@ Note that bletch is omitted because it doesn't exist; bar precedes foo
because bar.pth comes alphabetically before foo.pth; and spam is
omitted because it is not mentioned in either path configuration file.
-After these path manipulations, an attempt is made to import a module
+The readline module is also automatically configured to enable
+completion for systems that support it. This can be overriden in
+sitecustomize, usercustomize or PYTHONSTARTUP.
+
+After these operations, an attempt is made to import a module
named sitecustomize, which can perform arbitrary additional
site-specific customizations. If this import fails with an
ImportError exception, it is silently ignored.
-
"""
import sys
@@ -452,6 +455,40 @@ class _Helper(object):
def sethelper():
builtins.help = _Helper()
+def enablerlcompleter():
+ """Enable default readline configuration on interactive prompts, by
+ registering a sys.__interactivehook__.
+
+ If the readline module can be imported, the hook will set the Tab key
+ as completion key and register ~/.python_history as history file.
+ This can be overriden in the sitecustomize or usercustomize module,
+ or in a PYTHONSTARTUP file.
+ """
+ def register_readline():
+ import atexit
+ try:
+ import readline
+ import rlcompleter
+ except ImportError:
+ return
+
+ # Reading the initialization (config) file may not be enough to set a
+ # completion key, so we set one first and then read the file
+ if 'libedit' in getattr(readline, '__doc__', ''):
+ readline.parse_and_bind('bind ^I rl_complete')
+ else:
+ readline.parse_and_bind('tab: complete')
+ 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)
+
+ sys.__interactivehook__ = register_readline
+
def aliasmbcs():
"""On Windows, some default encodings are not provided by Python,
while they are always available as "mbcs" in each locale. Make
@@ -571,6 +608,7 @@ def main():
setquit()
setcopyright()
sethelper()
+ enablerlcompleter()
aliasmbcs()
execsitecustomize()
if ENABLE_USER_SITE: