From cb0367e708d75dab214723fcb60e8e46c810bd83 Mon Sep 17 00:00:00 2001 From: rbrtberglund Date: Sat, 18 May 2019 16:14:43 +0200 Subject: [PATCH] Add exit/quit builtin function --- vm/src/builtins.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/vm/src/builtins.rs b/vm/src/builtins.rs index 3250bef80..f87ea8e7e 100644 --- a/vm/src/builtins.rs +++ b/vm/src/builtins.rs @@ -613,6 +613,24 @@ impl Printer for std::io::StdoutLock<'_> { } } +pub fn builtin_exit(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { + arg_check!(vm, args, required = [], optional = [(object, None)]); + match object { + Some(value) => match i32::try_from_object(&vm, value.clone()) { + Ok(code) => std::process::exit(code), + _ => { + let stdout = io::stdout(); + let mut printer: Box = Box::new(stdout.lock()); + printer.write(vm, value.clone())?; + printer.write(vm, "\n".into_pyobject(vm).unwrap())?; + printer.flush(vm)?; + } + }, + _ => {} + } + std::process::exit(0); +} + pub fn builtin_print(objects: Args, options: PrintOptions, vm: &VirtualMachine) -> PyResult<()> { let stdout = io::stdout(); @@ -823,6 +841,8 @@ pub fn make_module(vm: &VirtualMachine, module: PyObjectRef) { "tuple" => ctx.tuple_type(), "type" => ctx.type_type(), "zip" => ctx.zip_type(), + "exit" => ctx.new_rustfunc(builtin_exit), + "quit" => ctx.new_rustfunc(builtin_exit), "__import__" => ctx.new_rustfunc(builtin_import), // Constants