-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
gh-72327: Add help message for pip in REPL #8536
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
1e02cdb
49a8299
b762543
0784d5a
ab29fa1
5bb0a8d
239fd5d
2d302e6
ae30adf
2bb1e38
f9bb5c8
e092342
0e6d9e2
8122a6e
2cd4867
5034571
36c8256
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -390,6 +390,55 @@ def setcopyright(): | |
files, dirs) | ||
|
||
|
||
def sethelper(): | ||
builtins.help = _sitebuiltins._Helper() | ||
|
||
def _register_readline(): | ||
""" | ||
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. | ||
""" | ||
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. | ||
readline_doc = getattr(readline, '__doc__', '') | ||
if readline_doc is not None and 'libedit' in readline_doc: | ||
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. | ||
# 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 = os.path.join(os.path.expanduser('~'), | ||
'.python_history') | ||
try: | ||
readline.read_history_file(history) | ||
except OSError: | ||
pass | ||
atexit.register(readline.write_history_file, history) | ||
|
||
|
||
def _register_detect_pip_usage_in_repl(): | ||
old_excepthook = sys.excepthook | ||
|
||
|
@@ -408,58 +457,17 @@ def detect_pip_usage_in_repl(typ, value, traceback): | |
sys.excepthook = detect_pip_usage_in_repl | ||
serhiy-storchaka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def sethelper(): | ||
builtins.help = _sitebuiltins._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 overridden in the sitecustomize or usercustomize module, | ||
or in a PYTHONSTARTUP file. | ||
def _set_interactive_hook(): | ||
"""Register a sys.__interactivehook__ to: | ||
- Enable default readline configuration on interactive prompts. | ||
- Register an excepthook to detect pip usage in the REPL. | ||
""" | ||
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. | ||
readline_doc = getattr(readline, '__doc__', '') | ||
if readline_doc is not None and 'libedit' in readline_doc: | ||
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 | ||
def interactivehook(): | ||
_register_readline() | ||
_register_detect_pip_usage_in_repl() | ||
|
||
if readline.get_current_history_length() == 0: | ||
# If no history was loaded, default to .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 = os.path.join(os.path.expanduser('~'), | ||
'.python_history') | ||
try: | ||
readline.read_history_file(history) | ||
except OSError: | ||
pass | ||
atexit.register(readline.write_history_file, history) | ||
sys.__interactivehook__ = interactivehook | ||
|
||
sys.__interactivehook__ = register_readline | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this change to the way Reviewing 0e6d9e2 I think the public API to preserve would be:
|
||
|
||
def venv(known_paths): | ||
global PREFIXES, ENABLE_USER_SITE | ||
|
@@ -576,12 +584,8 @@ def main(): | |
setquit() | ||
setcopyright() | ||
sethelper() | ||
# Detect REPL by prompt, -i flag or PYTHONINSPECT env var being set. | ||
if (hasattr(sys, 'ps1') or sys.flags.interactive or | ||
os.environ.get('PYTHONINSPECT')): | ||
_register_detect_pip_usage_in_repl() | ||
if not sys.flags.isolated: | ||
enablerlcompleter() | ||
_set_interactive_hook() | ||
execsitecustomize() | ||
if ENABLE_USER_SITE: | ||
execusercustomize() | ||
|
Uh oh!
There was an error while loading. Please reload this page.