-
Notifications
You must be signed in to change notification settings - Fork 1.4k
signal support #1189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
signal support #1189
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
1b17926
Initial signal support
palaviv 5b670f8
Simplify signal code
palaviv f540d31
Move static triggers array to signal.rs
palaviv ca23c43
Return previous handler from signal
palaviv 57cdae1
Add signal.getsignal
palaviv a7d96b7
Use rust signal module directly
palaviv e8001d7
Add signal.alarm
palaviv c1e0799
Add test for signal
palaviv 7061f1c
Add signal.{SIG_IGN, SIG_DFL}
palaviv a1af6b4
Iterate over triggers in check_sginals
palaviv 56b555b
Add signal numbers
palaviv 785b5d8
Improve signal test
palaviv 61bf076
User arr_macro to create triggers array
palaviv 9470d75
Compile nix parts only on unix
palaviv 48da527
Test signal only on unix
palaviv f3b4b28
SIGINT not defined on windows
palaviv 7cd5e89
Get SIG_IGN
palaviv 52d204c
Fix clippy warnings
palaviv 60b5d9d
Add empty check_signals on WASM
palaviv 25b9f35
Fix more clippy warnings
palaviv 3e07d61
Add vm to WASM check_sginals
palaviv 075f2c9
Use libc directly to set signal
palaviv 23cba40
Get signal numbers from libc
palaviv f24c6da
Add some windows test to signal
palaviv f600868
Define SIG_* on windows
palaviv c5486a6
Remove stub check_signals in WASM
palaviv 5e5d46d
Remove os_set_signal
palaviv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add signal.{SIG_IGN, SIG_DFL}
- Loading branch information
commit 7061f1c9c4bcd4336d359b97f7f85d01134c91b6
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,5 @@ | ||
| use crate::obj::objfunction::PyFunctionRef; | ||
| use crate::obj::objint::PyIntRef; | ||
| use crate::pyobject::PyObjectRef; | ||
| use crate::pyobject::PyResult; | ||
| use crate::pyobject::{IdProtocol, PyObjectRef, PyResult}; | ||
| use crate::vm::VirtualMachine; | ||
|
|
||
| use std::sync::atomic::{AtomicBool, Ordering}; | ||
|
|
@@ -41,23 +39,35 @@ extern "C" fn run_signal(signum: i32) { | |
|
|
||
| fn signal( | ||
| signalnum: PyIntRef, | ||
| handler: PyFunctionRef, | ||
| handler: PyObjectRef, | ||
| vm: &VirtualMachine, | ||
| ) -> PyResult<Option<PyObjectRef>> { | ||
| if !vm.isinstance(&handler, &vm.ctx.function_type())? | ||
| && !vm.isinstance(&handler, &vm.ctx.bound_method_type())? | ||
| && !vm.isinstance(&handler, &vm.ctx.builtin_function_or_method_type())? | ||
| { | ||
| return Err(vm.new_type_error("Hanlder must be callable".to_string())); | ||
| } | ||
| let signal = vm.import("signal", &vm.ctx.new_tuple(vec![]), 0)?; | ||
| let sig_dfl = vm.get_attribute(signal.clone(), "SIG_DFL")?; | ||
| let sig_ign = vm.get_attribute(signal, "SIG_DFL")?; | ||
| let signalnum = signalnum.as_bigint().to_i32().unwrap(); | ||
| let signal_enum = signal::Signal::from_c_int(signalnum).unwrap(); | ||
| let sig_handler = nix::sys::signal::SigHandler::Handler(run_signal); | ||
| let sig_handler = if handler.is(&sig_dfl) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @windelbouwman @coolreader18 any suggestion on how should I do this? This don't actually works...
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ignore this. I had a typo... |
||
| signal::SigHandler::SigDfl | ||
| } else if handler.is(&sig_ign) { | ||
| signal::SigHandler::SigIgn | ||
| } else { | ||
| signal::SigHandler::Handler(run_signal) | ||
| }; | ||
| let sig_action = signal::SigAction::new( | ||
| sig_handler, | ||
| signal::SaFlags::empty(), | ||
| signal::SigSet::empty(), | ||
| ); | ||
| check_signals(vm); | ||
| unsafe { signal::sigaction(signal_enum, &sig_action) }.unwrap(); | ||
| let old_handler = vm | ||
| .signal_handlers | ||
| .borrow_mut() | ||
| .insert(signalnum, handler.into_object()); | ||
| let old_handler = vm.signal_handlers.borrow_mut().insert(signalnum, handler); | ||
| Ok(old_handler) | ||
| } | ||
|
|
||
|
|
@@ -93,12 +103,21 @@ pub fn check_signals(vm: &VirtualMachine) { | |
| } | ||
| } | ||
|
|
||
| fn stub_func(_vm: &VirtualMachine) -> PyResult { | ||
| panic!("Do not use directly"); | ||
| } | ||
|
|
||
| pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { | ||
| let ctx = &vm.ctx; | ||
|
|
||
| let sig_dfl = ctx.new_rustfunc(stub_func); | ||
| let sig_ign = ctx.new_rustfunc(stub_func); | ||
|
|
||
| py_module!(vm, "signal", { | ||
| "signal" => ctx.new_rustfunc(signal), | ||
| "getsignal" => ctx.new_rustfunc(getsignal), | ||
| "alarm" => ctx.new_rustfunc(alarm), | ||
| "SIG_DFL" => sig_dfl, | ||
| "SIG_IGN" => sig_ign, | ||
| }) | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For clarity, you might consider naming this variable
signal_module.