shell calls sys.__interativehook__

This will set up readline tab and history completion by default.
This commit is contained in:
David Lord 2021-04-14 10:01:32 -07:00
parent 64213fc021
commit 32272da9ac
No known key found for this signature in database
GPG key ID: 7A1C87E3F5BC42A8
2 changed files with 20 additions and 0 deletions

View file

@ -70,6 +70,8 @@ Unreleased
- Support async views, error handlers, before and after request, and
teardown functions. :pr:`3412`
- Support nesting blueprints. :issue:`593, 1548`, :pr:`3923`
- ``flask shell`` sets up tab and history completion like the default
``python`` shell if ``readline`` is installed. :issue:`3941`
Version 1.1.2

View file

@ -887,6 +887,24 @@ def shell_command():
ctx.update(app.make_shell_context())
# Site, customize, or startup script can set a hook to call when
# entering interactive mode. The default one sets up readline with
# tab and history completion.
interactive_hook = getattr(sys, "__interactivehook__", None)
if interactive_hook is not None:
try:
import readline
from rlcompleter import Completer
except ImportError:
pass
else:
# rlcompleter uses __main__.__dict__ by default, which is
# flask.__main__. Use the shell context instead.
readline.set_completer(Completer(ctx).complete)
interactive_hook()
code.interact(banner=banner, local=ctx)