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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
30 changes: 30 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,36 @@ func xmain(args []string) {
} else {
_, err := py.RunFile(ctx, args[0], py.CompileOpts{}, 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)
}
}
py.TracebackDump(err)
os.Exit(1)
}
Expand Down
31 changes: 31 additions & 0 deletions repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package repl

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

Expand Down Expand Up @@ -109,6 +110,36 @@ func (r *REPL) Run(line string) {
}
_, 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)
}
}
py.TracebackDump(err)
}
}
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