Skip to content

Commit 9d15dbe

Browse files
fix multipe stdouts in loop
1 parent de2bd5f commit 9d15dbe

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

gdrepl/gdserverv4.gd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ class Session:
123123
var identation = " ".repeat(len(line.rstrip(" ")) - len(line.rstrip(" ").lstrip(" ")))
124124
_local += " " + identation + "print(\"" + STDOUT_MARKER_START + "\")" + "\n"
125125

126-
if is_print_call:
127-
# Replace with pass to keep control structures valid
126+
if is_print_call and i < last_index:
127+
# Replace with pass to avoid repeated output from previous executions
128128
var indentation = " ".repeat(len(line) - len(line.lstrip(" \t")))
129129
_local += " " + indentation + "pass\n"
130130
else:

tests/test_e2e.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,41 @@ def test_code_after_loop_execution(self, repl):
246246
repl.expect(r"-> 123", timeout=10)
247247
repl.expect(">>>", timeout=10)
248248

249+
def test_multiple_prints_in_loop(self, repl):
250+
"""Test that multiple print statements in a loop all produce output."""
251+
# Execute a for loop with multiple print statements
252+
repl.sendline("for i in range(3):")
253+
repl.expect(r"\.\.\.", timeout=10)
254+
255+
repl.sendline(" print(i)")
256+
repl.expect(r"\.\.\.", timeout=10)
257+
258+
repl.sendline(" print('and')")
259+
repl.expect(r"\.\.\.", timeout=10)
260+
261+
# Send empty line to finish the loop
262+
repl.sendline("")
263+
264+
# Should see output from BOTH print statements for each iteration
265+
repl.expect("0", timeout=10)
266+
repl.expect("and", timeout=10)
267+
repl.expect("1", timeout=10)
268+
repl.expect("and", timeout=10)
269+
repl.expect("2", timeout=10)
270+
repl.expect("and", timeout=10)
271+
repl.expect(">>>", timeout=10)
272+
273+
# Now execute code after the loop - the loop should NOT re-execute
274+
repl.sendline("99")
275+
repl.expect(r"-> 99", timeout=10)
276+
277+
# Get everything before the prompt to check no repeated output
278+
repl.expect(">>>", timeout=10)
279+
280+
# Verify that we don't see the loop output repeated
281+
# (the 'before' buffer should only contain "-> 99" and not "0", "1", "2", "and")
282+
assert repl.before.count("and") == 0, "Loop output should not repeat"
283+
249284

250285
@pytest.mark.slow
251286
class TestREPLPersistence:

0 commit comments

Comments
 (0)