Skip to content

Commit eb31442

Browse files
committed
Conditionally import readline
The readline module is not avialable on all platforms.
1 parent f13d7fd commit eb31442

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

python/bin/repl_stub.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@
1717
console_locals = globals().copy()
1818

1919
import code
20-
import readline
2120
import rlcompleter
2221
import sys
2322

23+
try:
24+
import readline
25+
except ImportError:
26+
pass # readline is not available on all platforms
27+
28+
2429
class DynamicCompleter(rlcompleter.Completer):
2530
"""
2631
A custom completer that dynamically updates its namespace to include new
@@ -47,18 +52,19 @@ def complete(self, text, state):
4752
sys.ps1 = ""
4853
sys.ps2 = ""
4954

50-
# Set up tab completion.
51-
completer = DynamicCompleter(console_locals)
52-
readline.set_completer(completer.complete)
55+
if "readline" in globals():
56+
# Set up tab completion.
57+
completer = DynamicCompleter(console_locals)
58+
readline.set_completer(completer.complete)
5359

54-
# TODO(jpwoodbu): Use readline.backend instead of readline.__doc__ once we can depend on having
55-
# Python >=3.13.
56-
if 'libedit' in readline.__doc__: # type: ignore
57-
readline.parse_and_bind("bind ^I rl_complete")
58-
elif 'GNU readline' in readline.__doc__: # type: ignore
59-
readline.parse_and_bind("tab: complete")
60-
else:
61-
print('Could not enable tab completion!')
60+
# TODO(jpwoodbu): Use readline.backend instead of readline.__doc__ once we can depend on having
61+
# Python >=3.13.
62+
if "libedit" in readline.__doc__: # type: ignore
63+
readline.parse_and_bind("bind ^I rl_complete")
64+
elif "GNU readline" in readline.__doc__: # type: ignore
65+
readline.parse_and_bind("tab: complete")
66+
else:
67+
print("Could not enable tab completion!")
6268

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

0 commit comments

Comments
 (0)