Skip to content
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
31 changes: 31 additions & 0 deletions stdlib/builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func init() {
py.MustNewMethod("divmod", builtin_divmod, 0, divmod_doc),
py.MustNewMethod("eval", py.InternalMethodEval, 0, eval_doc),
py.MustNewMethod("exec", py.InternalMethodExec, 0, exec_doc),
py.MustNewMethod("exit", builtin_exit, 0, exit_doc),
// py.MustNewMethod("format", builtin_format, 0, format_doc),
py.MustNewMethod("getattr", builtin_getattr, 0, getattr_doc),
py.MustNewMethod("globals", py.InternalMethodGlobals, 0, globals_doc),
Expand All @@ -61,6 +62,7 @@ func init() {
py.MustNewMethod("ord", builtin_ord, 0, ord_doc),
py.MustNewMethod("pow", builtin_pow, 0, pow_doc),
py.MustNewMethod("print", builtin_print, 0, print_doc),
py.MustNewMethod("quit", builtin_quit, 0, quit_doc),
py.MustNewMethod("repr", builtin_repr, 0, repr_doc),
py.MustNewMethod("round", builtin_round, 0, round_doc),
py.MustNewMethod("setattr", builtin_setattr, 0, setattr_doc),
Expand Down Expand Up @@ -1262,6 +1264,35 @@ func builtin_input(self py.Object, args py.Tuple) (py.Object, error) {
return line, nil
}

const exit_doc = `exit([status])
Exit the interpreter by raising SystemExit(status).`

const quit_doc = `quit([status])
Alias for exit().`

func builtin_exit(self py.Object, args py.Tuple) (py.Object, error) {
return builtinExit("exit", args)
}

func builtin_quit(self py.Object, args py.Tuple) (py.Object, error) {
return builtinExit("quit", args)
}

func builtinExit(name string, args py.Tuple) (py.Object, error) {
var exitCode py.Object
err := py.UnpackTuple(args, nil, name, 0, 1, &exitCode)
if err != nil {
return nil, err
}
exc, err := py.ExceptionNew(py.SystemExit, args, nil)
if err != nil {
return nil, err
}
return nil, exc.(*py.Exception)
}

const locals_doc = `locals() -> dictionary
Update and return a dictionary containing the current scope's local variables.`
Expand Down
9 changes: 9 additions & 0 deletions stdlib/builtin/tests/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@
assert exec("b = a+100", glob) == None
assert glob["b"] == 200

doc="exit/quit"
assertRaises(SystemExit, exit)
assertRaises(SystemExit, exit, 0)
assertRaises(SystemExit, exit, 3)
assertRaises(SystemExit, quit)
assertRaises(SystemExit, quit, "bye")
assertRaises(TypeError, exit, 1, 2)
assertRaises(TypeError, quit, 1, 2)

doc="getattr"
class C:
def __init__(self):
Expand Down
Loading