Skip to content
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

Execute hy -i < code.hy in the REPL environment #2587

Merged
merged 2 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,4 @@
* Andrey Vlasovskikh <andrey.vlasovskikh@gmail.com>
* Joseph LaFreniere <joseph@lafreniere.xyz>
* Daniel Tan <danieltanfh95@gmail.com>
* Zhan Tang <tz59pk@gmail.com>
2 changes: 2 additions & 0 deletions NEWS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Bug Fixes
------------------------------
* Tracebacks now point to the correct code in more cases.
* `help` should no longer crash when objects are missing docstrings.
* `hy -i < script.hy` now executes `script.hy` inside the REPL environment,
like Python.

0.28.0 (released 2024-01-05)
=============================
Expand Down
5 changes: 1 addition & 4 deletions hy/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,7 @@ def proc_opt(opt, arg=None, item=None, i=None):
return 0
elif action == "run_script_stdin":
sys.argv = argv
if repl:
source = sys.stdin
filename = 'stdin'
else:
if not repl:
return run_command(sys.stdin.read(), filename="<stdin>")
elif action == "run_script_file":
sys.argv = argv
Expand Down
12 changes: 12 additions & 0 deletions tests/test_bin.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ def test_stdin():
assert "TU" in out


def test_i_flag_repl_env():
# If a program is passed in through standard input, it's evaluated
# in the REPL environment.
code = '(import sys) (if (hasattr sys "ps1") "Yeppers" "Nopers")'
out, _ = run_cmd("hy -i", code)
assert "Yeppers" in out
# With `-c`, on the other hand, the code is run before the REPL is
# launched.
out, _ = run_cmd(['hy', '-i', '-c', code])
assert "Nopers" in out


def test_error_parts_length():
"""Confirm that exception messages print arrows surrounding the affected
expression."""
Expand Down