Skip to content

Commit 9a4df56

Browse files
committed
Fix comments in REPL - fixes #78
Before this change, entering a comment in the REPL caused the REPL to read the comment indefinitely effectively breaking it. After this change the behaviour is the same as pypy. The cpython behaviour is slightly different printing a '...' after a comment line. This is rather illogical and difficult to emulate properly.
1 parent 7bc4005 commit 9a4df56

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

repl/repl.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,13 @@ func (r *REPL) Run(line string) {
8282
// FIXME detect EOF properly!
8383
errText := err.Error()
8484
if strings.Contains(errText, "unexpected EOF while parsing") || strings.Contains(errText, "EOF while scanning triple-quoted string literal") {
85-
r.continuation = true
86-
r.previous += string(line) + "\n"
87-
r.term.SetPrompt(ContinuationPrompt)
85+
stripped := strings.TrimSpace(toCompile)
86+
isComment := len(stripped) > 0 && stripped[0] == '#'
87+
if !isComment {
88+
r.continuation = true
89+
r.previous += string(line) + "\n"
90+
r.term.SetPrompt(ContinuationPrompt)
91+
}
8892
return
8993
}
9094
}

repl/repl_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ func TestREPL(t *testing.T) {
6767

6868
r.Run("if")
6969
rt.assert(t, "compileError", NormalPrompt, "Compile error: \n File \"<string>\", line 1, offset 2\n if\n\n\nSyntaxError: 'invalid syntax'")
70+
71+
// test comments in the REPL work properly
72+
r.Run("# this is a comment")
73+
rt.assert(t, "comment", NormalPrompt, "")
74+
r.Run("a = 42")
75+
rt.assert(t, "comment continuation", NormalPrompt, "")
76+
r.Run("a")
77+
rt.assert(t, "comment check", NormalPrompt, "42")
7078
}
7179

7280
func TestCompleter(t *testing.T) {

0 commit comments

Comments
 (0)