Skip to content
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
6 changes: 0 additions & 6 deletions Lib/test/test_runpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,11 +801,9 @@ def assertSigInt(self, cmd, *args, **kwargs):
self.assertTrue(proc.stderr.endswith("\nKeyboardInterrupt\n"), proc.stderr)
self.assertEqual(proc.returncode, self.EXPECTED_CODE)

@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON; ")
def test_pymain_run_file(self):
self.assertSigInt([self.ham])

@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON; ")
def test_pymain_run_file_runpy_run_module(self):
tmp = self.ham.parent
run_module = tmp / "run_module.py"
Expand All @@ -819,7 +817,6 @@ def test_pymain_run_file_runpy_run_module(self):
)
self.assertSigInt([run_module], cwd=tmp)

@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON; ")
def test_pymain_run_file_runpy_run_module_as_main(self):
tmp = self.ham.parent
run_module_as_main = tmp / "run_module_as_main.py"
Expand All @@ -833,22 +830,19 @@ def test_pymain_run_file_runpy_run_module_as_main(self):
)
self.assertSigInt([run_module_as_main], cwd=tmp)

@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON; ")
def test_pymain_run_command_run_module(self):
self.assertSigInt(
["-c", "import runpy; runpy.run_module('ham')"],
cwd=self.ham.parent,
)

@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON; ")
def test_pymain_run_command(self):
self.assertSigInt(["-c", "import ham"], cwd=self.ham.parent)

@unittest.expectedFailure # TODO: RUSTPYTHON
def test_pymain_run_stdin(self):
self.assertSigInt([], input="import ham", cwd=self.ham.parent)

@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON; ")
def test_pymain_run_module(self):
ham = self.ham
self.assertSigInt(["-m", ham.stem], cwd=ham.parent)
Expand Down
2 changes: 0 additions & 2 deletions Lib/test/test_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,6 @@ def test_issue9324(self):
with self.assertRaises(ValueError):
signal.signal(7, handler)

# TODO: RUSTPYTHON
@unittest.expectedFailure
@unittest.skipUnless(sys.executable, "sys.executable required.")
@support.requires_subprocess()
def test_keyboard_interrupt_exit_code(self):
Expand Down
20 changes: 19 additions & 1 deletion crates/common/src/os.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
// spell-checker:disable
// TODO: we can move more os-specific bindings/interfaces from stdlib::{os, posix, nt} to here

use std::{io, str::Utf8Error};
use std::{io, process::ExitCode, str::Utf8Error};

/// Convert exit code to std::process::ExitCode
///
/// On Windows, this supports the full u32 range including STATUS_CONTROL_C_EXIT (0xC000013A).
/// On other platforms, only the lower 8 bits are used.
pub fn exit_code(code: u32) -> ExitCode {
#[cfg(windows)]
{
// For large exit codes like STATUS_CONTROL_C_EXIT (0xC000013A),
// we need to call std::process::exit() directly since ExitCode::from(u8)
// would truncate the value, and ExitCode::from_raw() is still unstable.
// FIXME: side effect in exit_code is not ideal.
if code > u8::MAX as u32 {
std::process::exit(code as i32)
}
}
ExitCode::from(code as u8)
}

pub trait ErrorExt {
fn posix_errno(&self) -> i32;
Expand Down
4 changes: 2 additions & 2 deletions crates/vm/src/vm/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl Interpreter {
///
/// See [`Interpreter::finalize`] for the finalization steps.
/// See also [`Interpreter::enter`] for pure function call to obtain Python exception.
pub fn run<F>(self, f: F) -> u8
pub fn run<F>(self, f: F) -> u32
where
F: FnOnce(&VirtualMachine) -> PyResult<()>,
{
Expand All @@ -113,7 +113,7 @@ impl Interpreter {
/// 1. Mark vm as finalized.
///
/// Note that calling `finalize` is not necessary by purpose though.
pub fn finalize(self, exc: Option<PyBaseExceptionRef>) -> u8 {
pub fn finalize(self, exc: Option<PyBaseExceptionRef>) -> u32 {
self.enter(|vm| {
vm.flush_std();

Expand Down
13 changes: 9 additions & 4 deletions crates/vm/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -841,15 +841,15 @@ impl VirtualMachine {
}
}

pub fn handle_exit_exception(&self, exc: PyBaseExceptionRef) -> u8 {
pub fn handle_exit_exception(&self, exc: PyBaseExceptionRef) -> u32 {
if exc.fast_isinstance(self.ctx.exceptions.system_exit) {
let args = exc.args();
let msg = match args.as_slice() {
[] => return 0,
[arg] => match_class!(match arg {
ref i @ PyInt => {
use num_traits::cast::ToPrimitive;
return i.as_bigint().to_u8().unwrap_or(0);
return i.as_bigint().to_u32().unwrap_or(0);
}
arg => {
if self.is_none(arg) {
Expand Down Expand Up @@ -883,9 +883,14 @@ impl VirtualMachine {
kill(getpid(), SIGINT).expect("Expect to be killed.");
}

(libc::SIGINT as u8) + 128u8
(libc::SIGINT as u32) + 128
}
#[cfg(not(unix))]
#[cfg(windows)]
{
// STATUS_CONTROL_C_EXIT - same as CPython
0xC000013A
}
#[cfg(not(any(unix, windows)))]
{
1
}
Expand Down
2 changes: 1 addition & 1 deletion examples/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ fn main() -> ExitCode {
vm.add_native_modules(rustpython_stdlib::get_module_inits());
});
let result = py_main(&interp);
ExitCode::from(interp.run(|_vm| result))
vm::common::os::exit_code(interp.run(|_vm| result))
}
2 changes: 1 addition & 1 deletion examples/package_embed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ fn main() -> ExitCode {
let result = result.map(|result| {
println!("name: {result}");
});
ExitCode::from(interp.run(|_vm| result))
vm::common::os::exit_code(interp.run(|_vm| result))
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub fn run(init: impl FnOnce(&mut VirtualMachine) + 'static) -> ExitCode {
let interp = config.interpreter();
let exitcode = interp.run(move |vm| run_rustpython(vm, run_mode));

ExitCode::from(exitcode)
rustpython_vm::common::os::exit_code(exitcode)
}

fn setup_main_module(vm: &VirtualMachine) -> PyResult<Scope> {
Expand Down
Loading