Skip to content

Commit

Permalink
Merge pull request #2587 from Kodiologist/repl2
Browse files Browse the repository at this point in the history
Execute `hy -i < code.hy` in the REPL environment
  • Loading branch information
Kodiologist committed May 17, 2024
2 parents 3ba2c87 + 33d22aa commit c2200e9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
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

0 comments on commit c2200e9

Please sign in to comment.