Skip to content

Change something related to exit #1483

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 5 commits into from
Oct 7, 2019
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
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Lib/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def _get_exports_list(module):

import _os
from _os import *
from _os import _exit
__all__.extend(_get_exports_list(_os))
del _os

Expand Down
35 changes: 35 additions & 0 deletions tests/snippets/exit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,38 @@

with assert_raises(SystemExit):
exit()

with assert_raises(SystemExit):
exit(None)

with assert_raises(SystemExit):
exit(1)

with assert_raises(SystemExit):
exit("AB")

with assert_raises(SystemExit):
quit()

with assert_raises(SystemExit):
quit(None)

with assert_raises(SystemExit):
quit(1)

with assert_raises(SystemExit):
quit("AB")

import sys

with assert_raises(SystemExit):
sys.exit()

with assert_raises(SystemExit):
sys.exit(None)

with assert_raises(SystemExit):
sys.exit(1)

with assert_raises(SystemExit):
sys.exit("AB")
3 changes: 3 additions & 0 deletions vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ flamer = { version = "0.3", optional = true }
[target.'cfg(all(unix, not(any(target_os = "android", target_os = "redox"))))'.dependencies]
pwd = "1"

[target.'cfg(unix)'.dependencies]
exitcode = "1.1.2"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
crc32fast = "1.2.0"
adler32 = "1.0.3"
Expand Down
11 changes: 3 additions & 8 deletions vm/src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,14 +601,9 @@ impl Printer for std::io::StdoutLock<'_> {
}
}

pub fn builtin_exit(exit_code_arg: OptionalArg<PyObjectRef>, vm: &VirtualMachine) -> PyResult<()> {
if let OptionalArg::Present(exit_code_obj) = exit_code_arg {
match i32::try_from_object(&vm, exit_code_obj.clone()) {
Ok(code) => std::process::exit(code),
_ => println!("{}", vm.to_str(&exit_code_obj)?.as_str()),
}
}
std::process::exit(0);
pub fn builtin_exit(exit_code_arg: OptionalArg<PyObjectRef>, vm: &VirtualMachine) -> PyResult {
let code = exit_code_arg.unwrap_or_else(|| vm.new_int(0));
Err(vm.new_exception_obj(vm.ctx.exceptions.system_exit.clone(), vec![code])?)
}

pub fn builtin_print(objects: Args, options: PrintOptions, vm: &VirtualMachine) -> PyResult<()> {
Expand Down
27 changes: 27 additions & 0 deletions vm/src/stdlib/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use std::os::windows::fs::OpenOptionsExt;
use std::time::{Duration, SystemTime};
use std::{env, fs};

#[cfg(unix)]
use exitcode;
#[cfg(unix)]
use nix::errno::Errno;
#[cfg(all(unix, not(target_os = "redox")))]
Expand Down Expand Up @@ -972,6 +974,14 @@ fn os_cpu_count(vm: &VirtualMachine) -> PyObjectRef {
vm.new_int(cpu_count)
}

fn os_exit(code: PyIntRef, _vm: &VirtualMachine) -> PyResult<()> {
if let Some(code) = code.as_bigint().to_i32() {
std::process::exit(code)
} else {
panic!("unwrap error from code.as_bigint().to_i32() in os_exit()")
}
}

#[cfg(unix)]
fn os_getppid(vm: &VirtualMachine) -> PyObjectRef {
let ppid = unistd::getppid().as_raw();
Expand Down Expand Up @@ -1215,6 +1225,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
"fspath" => ctx.new_rustfunc(os_fspath),
"getpid" => ctx.new_rustfunc(os_getpid),
"cpu_count" => ctx.new_rustfunc(os_cpu_count),
"_exit" => ctx.new_rustfunc(os_exit),

"O_RDONLY" => ctx.new_int(libc::O_RDONLY),
"O_WRONLY" => ctx.new_int(libc::O_WRONLY),
Expand Down Expand Up @@ -1283,6 +1294,22 @@ fn extend_module_platform_specific(vm: &VirtualMachine, module: PyObjectRef) ->
"SEEK_SET" => ctx.new_int(Whence::SeekSet as i8),
"SEEK_CUR" => ctx.new_int(Whence::SeekCur as i8),
"SEEK_END" => ctx.new_int(Whence::SeekEnd as i8),
"EX_OK" => ctx.new_int(exitcode::OK as i8),
"EX_USAGE" => ctx.new_int(exitcode::USAGE as i8),
"EX_DATAERR" => ctx.new_int(exitcode::DATAERR as i8),
"EX_NOINPUT" => ctx.new_int(exitcode::NOINPUT as i8),
"EX_NOUSER" => ctx.new_int(exitcode::NOUSER as i8),
"EX_NOHOST" => ctx.new_int(exitcode::NOHOST as i8),
"EX_UNAVAILABLE" => ctx.new_int(exitcode::UNAVAILABLE as i8),
"EX_SOFTWARE" => ctx.new_int(exitcode::SOFTWARE as i8),
"EX_OSERR" => ctx.new_int(exitcode::OSERR as i8),
"EX_OSFILE" => ctx.new_int(exitcode::OSFILE as i8),
"EX_CANTCREAT" => ctx.new_int(exitcode::CANTCREAT as i8),
"EX_IOERR" => ctx.new_int(exitcode::IOERR as i8),
"EX_TEMPFAIL" => ctx.new_int(exitcode::TEMPFAIL as i8),
"EX_PROTOCOL" => ctx.new_int(exitcode::PROTOCOL as i8),
"EX_NOPERM" => ctx.new_int(exitcode::NOPERM as i8),
"EX_CONFIG" => ctx.new_int(exitcode::CONFIG as i8),
});

#[cfg(not(target_os = "redox"))]
Expand Down