mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Merge pull request #4454 from moreal/sigaction
Correct exitcode when running through `subprocess.run`
This commit is contained in:
4 files changed
+30
-15
No files matched your search
Vendored
+13
-12
@@ -790,13 +790,9 @@ class TestExit(unittest.TestCase):
|
||||
self.assertTrue(proc.stderr.endswith("\nKeyboardInterrupt\n"))
|
||||
self.assertEqual(proc.returncode, self.EXPECTED_CODE)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_pymain_run_file(self):
|
||||
self.assertSigInt([sys.executable, self.ham])
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_pymain_run_file_runpy_run_module(self):
|
||||
tmp = self.ham.parent
|
||||
run_module = tmp / "run_module.py"
|
||||
@@ -810,8 +806,6 @@ class TestExit(unittest.TestCase):
|
||||
)
|
||||
self.assertSigInt([sys.executable, run_module], cwd=tmp)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
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"
|
||||
@@ -825,16 +819,12 @@ class TestExit(unittest.TestCase):
|
||||
)
|
||||
self.assertSigInt([sys.executable, run_module_as_main], cwd=tmp)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_pymain_run_command_run_module(self):
|
||||
self.assertSigInt(
|
||||
[sys.executable, "-c", "import runpy; runpy.run_module('ham')"],
|
||||
cwd=self.ham.parent,
|
||||
)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_pymain_run_command(self):
|
||||
self.assertSigInt([sys.executable, "-c", "import ham"], cwd=self.ham.parent)
|
||||
|
||||
@@ -843,12 +833,23 @@ class TestExit(unittest.TestCase):
|
||||
def test_pymain_run_stdin(self):
|
||||
self.assertSigInt([sys.executable], input="import ham", cwd=self.ham.parent)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_pymain_run_module(self):
|
||||
ham = self.ham
|
||||
self.assertSigInt([sys.executable, "-m", ham.stem], cwd=ham.parent)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
if sys.platform == "win32":
|
||||
windows_only_failed_tests = [
|
||||
test_pymain_run_file,
|
||||
test_pymain_run_file_runpy_run_module,
|
||||
test_pymain_run_file_runpy_run_module_as_main,
|
||||
test_pymain_run_command_run_module,
|
||||
test_pymain_run_command,
|
||||
test_pymain_run_module
|
||||
]
|
||||
for t in windows_only_failed_tests:
|
||||
unittest.expectedFailure(t)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Vendored
-2
@@ -96,8 +96,6 @@ class PosixTests(unittest.TestCase):
|
||||
self.assertNotIn(signal.NSIG, s)
|
||||
self.assertLess(len(s), signal.NSIG)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
@unittest.skipUnless(sys.executable, "sys.executable required.")
|
||||
def test_keyboard_interrupt_exit_code(self):
|
||||
"""KeyboardInterrupt triggers exit via SIGINT."""
|
||||
|
||||
@@ -84,7 +84,7 @@ impl Interpreter {
|
||||
}
|
||||
}
|
||||
|
||||
fn flush_std(vm: &VirtualMachine) {
|
||||
pub(crate) fn flush_std(vm: &VirtualMachine) {
|
||||
if let Ok(stdout) = sys::get_stdout(vm) {
|
||||
let _ = vm.call_method(&stdout, identifier!(vm, flush).as_str(), ());
|
||||
}
|
||||
|
||||
@@ -36,6 +36,11 @@ use crate::{
|
||||
AsObject, PyObject, PyObjectRef, PyPayload, PyRef, PyResult,
|
||||
};
|
||||
use crossbeam_utils::atomic::AtomicCell;
|
||||
#[cfg(unix)]
|
||||
use nix::{
|
||||
sys::signal::{kill, sigaction, SaFlags, SigAction, SigSet, Signal::SIGINT},
|
||||
unistd::getpid,
|
||||
};
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
@@ -763,6 +768,17 @@ impl VirtualMachine {
|
||||
self.print_exception(exc);
|
||||
#[cfg(unix)]
|
||||
{
|
||||
let action = SigAction::new(
|
||||
nix::sys::signal::SigHandler::SigDfl,
|
||||
SaFlags::SA_ONSTACK,
|
||||
SigSet::empty(),
|
||||
);
|
||||
let result = unsafe { sigaction(SIGINT, &action) };
|
||||
if result.is_ok() {
|
||||
interpreter::flush_std(self);
|
||||
kill(getpid(), SIGINT).expect("Expect to be killed.");
|
||||
}
|
||||
|
||||
(libc::SIGINT as u8) + 128u8
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
|
||||
Reference in new issue
Block a user