-
-
Notifications
You must be signed in to change notification settings - Fork 611
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
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 d0dce4f
Merge remote-tracking branch 'upstream/main' into HEAD
philsc 06cef67
Remove the macro-generator
philsc 6c2dd35
revert some unnecessary changes
philsc fc9cdeb
Generate main.py so we can get independent of ipython
philsc a2be313
precommit
philsc f6dc039
fix a couple of comments
philsc b0b83ab
Delete unnecessary code
philsc 402faf5
Undo bootstrap hook for now
philsc 66d216d
prevent import leaks
philsc ac5d58c
simplify design a bit
philsc 9c9c8b0
clean up some more
philsc 1d3de79
run pre-commit
philsc d8b1125
add docstring
philsc e05f9af
clean up naming a tad
philsc 9121abd
removed dead code
philsc 8635890
Merge remote-tracking branch 'upstream/main' into HEAD
philsc 8a00d32
Merge remote-tracking branch 'upstream/main' into HEAD
philsc d48433b
Incorporate some of the feedback
philsc 2aa2a27
more feedback incorporated
philsc 2c6f544
add tests
philsc 200e5e8
precommit
philsc 5f46437
add tests
philsc 98064dc
precommit
philsc 0ae8a81
Merge remote-tracking branch 'origin/main' into HEAD
philsc 32c436e
Fix the missing banner on startup
philsc 863e970
precommit
philsc b769fb1
add doc
philsc 44dda2f
add some docs
philsc d9bed35
flesh out docs a bit
philsc f1a68ab
Merge branch 'main' into dev/philsc/repl
aignas 520c980
docs changes
aignas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import code | ||
|
||
code.interact() | ||
philsc marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
philsc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""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, | ||
aignas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
), | ||
"_template": attr.label( | ||
default = "//python/private:repl_template.py", | ||
allow_single_file = True, | ||
), | ||
}, | ||
) | ||
|
||
def py_repl_binary(name, stub, deps = [], data = [], **kwargs): | ||
philsc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_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 | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.