Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func xmain(args []string) {
defer pprof.StopCPUProfile()
}

var err error
// IF no args, enter REPL mode
if len(args) == 0 {

Expand All @@ -69,13 +70,46 @@ func xmain(args []string) {
fmt.Printf("- go version: %s\n", runtime.Version())

replCtx := repl.New(ctx)
cli.RunREPL(replCtx)
err = cli.RunREPL(replCtx)
} else {
_, err = py.RunFile(ctx, args[0], py.CompileOpts{}, nil)
}
if err != nil {
if py.IsException(py.SystemExit, err) {
handleSystemExit(err.(py.ExceptionInfo).Value.(*py.Exception))
}
py.TracebackDump(err)
os.Exit(1)
}
}

func handleSystemExit(exc *py.Exception) {
args := exc.Args.(py.Tuple)
if len(args) == 0 {
os.Exit(0)
} else if len(args) == 1 {
if code, ok := args[0].(py.Int); ok {
c, err := code.GoInt()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
os.Exit(c)
}
msg, err := py.ReprAsString(args[0])
if err != nil {
fmt.Fprintln(os.Stderr, err)
} else {
fmt.Fprintln(os.Stderr, msg)
}
os.Exit(1)
} else {
_, err := py.RunFile(ctx, args[0], py.CompileOpts{}, nil)
msg, err := py.ReprAsString(args)
if err != nil {
py.TracebackDump(err)
os.Exit(1)
fmt.Fprintln(os.Stderr, err)
} else {
fmt.Fprintln(os.Stderr, msg)
}
os.Exit(1)
}
}
8 changes: 6 additions & 2 deletions repl/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (rl *readline) Print(out string) {
}

// RunREPL starts the REPL loop
func RunREPL(replCtx *repl.REPL) {
func RunREPL(replCtx *repl.REPL) error {
if replCtx == nil {
replCtx = repl.New(nil)
}
Expand All @@ -144,6 +144,10 @@ func RunREPL(replCtx *repl.REPL) {
if line != "" {
rl.AppendHistory(line)
}
rl.repl.Run(line)
err = rl.repl.Run(line)
if err != nil {
return err
}
}
return nil
}
14 changes: 9 additions & 5 deletions repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (r *REPL) SetUI(term UI) {
}

// Run runs a single line of the REPL
func (r *REPL) Run(line string) {
func (r *REPL) Run(line string) error {
// Override the PrintExpr output temporarily
oldPrintExpr := vm.PrintExpr
vm.PrintExpr = r.term.Print
Expand All @@ -76,13 +76,13 @@ func (r *REPL) Run(line string) {
if r.continuation {
if line != "" {
r.previous += string(line) + "\n"
return
return nil
}
}
// need +"\n" because "single" expects \n terminated input
toCompile := r.previous + string(line)
if toCompile == "" {
return
return nil
}
code, err := py.Compile(toCompile+"\n", r.prog, py.SingleMode, 0, true)
if err != nil {
Expand All @@ -97,20 +97,24 @@ func (r *REPL) Run(line string) {
r.previous += string(line) + "\n"
r.term.SetPrompt(ContinuationPrompt)
}
return
return nil
}
}
r.continuation = false
r.term.SetPrompt(NormalPrompt)
r.previous = ""
if err != nil {
r.term.Print(fmt.Sprintf("Compile error: %v", err))
return
return nil
}
_, err = r.Context.RunCode(code, r.Module.Globals, r.Module.Globals, nil)
if err != nil {
if py.IsException(py.SystemExit, err) {
return err
}
py.TracebackDump(err)
}
return nil
}

// WordCompleter takes the currently edited line with the cursor
Expand Down
6 changes: 5 additions & 1 deletion stdlib/sys/sys.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,11 @@ func sys_exit(self py.Object, args py.Tuple) (py.Object, error) {
return nil, err
}
// Raise SystemExit so callers may catch it or clean up.
return py.ExceptionNew(py.SystemExit, args, nil)
exc, err := py.ExceptionNew(py.SystemExit, args, nil)
if err != nil {
return nil, err
}
return nil, exc.(*py.Exception)
}

const getdefaultencoding_doc = `getdefaultencoding() -> string
Expand Down
Loading