Skip to content

Commit c2200e9

Browse files
authoredMay 17, 2024··
Merge pull request #2587 from Kodiologist/repl2
Execute `hy -i < code.hy` in the REPL environment
2 parents 3ba2c87 + 33d22aa commit c2200e9

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed
 

‎AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,4 @@
109109
* Andrey Vlasovskikh <andrey.vlasovskikh@gmail.com>
110110
* Joseph LaFreniere <joseph@lafreniere.xyz>
111111
* Daniel Tan <danieltanfh95@gmail.com>
112+
* Zhan Tang <tz59pk@gmail.com>

‎NEWS.rst

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Bug Fixes
2525
------------------------------
2626
* Tracebacks now point to the correct code in more cases.
2727
* `help` should no longer crash when objects are missing docstrings.
28+
* `hy -i < script.hy` now executes `script.hy` inside the REPL environment,
29+
like Python.
2830

2931
0.28.0 (released 2024-01-05)
3032
=============================

‎hy/cmdline.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,7 @@ def proc_opt(opt, arg=None, item=None, i=None):
262262
return 0
263263
elif action == "run_script_stdin":
264264
sys.argv = argv
265-
if repl:
266-
source = sys.stdin
267-
filename = 'stdin'
268-
else:
265+
if not repl:
269266
return run_command(sys.stdin.read(), filename="<stdin>")
270267
elif action == "run_script_file":
271268
sys.argv = argv

‎tests/test_bin.py

+12
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ def test_stdin():
8080
assert "TU" in out
8181

8282

83+
def test_i_flag_repl_env():
84+
# If a program is passed in through standard input, it's evaluated
85+
# in the REPL environment.
86+
code = '(import sys) (if (hasattr sys "ps1") "Yeppers" "Nopers")'
87+
out, _ = run_cmd("hy -i", code)
88+
assert "Yeppers" in out
89+
# With `-c`, on the other hand, the code is run before the REPL is
90+
# launched.
91+
out, _ = run_cmd(['hy', '-i', '-c', code])
92+
assert "Nopers" in out
93+
94+
8395
def test_error_parts_length():
8496
"""Confirm that exception messages print arrows surrounding the affected
8597
expression."""

0 commit comments

Comments
 (0)
Please sign in to comment.