@@ -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 ;
@@ -154,6 +167,9 @@ pub(crate) mod _signal {
154167 vm. new_exception_msg ( itimer_error ( vm) , msg. into ( ) )
155168 }
156169
170+ const _: ( ) = assert ! ( SIGNUM_RANGE . start. is_positive( ) ) ;
171+ const _: ( ) = assert ! ( SIGNUM_RANGE . end. is_positive( ) ) ;
172+
157173 #[ cfg( any( unix, windows) ) ]
158174 pub ( super ) fn init_signal_handlers (
159175 module : & Py < crate :: builtins:: PyModule > ,
@@ -163,8 +179,8 @@ pub(crate) mod _signal {
163179 let sig_dfl = vm. new_pyobj ( SIG_DFL as u8 ) ;
164180 let sig_ign = vm. new_pyobj ( SIG_IGN as u8 ) ;
165181
166- for signum in 1 .. NSIG {
167- let Some ( handler) = ( unsafe { host_signal:: probe_handler ( signum as i32 ) } ) else {
182+ for signum in SIGNUM_RANGE {
183+ let Some ( handler) = ( unsafe { host_signal:: probe_handler ( signum) } ) else {
168184 continue ;
169185 } ;
170186 let py_handler = if handler == SIG_DFL {
@@ -174,9 +190,10 @@ pub(crate) mod _signal {
174190 } else {
175191 None
176192 } ;
193+
177194 vm. signal_handlers
178195 . get_or_init ( signal:: new_signal_handlers)
179- . borrow_mut ( ) [ signum] = py_handler;
196+ . borrow_mut ( ) [ signum as usize ] = py_handler;
180197 }
181198
182199 let int_handler = module
@@ -402,9 +419,10 @@ pub(crate) mod _signal {
402419 #[ cfg( any( unix, windows) ) ]
403420 #[ pyfunction]
404421 fn strsignal ( signalnum : i32 , vm : & VirtualMachine ) -> PyResult < Option < String > > {
405- if signalnum < 1 || signalnum >= signal :: NSIG as i32 {
422+ if ! SIGNUM_RANGE . contains ( & signalnum) {
406423 return Err ( vm. new_value_error ( format ! ( "signal number {signalnum} out of range" ) ) ) ;
407424 }
425+
408426 Ok ( host_signal:: strsignal ( signalnum) )
409427 }
410428
@@ -430,7 +448,7 @@ pub(crate) mod _signal {
430448 use crate :: PyPayload ;
431449 use crate :: builtins:: PySet ;
432450 let set = PySet :: default ( ) . into_ref ( & vm. ctx ) ;
433- for signum in 1 ..signal :: NSIG {
451+ for signum in SIGNUM_RANGE {
434452 if host_signal:: sigset_contains ( mask, signum as i32 ) {
435453 set. add ( vm. ctx . new_int ( signum as i32 ) . into ( ) , vm) ?;
436454 }
@@ -459,11 +477,11 @@ pub(crate) mod _signal {
459477 let signum = sig
460478 . try_to_value :: < i32 > ( vm)
461479 . ok ( )
462- . filter ( |v| ( 1 ..signal :: NSIG as i32 ) . contains ( v) )
480+ . filter ( |v| SIGNUM_RANGE . contains ( v) )
463481 . ok_or_else ( || {
464482 vm. new_value_error ( format ! (
465483 "signal number out of range [1, {}]" ,
466- signal :: NSIG - 1
484+ SIGNUM_RANGE . end
467485 ) )
468486 } ) ?;
469487
0 commit comments