Skip to content

feat: Add support for REPLs #2723

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

Merged
merged 32 commits into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
34cd838
Add support for REPLs
philsc Apr 1, 2025
d0dce4f
Merge remote-tracking branch 'upstream/main' into HEAD
philsc Apr 8, 2025
06cef67
Remove the macro-generator
philsc Apr 8, 2025
6c2dd35
revert some unnecessary changes
philsc Apr 8, 2025
fc9cdeb
Generate main.py so we can get independent of ipython
philsc Apr 8, 2025
a2be313
precommit
philsc Apr 8, 2025
f6dc039
fix a couple of comments
philsc Apr 8, 2025
b0b83ab
Delete unnecessary code
philsc Apr 8, 2025
402faf5
Undo bootstrap hook for now
philsc Apr 8, 2025
66d216d
prevent import leaks
philsc Apr 8, 2025
ac5d58c
simplify design a bit
philsc Apr 8, 2025
9c9c8b0
clean up some more
philsc Apr 8, 2025
1d3de79
run pre-commit
philsc Apr 8, 2025
d8b1125
add docstring
philsc Apr 8, 2025
e05f9af
clean up naming a tad
philsc Apr 8, 2025
9121abd
removed dead code
philsc Apr 8, 2025
8635890
Merge remote-tracking branch 'upstream/main' into HEAD
philsc Apr 20, 2025
8a00d32
Merge remote-tracking branch 'upstream/main' into HEAD
philsc Apr 28, 2025
d48433b
Incorporate some of the feedback
philsc Apr 28, 2025
2aa2a27
more feedback incorporated
philsc Apr 28, 2025
2c6f544
add tests
philsc Apr 28, 2025
200e5e8
precommit
philsc Apr 28, 2025
5f46437
add tests
philsc Apr 28, 2025
98064dc
precommit
philsc Apr 28, 2025
0ae8a81
Merge remote-tracking branch 'origin/main' into HEAD
philsc May 4, 2025
32c436e
Fix the missing banner on startup
philsc May 4, 2025
863e970
precommit
philsc May 4, 2025
b769fb1
add doc
philsc May 4, 2025
44dda2f
add some docs
philsc May 4, 2025
d9bed35
flesh out docs a bit
philsc May 5, 2025
f1a68ab
Merge branch 'main' into dev/philsc/repl
aignas May 15, 2025
520c980
docs changes
aignas May 15, 2025
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
33 changes: 33 additions & 0 deletions python/bin/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("//python/private:interpreter.bzl", _interpreter_binary = "interpreter_binary")
load("//python/private:repl.bzl", "py_repl_binary")

filegroup(
name = "distribution",
Expand All @@ -22,3 +23,35 @@ label_flag(
name = "python_src",
build_setting_default = "//python:none",
)

py_repl_binary(
name = "repl",
stub = ":repl_stub",
visibility = ["//visibility:public"],
deps = [
":repl_dep",
":repl_stub_dep",
],
)

# The user can replace this with their own stub. E.g. they can use this to
# import ipython instead of the default shell.
label_flag(
name = "repl_stub",
build_setting_default = "repl_stub.py",
)

# The user can modify this flag to make an interpreter shell library available
# for the stub. E.g. if they switch the stub for an ipython-based one, then they
# can point this at their version of ipython.
label_flag(
name = "repl_stub_dep",
build_setting_default = "//python/private:empty",
)

# The user can modify this flag to make arbitrary PyInfo targets available for
# import on the REPL.
label_flag(
name = "repl_dep",
build_setting_default = "//python/private:empty",
)
3 changes: 3 additions & 0 deletions python/bin/repl_stub.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import code

code.interact()
4 changes: 4 additions & 0 deletions python/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,10 @@ current_interpreter_executable(
visibility = ["//visibility:public"],
)

py_library(
name = "empty",
)

sentinel(
name = "sentinel",
)
67 changes: 67 additions & 0 deletions python/private/repl.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright 2025 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Implementation of the rules to expose a REPL."""

load("//python:py_binary.bzl", "py_binary")

def _generate_repl_main_impl(ctx):
stub_repo = ctx.attr.stub.label.repo_name or ctx.workspace_name
stub_path = "/".join([stub_repo, ctx.file.stub.short_path])

ctx.actions.expand_template(
template = ctx.file._template,
output = ctx.outputs.out,
substitutions = {
"%stub_path%": stub_path,
},
)

_generate_repl_main = rule(
implementation = _generate_repl_main_impl,
attrs = {
"out": attr.output(
mandatory = True,
),
"stub": attr.label(
mandatory = True,
allow_single_file = True,
),
"_template": attr.label(
default = "//python/private:repl_template.py",
allow_single_file = True,
),
},
)

def py_repl_binary(name, stub, deps = [], data = [], **kwargs):
_generate_repl_main(
name = "%s_py" % name,
stub = stub,
out = "%s.py" % name,
)

py_binary(
name = name,
srcs = [
":%s.py" % name,
],
data = data + [
stub,
],
deps = deps + [
"//python/runfiles",
],
**kwargs
)
28 changes: 28 additions & 0 deletions python/private/repl_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import os
import runpy
from pathlib import Path

from python.runfiles import runfiles

STUB_PATH = "%stub_path%"


def start_repl():
# Simulate Python's behavior when a valid startup script is defined by the
# PYTHONSTARTUP variable. If this file path fails to load, print the error
# and revert to the default behavior.
if startup_file := os.getenv("PYTHONSTARTUP"):
try:
source_code = Path(startup_file).read_text()
except Exception as error:
print(f"{type(error).__name__}: {error}")
else:
compiled_code = compile(source_code, filename=startup_file, mode="exec")
eval(compiled_code, {})

bazel_runfiles = runfiles.Create()
runpy.run_path(bazel_runfiles.Rlocation(STUB_PATH), run_name="__main__")


if __name__ == "__main__":
start_repl()