MCPcopy Index your code
hub / github.com/RustPython/RustPython / handle_exit_exception

Method handle_exit_exception

crates/vm/src/vm/mod.rs:2114–2181  ·  view source on GitHub ↗
(&self, exc: PyBaseExceptionRef)

Source from the content-addressed store, hash-verified

2112 }
2113
2114 pub fn handle_exit_exception(&self, exc: PyBaseExceptionRef) -> u32 {
2115 if exc.fast_isinstance(self.ctx.exceptions.system_exit) {
2116 let args = exc.args();
2117 let msg = match args.as_slice() {
2118 [] => return 0,
2119 [arg] => match_class!(match arg {
2120 ref i @ PyInt => {
2121 use num_traits::cast::ToPrimitive;
2122 // Try u32 first, then i32 (for negative values), else -1 for overflow
2123 let code = i
2124 .as_bigint()
2125 .to_u32()
2126 .or_else(|| i.as_bigint().to_i32().map(|v| v as u32))
2127 .unwrap_or(-1i32 as u32);
2128 return code;
2129 }
2130 arg => {
2131 if self.is_none(arg) {
2132 return 0;
2133 } else {
2134 arg.str(self).ok()
2135 }
2136 }
2137 }),
2138 _ => args.as_object().repr(self).ok(),
2139 };
2140 if let Some(msg) = msg {
2141 // Write using Python's write() to use stderr's error handler (backslashreplace)
2142 if let Ok(stderr) = stdlib::sys::get_stderr(self) {
2143 let _ = self.call_method(&stderr, "write", (msg,));
2144 let _ = self.call_method(&stderr, "write", ("\n",));
2145 }
2146 }
2147 1
2148 } else if exc.fast_isinstance(self.ctx.exceptions.keyboard_interrupt) {
2149 #[allow(clippy::if_same_then_else)]
2150 {
2151 self.print_exception(exc);
2152 #[cfg(unix)]
2153 {
2154 let action = SigAction::new(
2155 nix::sys::signal::SigHandler::SigDfl,
2156 SaFlags::SA_ONSTACK,
2157 SigSet::empty(),
2158 );
2159 let result = unsafe { sigaction(SIGINT, &action) };
2160 if result.is_ok() {
2161 self.flush_std();
2162 kill(getpid(), SIGINT).expect("Expect to be killed.");
2163 }
2164
2165 (libc::SIGINT as u32) + 128
2166 }
2167 #[cfg(windows)]
2168 {
2169 // STATUS_CONTROL_C_EXIT - same as CPython
2170 0xC000013A
2171 }

Callers 1

finalizeMethod · 0.80

Calls 13

get_stderrFunction · 0.85
newFunction · 0.85
getpidFunction · 0.85
fast_isinstanceMethod · 0.80
okMethod · 0.80
print_exceptionMethod · 0.80
flush_stdMethod · 0.80
killFunction · 0.50
argsMethod · 0.45
as_sliceMethod · 0.45
reprMethod · 0.45
as_objectMethod · 0.45

Tested by

no test coverage detected