From 2d640784cb357418013b578c1f2fae8ff1172286 Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Mon, 13 May 2024 14:04:43 -0400 Subject: [PATCH] Execute `hy -i < code.hy` in the REPL environment Co-authored-by: moonveil --- NEWS.rst | 2 ++ hy/cmdline.py | 5 +---- tests/test_bin.py | 12 ++++++++++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/NEWS.rst b/NEWS.rst index 45c6fd334..75db958b7 100644 --- a/NEWS.rst +++ b/NEWS.rst @@ -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) ============================= diff --git a/hy/cmdline.py b/hy/cmdline.py index bdafee104..7819299df 100644 --- a/hy/cmdline.py +++ b/hy/cmdline.py @@ -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="") elif action == "run_script_file": sys.argv = argv diff --git a/tests/test_bin.py b/tests/test_bin.py index d9a97ea13..71f36df6f 100644 --- a/tests/test_bin.py +++ b/tests/test_bin.py @@ -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."""