Skip to content

fix: repl tab completion #3114

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions python/bin/repl_stub.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,40 @@
sys.ps1 = ""
sys.ps2 = ""

# Set up tab completion.
try:
import readline
import rlcompleter

class DynamicCompleter(rlcompleter.Completer):
"""
A custom completer that dynamically updates its namespace to include new
imports made within the interactive session.
"""
def __init__(self, namespace):
# Store a reference to the namespace, not a copy, so that changes to the namespace are
# reflected.
self.namespace = namespace

def complete(self, text, state):
# Update the completer's internal namespace with the current interactive session's locals
# and globals. This is the key to making new imports discoverable.
rlcompleter.Completer.__init__(self, self.namespace)
return super().complete(text, state)

completer = DynamicCompleter(console_locals)
readline.set_completer(completer.complete)

# TODO(jpwoodbu): Use readline.backend instead of readline.__doc__ once we can depend on having
# Python >=3.13.
if "libedit" in readline.__doc__: # type: ignore
readline.parse_and_bind("bind ^I rl_complete")
elif "GNU readline" in readline.__doc__: # type: ignore
readline.parse_and_bind("tab: complete")
else:
print("Could not enable tab completion!")
except ImportError:
print("Could not enable tab completion!")

# We set the banner to an empty string because the repl_template.py file already prints the banner.
code.interact(local=console_locals, banner="", exitmsg=exitmsg)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy