Skip to content

Commit ad8a182

Browse files
Merge pull request #1483 from ChJR/hotfix/SystemExit
Change something related to exit
2 parents f78aa07 + 2bc8543 commit ad8a182

File tree

6 files changed

+76
-8
lines changed

6 files changed

+76
-8
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/os.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def _get_exports_list(module):
4545

4646
import _os
4747
from _os import *
48+
from _os import _exit
4849
__all__.extend(_get_exports_list(_os))
4950
del _os
5051

tests/snippets/exit.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,38 @@
22

33
with assert_raises(SystemExit):
44
exit()
5+
6+
with assert_raises(SystemExit):
7+
exit(None)
8+
9+
with assert_raises(SystemExit):
10+
exit(1)
11+
12+
with assert_raises(SystemExit):
13+
exit("AB")
14+
15+
with assert_raises(SystemExit):
16+
quit()
17+
18+
with assert_raises(SystemExit):
19+
quit(None)
20+
21+
with assert_raises(SystemExit):
22+
quit(1)
23+
24+
with assert_raises(SystemExit):
25+
quit("AB")
26+
27+
import sys
28+
29+
with assert_raises(SystemExit):
30+
sys.exit()
31+
32+
with assert_raises(SystemExit):
33+
sys.exit(None)
34+
35+
with assert_raises(SystemExit):
36+
sys.exit(1)
37+
38+
with assert_raises(SystemExit):
39+
sys.exit("AB")

vm/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ flamer = { version = "0.3", optional = true }
7171
[target.'cfg(all(unix, not(any(target_os = "android", target_os = "redox"))))'.dependencies]
7272
pwd = "1"
7373

74+
[target.'cfg(unix)'.dependencies]
75+
exitcode = "1.1.2"
76+
7477
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
7578
crc32fast = "1.2.0"
7679
adler32 = "1.0.3"

vm/src/builtins.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -601,14 +601,9 @@ impl Printer for std::io::StdoutLock<'_> {
601601
}
602602
}
603603

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

614609
pub fn builtin_print(objects: Args, options: PrintOptions, vm: &VirtualMachine) -> PyResult<()> {

vm/src/stdlib/os.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use std::os::windows::fs::OpenOptionsExt;
1111
use std::time::{Duration, SystemTime};
1212
use std::{env, fs};
1313

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

977+
fn os_exit(code: PyIntRef, _vm: &VirtualMachine) -> PyResult<()> {
978+
if let Some(code) = code.as_bigint().to_i32() {
979+
std::process::exit(code)
980+
} else {
981+
panic!("unwrap error from code.as_bigint().to_i32() in os_exit()")
982+
}
983+
}
984+
975985
#[cfg(unix)]
976986
fn os_getppid(vm: &VirtualMachine) -> PyObjectRef {
977987
let ppid = unistd::getppid().as_raw();
@@ -1215,6 +1225,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
12151225
"fspath" => ctx.new_rustfunc(os_fspath),
12161226
"getpid" => ctx.new_rustfunc(os_getpid),
12171227
"cpu_count" => ctx.new_rustfunc(os_cpu_count),
1228+
"_exit" => ctx.new_rustfunc(os_exit),
12181229

12191230
"O_RDONLY" => ctx.new_int(libc::O_RDONLY),
12201231
"O_WRONLY" => ctx.new_int(libc::O_WRONLY),
@@ -1283,6 +1294,22 @@ fn extend_module_platform_specific(vm: &VirtualMachine, module: PyObjectRef) ->
12831294
"SEEK_SET" => ctx.new_int(Whence::SeekSet as i8),
12841295
"SEEK_CUR" => ctx.new_int(Whence::SeekCur as i8),
12851296
"SEEK_END" => ctx.new_int(Whence::SeekEnd as i8),
1297+
"EX_OK" => ctx.new_int(exitcode::OK as i8),
1298+
"EX_USAGE" => ctx.new_int(exitcode::USAGE as i8),
1299+
"EX_DATAERR" => ctx.new_int(exitcode::DATAERR as i8),
1300+
"EX_NOINPUT" => ctx.new_int(exitcode::NOINPUT as i8),
1301+
"EX_NOUSER" => ctx.new_int(exitcode::NOUSER as i8),
1302+
"EX_NOHOST" => ctx.new_int(exitcode::NOHOST as i8),
1303+
"EX_UNAVAILABLE" => ctx.new_int(exitcode::UNAVAILABLE as i8),
1304+
"EX_SOFTWARE" => ctx.new_int(exitcode::SOFTWARE as i8),
1305+
"EX_OSERR" => ctx.new_int(exitcode::OSERR as i8),
1306+
"EX_OSFILE" => ctx.new_int(exitcode::OSFILE as i8),
1307+
"EX_CANTCREAT" => ctx.new_int(exitcode::CANTCREAT as i8),
1308+
"EX_IOERR" => ctx.new_int(exitcode::IOERR as i8),
1309+
"EX_TEMPFAIL" => ctx.new_int(exitcode::TEMPFAIL as i8),
1310+
"EX_PROTOCOL" => ctx.new_int(exitcode::PROTOCOL as i8),
1311+
"EX_NOPERM" => ctx.new_int(exitcode::NOPERM as i8),
1312+
"EX_CONFIG" => ctx.new_int(exitcode::CONFIG as i8),
12861313
});
12871314

12881315
#[cfg(not(target_os = "redox"))]

0 commit comments

Comments
 (0)