Skip to content

Commit 2aa2a27

Browse files
committed
more feedback incorporated
1 parent d48433b commit 2aa2a27

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

python/private/repl.bzl

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
"""Implementation of the rules to expose a REPL."""
22

3-
load("//python:py_binary.bzl", "py_binary")
3+
load("//python:py_binary.bzl", _py_binary="py_binary")
44

55
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+
# Point the generated main file at the stub.
910
ctx.actions.expand_template(
1011
template = ctx.file._template,
1112
output = ctx.outputs.out,
@@ -33,17 +34,40 @@ _generate_repl_main = rule(
3334
doc = "The template to use for generating `out`.",
3435
),
3536
},
37+
doc = """\
38+
Generates a "main" script for a py_binary target that starts a Python REPL.
39+
40+
The template is designed to take care of the majority of the logic. The user
41+
customizes the exact shell that will be started via the stub. The stub is a
42+
simple shell script that imports the desired shell and then executes it.
43+
""",
3644
)
3745

3846
def py_repl_binary(name, stub, deps = [], data = [], **kwargs):
39-
"""A
47+
"""A py_binary target that executes a REPL when run.
48+
49+
The stub is the script that ultimately decides which shell the REPL will run.
50+
It can be as simple as this:
51+
52+
import code
53+
code.interact()
54+
55+
Or it can load something like IPython instead.
56+
57+
Args:
58+
name: Name of the generated py_binary target.
59+
stub: The script that invokes the shell.
60+
deps: The dependencies of the py_binary.
61+
data: The runtime dependencies of the py_binary.
62+
**kwargs: Forwarded to the py_binary.
63+
"""
4064
_generate_repl_main(
4165
name = "%s_py" % name,
4266
stub = stub,
4367
out = "%s.py" % name,
4468
)
4569

46-
py_binary(
70+
_py_binary(
4771
name = name,
4872
srcs = [
4973
":%s.py" % name,

python/private/repl_template.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ def start_repl():
1111
# Simulate Python's behavior when a valid startup script is defined by the
1212
# PYTHONSTARTUP variable. If this file path fails to load, print the error
1313
# and revert to the default behavior.
14+
#
15+
# See upstream for more information:
16+
# https://docs.python.org/3/using/cmdline.html#envvar-PYTHONSTARTUP
1417
if startup_file := os.getenv("PYTHONSTARTUP"):
1518
try:
1619
source_code = Path(startup_file).read_text()

0 commit comments

Comments
 (0)