mirror of
https://github.com/python/cpython.git
synced 2024-11-27 11:55:13 +08:00
gh-84995: Run sys.__interactivehook__() on asyncio REPL startup (#20517)
This makes the asyncio REPL (`python -m asyncio`) more usable and similar to the regular REPL. This exposes register_readline() as a top-level function in site.py, but it's intentionally undocumented. Co-authored-by: Carol Willing <carolcode@willingconsulting.com> Co-authored-by: Itamar Oren <itamarost@gmail.com>
This commit is contained in:
parent
e6e35327d8
commit
b5949eac62
@ -3,6 +3,7 @@ import asyncio
|
||||
import code
|
||||
import concurrent.futures
|
||||
import inspect
|
||||
import site
|
||||
import sys
|
||||
import threading
|
||||
import types
|
||||
@ -109,6 +110,21 @@ if __name__ == '__main__':
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
interactive_hook = getattr(sys, "__interactivehook__", None)
|
||||
|
||||
if interactive_hook is not None:
|
||||
interactive_hook()
|
||||
|
||||
if interactive_hook is site.register_readline:
|
||||
# Fix the completer function to use the interactive console locals
|
||||
try:
|
||||
import rlcompleter
|
||||
except:
|
||||
pass
|
||||
else:
|
||||
completer = rlcompleter.Completer(console.locals)
|
||||
readline.set_completer(completer.complete)
|
||||
|
||||
repl_thread = REPLThread()
|
||||
repl_thread.daemon = True
|
||||
repl_thread.start()
|
||||
|
14
Lib/site.py
14
Lib/site.py
@ -460,13 +460,18 @@ def gethistoryfile():
|
||||
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
|
||||
@ -506,14 +511,13 @@ def enablerlcompleter():
|
||||
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.
|
||||
except (FileNotFoundError, PermissionError):
|
||||
# home directory does not exist or is not writable
|
||||
# https://bugs.python.org/issue19891
|
||||
pass
|
||||
|
||||
atexit.register(write_history)
|
||||
|
||||
sys.__interactivehook__ = register_readline
|
||||
|
||||
def venv(known_paths):
|
||||
global PREFIXES, ENABLE_USER_SITE
|
||||
|
@ -0,0 +1,3 @@
|
||||
The asyncio REPL now runs :data:`sys.__interactivehook__` on startup. The
|
||||
default implementation of :data:`sys.__interactivehook__` provides
|
||||
auto-completion to the asyncio REPL. Patch contributed by Rémi Lapeyre.
|
Loading…
Reference in New Issue
Block a user