Skip to content

Commit 73a86c1

Browse files
committed
Fix test_runpy
1 parent 2ff1fba commit 73a86c1

File tree

8 files changed

+33
-18
lines changed

8 files changed

+33
-18
lines changed

Lib/test/test_runpy.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -801,11 +801,9 @@ def assertSigInt(self, cmd, *args, **kwargs):
801801
self.assertTrue(proc.stderr.endswith("\nKeyboardInterrupt\n"), proc.stderr)
802802
self.assertEqual(proc.returncode, self.EXPECTED_CODE)
803803

804-
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON; ")
805804
def test_pymain_run_file(self):
806805
self.assertSigInt([self.ham])
807806

808-
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON; ")
809807
def test_pymain_run_file_runpy_run_module(self):
810808
tmp = self.ham.parent
811809
run_module = tmp / "run_module.py"
@@ -819,7 +817,6 @@ def test_pymain_run_file_runpy_run_module(self):
819817
)
820818
self.assertSigInt([run_module], cwd=tmp)
821819

822-
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON; ")
823820
def test_pymain_run_file_runpy_run_module_as_main(self):
824821
tmp = self.ham.parent
825822
run_module_as_main = tmp / "run_module_as_main.py"
@@ -833,22 +830,19 @@ def test_pymain_run_file_runpy_run_module_as_main(self):
833830
)
834831
self.assertSigInt([run_module_as_main], cwd=tmp)
835832

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

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

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

851-
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON; ")
852846
def test_pymain_run_module(self):
853847
ham = self.ham
854848
self.assertSigInt(["-m", ham.stem], cwd=ham.parent)

Lib/test/test_signal.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,6 @@ def test_issue9324(self):
224224
with self.assertRaises(ValueError):
225225
signal.signal(7, handler)
226226

227-
# TODO: RUSTPYTHON
228-
@unittest.expectedFailure
229227
@unittest.skipUnless(sys.executable, "sys.executable required.")
230228
@support.requires_subprocess()
231229
def test_keyboard_interrupt_exit_code(self):

crates/common/src/os.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
11
// spell-checker:disable
22
// TODO: we can move more os-specific bindings/interfaces from stdlib::{os, posix, nt} to here
33

4-
use std::{io, str::Utf8Error};
4+
use std::{io, process::ExitCode, str::Utf8Error};
5+
6+
/// Convert exit code to std::process::ExitCode
7+
///
8+
/// On Windows, this supports the full u32 range including STATUS_CONTROL_C_EXIT (0xC000013A).
9+
/// On other platforms, only the lower 8 bits are used.
10+
pub fn exit_code(code: u32) -> ExitCode {
11+
#[cfg(windows)]
12+
{
13+
// For large exit codes like STATUS_CONTROL_C_EXIT (0xC000013A),
14+
// we need to call std::process::exit() directly since ExitCode::from(u8)
15+
// would truncate the value, and ExitCode::from_raw() is still unstable.
16+
// FIXME: side effect in exit_code is not ideal.
17+
if code > u8::MAX as u32 {
18+
std::process::exit(code as i32)
19+
}
20+
}
21+
ExitCode::from(code as u8)
22+
}
523

624
pub trait ErrorExt {
725
fn posix_errno(&self) -> i32;

crates/vm/src/vm/interpreter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl Interpreter {
9696
///
9797
/// See [`Interpreter::finalize`] for the finalization steps.
9898
/// See also [`Interpreter::enter`] for pure function call to obtain Python exception.
99-
pub fn run<F>(self, f: F) -> u8
99+
pub fn run<F>(self, f: F) -> u32
100100
where
101101
F: FnOnce(&VirtualMachine) -> PyResult<()>,
102102
{
@@ -113,7 +113,7 @@ impl Interpreter {
113113
/// 1. Mark vm as finalized.
114114
///
115115
/// Note that calling `finalize` is not necessary by purpose though.
116-
pub fn finalize(self, exc: Option<PyBaseExceptionRef>) -> u8 {
116+
pub fn finalize(self, exc: Option<PyBaseExceptionRef>) -> u32 {
117117
self.enter(|vm| {
118118
vm.flush_std();
119119

crates/vm/src/vm/mod.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -841,15 +841,15 @@ impl VirtualMachine {
841841
}
842842
}
843843

844-
pub fn handle_exit_exception(&self, exc: PyBaseExceptionRef) -> u8 {
844+
pub fn handle_exit_exception(&self, exc: PyBaseExceptionRef) -> u32 {
845845
if exc.fast_isinstance(self.ctx.exceptions.system_exit) {
846846
let args = exc.args();
847847
let msg = match args.as_slice() {
848848
[] => return 0,
849849
[arg] => match_class!(match arg {
850850
ref i @ PyInt => {
851851
use num_traits::cast::ToPrimitive;
852-
return i.as_bigint().to_u8().unwrap_or(0);
852+
return i.as_bigint().to_u32().unwrap_or(0);
853853
}
854854
arg => {
855855
if self.is_none(arg) {
@@ -883,9 +883,14 @@ impl VirtualMachine {
883883
kill(getpid(), SIGINT).expect("Expect to be killed.");
884884
}
885885

886-
(libc::SIGINT as u8) + 128u8
886+
(libc::SIGINT as u32) + 128
887887
}
888-
#[cfg(not(unix))]
888+
#[cfg(windows)]
889+
{
890+
// STATUS_CONTROL_C_EXIT - same as CPython
891+
0xC000013A
892+
}
893+
#[cfg(not(any(unix, windows)))]
889894
{
890895
1
891896
}

examples/generator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ fn main() -> ExitCode {
4646
vm.add_native_modules(rustpython_stdlib::get_module_inits());
4747
});
4848
let result = py_main(&interp);
49-
ExitCode::from(interp.run(|_vm| result))
49+
vm::common::os::exit_code(interp.run(|_vm| result))
5050
}

examples/package_embed.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ fn main() -> ExitCode {
2626
let result = result.map(|result| {
2727
println!("name: {result}");
2828
});
29-
ExitCode::from(interp.run(|_vm| result))
29+
vm::common::os::exit_code(interp.run(|_vm| result))
3030
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ pub fn run(init: impl FnOnce(&mut VirtualMachine) + 'static) -> ExitCode {
111111
let interp = config.interpreter();
112112
let exitcode = interp.run(move |vm| run_rustpython(vm, run_mode));
113113

114-
ExitCode::from(exitcode)
114+
rustpython_vm::common::os::exit_code(exitcode)
115115
}
116116

117117
fn setup_main_module(vm: &VirtualMachine) -> PyResult<Scope> {

0 commit comments

Comments
 (0)