@@ -6,28 +6,41 @@ pub(crate) use _signal::module_def;
66pub ( crate ) mod _signal {
77 #![ allow( unreachable_pub) ]
88
9- #[ cfg( any( unix, windows) ) ]
10- use crate :: convert:: { IntoPyException , TryFromBorrowedObject } ;
119 use crate :: { Py , PyObjectRef , PyResult , VirtualMachine , signal} ;
12- #[ cfg( unix) ]
13- use crate :: {
14- builtins:: { PyBaseExceptionRef , PyTypeRef } ,
15- function:: { ArgIntoFloat , OptionalArg } ,
10+ use core:: {
11+ ops:: Range ,
12+ sync:: atomic:: { self , Ordering } ,
1613 } ;
17- use core:: sync:: atomic:: { self , Ordering } ;
18- #[ cfg( any( unix, windows) ) ]
19- use rustpython_host_env:: signal as host_signal;
20- #[ cfg( unix) ]
21- use rustpython_host_env:: signal:: { double_to_timeval, itimerval_to_tuple} ;
22- #[ cfg( unix) ]
23- use std:: os:: fd:: AsFd ;
14+
15+ cfg_select ! {
16+ any( unix, windows) => {
17+ use crate :: convert:: { IntoPyException , TryFromBorrowedObject } ;
18+ use rustpython_host_env:: signal as host_signal;
19+ }
20+ _ => { }
21+ }
22+
23+ cfg_select ! {
24+ unix => {
25+ use crate :: {
26+ builtins:: { PyBaseExceptionRef , PyTypeRef } ,
27+ function:: { ArgIntoFloat , OptionalArg } ,
28+ } ;
29+ use rustpython_host_env:: signal:: { double_to_timeval, itimerval_to_tuple} ;
30+
31+ use std:: os:: fd:: AsFd ;
32+ } ,
33+ _ => { }
34+ }
2435
2536 #[ allow( non_camel_case_types) ]
2637 type sighandler_t = cfg_select ! {
2738 any( unix, windows) => libc:: sighandler_t,
2839 _ => usize ,
2940 } ;
3041
42+ const SIGNUM_RANGE : Range < i32 > = 1 ..signal:: NSIG as i32 ;
43+
3144 cfg_select ! {
3245 windows => {
3346 type WakeupFdRaw = libc:: SOCKET ;
@@ -150,10 +163,14 @@ pub(crate) mod _signal {
150163 )
151164 }
152165
166+ #[ cfg( unix) ]
153167 fn new_itimer_error ( msg : & str , vm : & VirtualMachine ) -> PyBaseExceptionRef {
154168 vm. new_exception_msg ( itimer_error ( vm) , msg. into ( ) )
155169 }
156170
171+ const _: ( ) = assert ! ( SIGNUM_RANGE . start. is_positive( ) ) ;
172+ const _: ( ) = assert ! ( SIGNUM_RANGE . end. is_positive( ) ) ;
173+
157174 #[ cfg( any( unix, windows) ) ]
158175 pub ( super ) fn init_signal_handlers (
159176 module : & Py < crate :: builtins:: PyModule > ,
@@ -163,8 +180,8 @@ pub(crate) mod _signal {
163180 let sig_dfl = vm. new_pyobj ( SIG_DFL as u8 ) ;
164181 let sig_ign = vm. new_pyobj ( SIG_IGN as u8 ) ;
165182
166- for signum in 1 .. NSIG {
167- let Some ( handler) = ( unsafe { host_signal:: probe_handler ( signum as i32 ) } ) else {
183+ for signum in SIGNUM_RANGE {
184+ let Some ( handler) = ( unsafe { host_signal:: probe_handler ( signum) } ) else {
168185 continue ;
169186 } ;
170187 let py_handler = if handler == SIG_DFL {
@@ -174,9 +191,10 @@ pub(crate) mod _signal {
174191 } else {
175192 None
176193 } ;
194+
177195 vm. signal_handlers
178196 . get_or_init ( signal:: new_signal_handlers)
179- . borrow_mut ( ) [ signum] = py_handler;
197+ . borrow_mut ( ) [ signum as usize ] = py_handler;
180198 }
181199
182200 let int_handler = module
@@ -402,9 +420,10 @@ pub(crate) mod _signal {
402420 #[ cfg( any( unix, windows) ) ]
403421 #[ pyfunction]
404422 fn strsignal ( signalnum : i32 , vm : & VirtualMachine ) -> PyResult < Option < String > > {
405- if signalnum < 1 || signalnum >= signal :: NSIG as i32 {
423+ if ! SIGNUM_RANGE . contains ( & signalnum) {
406424 return Err ( vm. new_value_error ( format ! ( "signal number {signalnum} out of range" ) ) ) ;
407425 }
426+
408427 Ok ( host_signal:: strsignal ( signalnum) )
409428 }
410429
@@ -430,7 +449,7 @@ pub(crate) mod _signal {
430449 use crate :: PyPayload ;
431450 use crate :: builtins:: PySet ;
432451 let set = PySet :: default ( ) . into_ref ( & vm. ctx ) ;
433- for signum in 1 ..signal :: NSIG {
452+ for signum in SIGNUM_RANGE {
434453 if host_signal:: sigset_contains ( mask, signum as i32 ) {
435454 set. add ( vm. ctx . new_int ( signum as i32 ) . into ( ) , vm) ?;
436455 }
@@ -459,11 +478,11 @@ pub(crate) mod _signal {
459478 let signum = sig
460479 . try_to_value :: < i32 > ( vm)
461480 . ok ( )
462- . filter ( |v| ( 1 ..signal :: NSIG as i32 ) . contains ( v) )
481+ . filter ( |v| SIGNUM_RANGE . contains ( v) )
463482 . ok_or_else ( || {
464483 vm. new_value_error ( format ! (
465484 "signal number out of range [1, {}]" ,
466- signal :: NSIG - 1
485+ SIGNUM_RANGE . end
467486 ) )
468487 } ) ?;
469488
0 commit comments