1+ import errno
12import os
23import random
34import signal
@@ -254,7 +255,7 @@ def handler(signum, frame):
254255 signal.set_wakeup_fd(r)
255256 try:
256257 with captured_stderr() as err:
257- _testcapi .raise_signal(signal.SIGALRM)
258+ signal .raise_signal(signal.SIGALRM)
258259 except ZeroDivisionError:
259260 # An ignored exception should have been printed out on stderr
260261 err = err.getvalue()
@@ -348,10 +349,9 @@ def handler(signum, frame):
348349
349350 def test_signum (self ):
350351 self .check_wakeup ("""def test():
351- import _testcapi
352352 signal.signal(signal.SIGUSR1, handler)
353- _testcapi .raise_signal(signal.SIGUSR1)
354- _testcapi .raise_signal(signal.SIGALRM)
353+ signal .raise_signal(signal.SIGUSR1)
354+ signal .raise_signal(signal.SIGALRM)
355355 """ , signal .SIGUSR1 , signal .SIGALRM )
356356
357357 @unittest .skipUnless (hasattr (signal , 'pthread_sigmask' ),
@@ -365,8 +365,8 @@ def test_pending(self):
365365 signal.signal(signum2, handler)
366366
367367 signal.pthread_sigmask(signal.SIG_BLOCK, (signum1, signum2))
368- _testcapi .raise_signal(signum1)
369- _testcapi .raise_signal(signum2)
368+ signal .raise_signal(signum1)
369+ signal .raise_signal(signum2)
370370 # Unblocking the 2 signals calls the C signal handler twice
371371 signal.pthread_sigmask(signal.SIG_UNBLOCK, (signum1, signum2))
372372 """ , signal .SIGUSR1 , signal .SIGUSR2 , ordered = False )
@@ -396,7 +396,7 @@ def handler(signum, frame):
396396 write.setblocking(False)
397397 signal.set_wakeup_fd(write.fileno())
398398
399- _testcapi .raise_signal(signum)
399+ signal .raise_signal(signum)
400400
401401 data = read.recv(1)
402402 if not data:
@@ -445,7 +445,7 @@ def handler(signum, frame):
445445 write.close()
446446
447447 with captured_stderr() as err:
448- _testcapi .raise_signal(signum)
448+ signal .raise_signal(signum)
449449
450450 err = err.getvalue()
451451 if ('Exception ignored when trying to {action} to the signal wakeup fd'
@@ -519,7 +519,7 @@ def handler(signum, frame):
519519 signal.set_wakeup_fd(write.fileno())
520520
521521 with captured_stderr() as err:
522- _testcapi .raise_signal(signum)
522+ signal .raise_signal(signum)
523523
524524 err = err.getvalue()
525525 if msg not in err:
@@ -530,7 +530,7 @@ def handler(signum, frame):
530530 signal.set_wakeup_fd(write.fileno(), warn_on_full_buffer=True)
531531
532532 with captured_stderr() as err:
533- _testcapi .raise_signal(signum)
533+ signal .raise_signal(signum)
534534
535535 err = err.getvalue()
536536 if msg not in err:
@@ -541,7 +541,7 @@ def handler(signum, frame):
541541 signal.set_wakeup_fd(write.fileno(), warn_on_full_buffer=False)
542542
543543 with captured_stderr() as err:
544- _testcapi .raise_signal(signum)
544+ signal .raise_signal(signum)
545545
546546 err = err.getvalue()
547547 if err != "":
@@ -553,7 +553,7 @@ def handler(signum, frame):
553553 signal.set_wakeup_fd(write.fileno())
554554
555555 with captured_stderr() as err:
556- _testcapi .raise_signal(signum)
556+ signal .raise_signal(signum)
557557
558558 err = err.getvalue()
559559 if msg not in err:
@@ -1214,6 +1214,38 @@ def handler(signum, frame):
12141214 # Python handler
12151215 self .assertEqual (len (sigs ), N , "Some signals were lost" )
12161216
1217+ class RaiseSignalTest (unittest .TestCase ):
1218+
1219+ def test_sigint (self ):
1220+ try :
1221+ signal .raise_signal (signal .SIGINT )
1222+ self .fail ("Expected KeyInterrupt" )
1223+ except KeyboardInterrupt :
1224+ pass
1225+
1226+ @unittest .skipIf (sys .platform != "win32" , "Windows specific test" )
1227+ def test_invalid_argument (self ):
1228+ try :
1229+ SIGHUP = 1 # not supported on win32
1230+ signal .raise_signal (SIGHUP )
1231+ self .fail ("OSError (Invalid argument) expected" )
1232+ except OSError as e :
1233+ if e .errno == errno .EINVAL :
1234+ pass
1235+ else :
1236+ raise
1237+
1238+ def test_handler (self ):
1239+ is_ok = False
1240+ def handler (a , b ):
1241+ nonlocal is_ok
1242+ is_ok = True
1243+ old_signal = signal .signal (signal .SIGINT , handler )
1244+ self .addCleanup (signal .signal , signal .SIGINT , old_signal )
1245+
1246+ signal .raise_signal (signal .SIGINT )
1247+ self .assertTrue (is_ok )
1248+
12171249
12181250def tearDownModule ():
12191251 support .reap_children ()
0 commit comments