Skip to content

Commit 32c436e

Browse files
committed
Fix the missing banner on startup
1 parent 0ae8a81 commit 32c436e

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

python/bin/repl_stub.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,16 @@
1414
"""
1515

1616
import code
17+
import sys
1718

18-
code.interact()
19+
if sys.stdin.isatty():
20+
# Use the default options.
21+
exitmsg = None
22+
else:
23+
# On a non-interactive console, we want to suppress the >>> and the exit message.
24+
exitmsg = ""
25+
sys.ps1 = ""
26+
sys.ps2 = ""
27+
28+
# We set the banner to an empty string because the repl_template.py file already prints the banner.
29+
code.interact(banner="", exitmsg=exitmsg)

python/private/repl.bzl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ def _generate_repl_main_impl(ctx):
66
stub_repo = ctx.attr.stub.label.repo_name or ctx.workspace_name
77
stub_path = "/".join([stub_repo, ctx.file.stub.short_path])
88

9+
out = ctx.actions.declare_file(ctx.label.name + ".py")
10+
911
# Point the generated main file at the stub.
1012
ctx.actions.expand_template(
1113
template = ctx.file._template,
12-
output = ctx.outputs.out,
14+
output = out,
1315
substitutions = {
1416
"%stub_path%": stub_path,
1517
},
1618
)
1719

20+
return [DefaultInfo(files=depset([out]))]
21+
1822
_generate_repl_main = rule(
1923
implementation = _generate_repl_main_impl,
2024
attrs = {
21-
"out": attr.output(
22-
mandatory = True,
23-
doc = "The path to the file to generate.",
24-
),
2525
"stub": attr.label(
2626
mandatory = True,
2727
allow_single_file = True,
@@ -40,6 +40,8 @@ Generates a "main" script for a py_binary target that starts a Python REPL.
4040
The template is designed to take care of the majority of the logic. The user
4141
customizes the exact shell that will be started via the stub. The stub is a
4242
simple shell script that imports the desired shell and then executes it.
43+
44+
The target's name is used for the output filename (with a .py extension).
4345
""",
4446
)
4547

@@ -64,14 +66,14 @@ def py_repl_binary(name, stub, deps = [], data = [], **kwargs):
6466
_generate_repl_main(
6567
name = "%s_py" % name,
6668
stub = stub,
67-
out = "%s.py" % name,
6869
)
6970

7071
_py_binary(
7172
name = name,
7273
srcs = [
73-
":%s.py" % name,
74+
":%s_py" % name,
7475
],
76+
main = "%s_py.py" % name,
7577
data = data + [
7678
stub,
7779
],

python/private/repl_template.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import runpy
3+
import sys
34
from pathlib import Path
45

56
from python.runfiles import runfiles
@@ -8,6 +9,11 @@
89

910

1011
def start_repl():
12+
if sys.stdin.isatty():
13+
# Print the banner similar to how python does it on startup when running interactively.
14+
cprt = 'Type "help", "copyright", "credits" or "license" for more information.'
15+
sys.stderr.write("Python %s on %s\n%s\n" % (sys.version, sys.platform, cprt))
16+
1117
# Simulate Python's behavior when a valid startup script is defined by the
1218
# PYTHONSTARTUP variable. If this file path fails to load, print the error
1319
# and revert to the default behavior.

0 commit comments

Comments
 (0)