8000 all: handle SystemExit by aisk · Pull Request #243 · go-python/gpython · GitHub
[go: up one dir, main page]

Skip to content

all: handle SystemExit #243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 3, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Reduce code duplication
  • Loading branch information
aisk committed Jul 3, 2025
commit ecdd85c5c8a70e999fbfce4eab385c4d603c6ab3
72 changes: 38 additions & 34 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,43 +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 {
if py.IsException(py.SystemExit, err) {
args := err.(py.ExceptionInfo).Value.(*py.Exception).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 {
msg, err := py.ReprAsString(args)
if err != nil {
fmt.Fprintln(os.Stderr, err)
} else {
fmt.Fprintln(os.Stderr, msg)
}
os.Exit(1)
}
}
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
}
41 changes: 7 additions & 34 deletions repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package repl

import (
"fmt"
"os"
"sort"
"strings"

Expand Down Expand Up @@ -67,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 @@ -77,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 @@ -98,50 +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) {
args := err.(py.ExceptionInfo).Value.(*py.Exception).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 {
msg, err := py.ReprAsString(args)
if err != nil {
fmt.Fprintln(os.Stderr, err)
} else {
fmt.Fprintln(os.Stderr, msg)
}
os.Exit(1)
}
return err
}
py.TracebackDump(err)
}
return nil
}

// WordCompleter takes the currently edited line with the cursor
Expand Down
Loading
0