Skip to content

Commit fcddd1c

Browse files
Jon Poolesbinet
Jon Poole
authored andcommitted
all: improve error logging when parsing a file
1 parent 12886a2 commit fcddd1c

File tree

6 files changed

+11
-10
lines changed

6 files changed

+11
-10
lines changed

compile/compile.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package compile
99
// FIXME kill ast.Identifier and turn into string?
1010

1111
import (
12+
"bytes"
1213
"fmt"
1314
"log"
1415
"strings"
@@ -107,7 +108,7 @@ func init() {
107108
// in addition to any features explicitly specified.
108109
func Compile(src, srcDesc string, mode py.CompileMode, futureFlags int, dont_inherit bool) (*py.Code, error) {
109110
// Parse Ast
110-
Ast, err := parser.ParseString(src, mode)
111+
Ast, err := parser.Parse(bytes.NewBufferString(src), srcDesc, mode)
111112
if err != nil {
112113
return nil, err
113114
}

main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func xmain(args []string) {
7878
_, err := py.RunFile(ctx, args[0], py.CompileOpts{}, nil)
7979
if err != nil {
8080
py.TracebackDump(err)
81-
log.Fatal(err)
81+
os.Exit(1)
8282
}
8383
}
8484
}

parser/grammar_data_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ var grammarTestData = []struct {
3333
{"b'abc' b'''123'''", "eval", "Expression(body=Bytes(s=b'abc123'))", nil, ""},
3434
{"1234", "eval", "Expression(body=Num(n=1234))", nil, ""},
3535
{"01234", "eval", "", py.SyntaxError, "illegal decimal with leading zero"},
36-
{"1234d", "eval", "", py.SyntaxError, "invalid syntax"},
37-
{"1234d", "exec", "", py.SyntaxError, "invalid syntax"},
36+
{"1234d", "eval", "", py.SyntaxError, "unexpected EOF while parsing"},
37+
{"1234d", "exec", "", py.SyntaxError, "unexpected EOF while parsing"},
3838
{"1234d", "single", "", py.SyntaxError, "unexpected EOF while parsing"},
3939
{"0x1234", "eval", "Expression(body=Num(n=4660))", nil, ""},
4040
{"12.34", "eval", "Expression(body=Num(n=12.34))", nil, ""},
@@ -325,10 +325,10 @@ var grammarTestData = []struct {
325325
{"pass\n", "single", "Interactive(body=[Pass()])", nil, ""},
326326
{"if True:\n pass\n\n", "single", "Interactive(body=[If(test=NameConstant(value=True), body=[Pass()], orelse=[])])", nil, ""},
327327
{"while True:\n pass\nelse:\n return\n", "single", "Interactive(body=[While(test=NameConstant(value=True), body=[Pass()], orelse=[Return(value=None)])])", nil, ""},
328-
{"a='potato", "eval", "", py.SyntaxError, "invalid syntax"},
328+
{"a='potato", "eval", "", py.SyntaxError, "unexpected EOF while parsing"},
329329
{"a='potato", "exec", "", py.SyntaxError, "EOL while scanning string literal"},
330330
{"a='potato", "single", "", py.SyntaxError, "EOL while scanning string literal"},
331-
{"a='''potato", "eval", "", py.SyntaxError, "invalid syntax"},
331+
{"a='''potato", "eval", "", py.SyntaxError, "unexpected EOF while parsing"},
332332
{"a='''potato", "exec", "", py.SyntaxError, "EOF while scanning triple-quoted string literal"},
333333
{"a='''potato", "single", "", py.SyntaxError, "EOF while scanning triple-quoted string literal"},
334334
}

parser/lexer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,7 @@ func (x *yyLex) SyntaxErrorf(format string, a ...interface{}) {
916916
func (x *yyLex) ErrorReturn() error {
917917
if x.error {
918918
if x.errorString == "" {
919-
if x.eof && x.interactive {
919+
if x.eof {
920920
x.errorString = "unexpected EOF while parsing"
921921
} else {
922922
x.errorString = "invalid syntax"

py/traceback.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ func TracebackDump(err interface{}) {
6464
case *ExceptionInfo:
6565
e.TracebackDump(os.Stderr)
6666
case *Exception:
67-
fmt.Fprintf(os.Stderr, "Exception %#v\n", e)
67+
fmt.Fprintf(os.Stderr, "Exception %v\n", e)
6868
fmt.Fprintf(os.Stderr, "-- No traceback available --\n")
6969
default:
70-
fmt.Fprintf(os.Stderr, "Error %#v\n", err)
70+
fmt.Fprintf(os.Stderr, "Error %v\n", err)
7171
fmt.Fprintf(os.Stderr, "-- No traceback available --\n")
7272
}
7373
}

repl/repl_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func TestREPL(t *testing.T) {
6363
rt.assert(t, "multi#5", NormalPrompt, "45")
6464

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

6868
// test comments in the REPL work properly
6969
r.Run("# this is a comment")

0 commit comments

Comments
 (0)