def enablerlcompleter():
"""Enable default readline configuration on interactive prompts, by
registering a sys.__interactivehook__.
+ """
+ sys.__interactivehook__ = register_readline
+
+
+def register_readline():
+ """Configure readline completion on interactive prompts.
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 overridden 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 readline.backend == 'editline':
- readline.parse_and_bind('bind ^I rl_complete')
- else:
- readline.parse_and_bind('tab: complete')
+ 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 readline.backend == 'editline':
+ readline.parse_and_bind('bind ^I rl_complete')
+ else:
+ readline.parse_and_bind('tab: complete')
+
+ try:
+ readline.read_init_file()
+ except OSError:
+ # An OSError here could have many causes, but the most likely one
+ # is that there's no .inputrc file (or .editrc file in the case of
+ # Mac OS X + libedit) in the expected location. In that case, we
+ # want to ignore the exception.
+ pass
+
+ if readline.get_current_history_length() == 0:
+ # If no history was loaded, default to .python_history,
+ # or PYTHON_HISTORY.
+ # The guard is necessary to avoid doubling history size at
+ # each interpreter exit when readline was already configured
+ # through a PYTHONSTARTUP hook, see:
+ # http://bugs.python.org/issue5845#msg198636
+ history = gethistoryfile()
try:
- readline.read_init_file()
+ readline.read_history_file(history)
except OSError:
- # An OSError here could have many causes, but the most likely one
- # is that there's no .inputrc file (or .editrc file in the case of
- # Mac OS X + libedit) in the expected location. In that case, we
- # want to ignore the exception.
pass
- if readline.get_current_history_length() == 0:
- # If no history was loaded, default to .python_history,
- # or PYTHON_HISTORY.
- # The guard is necessary to avoid doubling history size at
- # each interpreter exit when readline was already configured
- # through a PYTHONSTARTUP hook, see:
- # http://bugs.python.org/issue5845#msg198636
- history = gethistoryfile()
+ def write_history():
try:
- readline.read_history_file(history)
- except OSError:
+ readline.write_history_file(history)
+ except (FileNotFoundError, PermissionError):
+ # home directory does not exist or is not writable
+ # https://bugs.python.org/issue19891
pass
- def write_history():
- try:
- readline.write_history_file(history)
- except OSError:
- # bpo-19891, bpo-41193: Home directory does not exist
- # or is not writable, or the filesystem is read-only.
- pass
+ atexit.register(write_history)
- atexit.register(write_history)
-
- sys.__interactivehook__ = register_readline
def venv(known_paths):
global PREFIXES, ENABLE_USER_SITE