Skip to content

Commit

Permalink
Fix comments in REPL - fixes #78
Browse files Browse the repository at this point in the history
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 should be exactly the same as python3/
  • Loading branch information
ncw committed Sep 15, 2019
1 parent 7bc4005 commit bcb3785
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 6 additions & 2 deletions repl/repl.go
Expand Up @@ -82,8 +82,12 @@ func (r *REPL) Run(line string) {
// FIXME detect EOF properly!
errText := err.Error()
if strings.Contains(errText, "unexpected EOF while parsing") || strings.Contains(errText, "EOF while scanning triple-quoted string literal") {
r.continuation = true
r.previous += string(line) + "\n"
stripped := strings.TrimSpace(toCompile)
isComment := len(stripped) > 0 && stripped[0] == '#'
if !isComment {
r.continuation = true
r.previous += string(line) + "\n"
}
r.term.SetPrompt(ContinuationPrompt)
return
}
Expand Down
8 changes: 8 additions & 0 deletions repl/repl_test.go
Expand Up @@ -67,6 +67,14 @@ func TestREPL(t *testing.T) {

r.Run("if")
rt.assert(t, "compileError", NormalPrompt, "Compile error: \n File \"<string>\", line 1, offset 2\n if\n\n\nSyntaxError: 'invalid syntax'")

// test comments in the REPL work properly
r.Run("# this is a comment")
rt.assert(t, "comment", ContinuationPrompt, "")
r.Run("a = 42")
rt.assert(t, "comment continuation", NormalPrompt, "")
r.Run("a")
rt.assert(t, "comment check", NormalPrompt, "42")
}

func TestCompleter(t *testing.T) {
Expand Down

0 comments on commit bcb3785

Please sign in to comment.