Update signal and its test from CPython 3.10.5

This commit is contained in:
CPython developers authored and Jeong Yunwon committed 2022-07-19 00:11:57 +09:00
1 parent f2ea8e22d7
commit b7dd1b7928
2 files changed
+55 -11

No files matched your search

+10 -3
View File
@@ -1,6 +1,5 @@
import _signal
from _signal import *
from functools import wraps as _wraps
from enum import IntEnum as _IntEnum
_globals = globals()
@@ -42,6 +41,16 @@ def _enum_to_int(value):
return value
# Similar to functools.wraps(), but only assign __doc__.
# __module__ should be preserved,
# __name__ and __qualname__ are already fine,
# __annotations__ is not set.
def _wraps(wrapped):
def decorator(wrapper):
wrapper.__doc__ = wrapped.__doc__
return wrapper
return decorator
@_wraps(_signal.signal)
def signal(signalnum, handler):
handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler))
@@ -59,7 +68,6 @@ if 'pthread_sigmask' in _globals:
def pthread_sigmask(how, mask):
sigs_set = _signal.pthread_sigmask(how, mask)
return set(_int_to_enum(x, Signals) for x in sigs_set)
pthread_sigmask.__doc__ = _signal.pthread_sigmask.__doc__
if 'sigpending' in _globals:
@@ -73,7 +81,6 @@ if 'sigwait' in _globals:
def sigwait(sigset):
retsig = _signal.sigwait(sigset)
return _int_to_enum(retsig, Signals)
sigwait.__doc__ = _signal.sigwait
if 'valid_signals' in _globals:
+45 -8
View File
@@ -1,4 +1,5 @@
import errno
import inspect
import os
import random
import signal
@@ -33,6 +34,14 @@ class GenericTests(unittest.TestCase):
self.assertIsInstance(sig, signal.Signals)
self.assertEqual(sys.platform, "win32")
def test_functions_module_attr(self):
# Issue #27718: If __all__ is not defined all non-builtin functions
# should have correct __module__ to be displayed by pydoc.
for name in dir(signal):
value = getattr(signal, name)
if inspect.isroutine(value) and not inspect.isbuiltin(value):
self.assertEqual(value.__module__, 'signal')
@unittest.skipIf(sys.platform == "win32", "Not valid on Windows")
class PosixTests(unittest.TestCase):
@@ -552,16 +561,20 @@ class WakeupSocketSignalTests(unittest.TestCase):
else:
write.setblocking(False)
# Start with large chunk size to reduce the
# number of send needed to fill the buffer.
written = 0
for chunk_size in (2 ** 16, 2 ** 8, 1):
if sys.platform == "vxworks":
CHUNK_SIZES = (1,)
else:
# Start with large chunk size to reduce the
# number of send needed to fill the buffer.
CHUNK_SIZES = (2 ** 16, 2 ** 8, 1)
for chunk_size in CHUNK_SIZES:
chunk = b"x" * chunk_size
try:
while True:
write.send(chunk)
written += chunk_size
except (BlockingIOError, socket.timeout):
except (BlockingIOError, TimeoutError):
pass
print(f"%s bytes written into the socketpair" % written, flush=True)
@@ -628,6 +641,7 @@ class WakeupSocketSignalTests(unittest.TestCase):
@unittest.skipIf(sys.platform == "win32", "Not valid on Windows")
@unittest.skipUnless(hasattr(signal, 'siginterrupt'), "needs signal.siginterrupt()")
class SiginterruptTest(unittest.TestCase):
def readpipe_interrupted(self, interrupt):
@@ -678,7 +692,7 @@ class SiginterruptTest(unittest.TestCase):
# wait until the child process is loaded and has started
first_line = process.stdout.readline()
stdout, stderr = process.communicate(timeout=5.0)
stdout, stderr = process.communicate(timeout=support.SHORT_TIMEOUT)
except subprocess.TimeoutExpired:
process.kill()
return False
@@ -717,6 +731,8 @@ class SiginterruptTest(unittest.TestCase):
@unittest.skipIf(sys.platform == "win32", "Not valid on Windows")
@unittest.skipUnless(hasattr(signal, 'getitimer') and hasattr(signal, 'setitimer'),
"needs signal.getitimer() and signal.setitimer()")
class ItimerTest(unittest.TestCase):
def setUp(self):
self.hndl_called = False
@@ -1240,7 +1256,7 @@ class StressTest(unittest.TestCase):
self.setsig(signal.SIGALRM, second_handler) # for ITIMER_REAL
expected_sigs = 0
deadline = time.monotonic() + 15.0
deadline = time.monotonic() + support.SHORT_TIMEOUT
while expected_sigs < N:
os.kill(os.getpid(), signal.SIGPROF)
@@ -1274,7 +1290,7 @@ class StressTest(unittest.TestCase):
self.setsig(signal.SIGALRM, handler) # for ITIMER_REAL
expected_sigs = 0
deadline = time.monotonic() + 15.0
deadline = time.monotonic() + support.SHORT_TIMEOUT
while expected_sigs < N:
# Hopefully the SIGALRM will be received somewhere during
@@ -1336,7 +1352,7 @@ class StressTest(unittest.TestCase):
# race condition, check it.
self.assertIsInstance(cm.unraisable.exc_value, OSError)
self.assertIn(
f"Signal {signum} ignored due to race condition",
f"Signal {signum:d} ignored due to race condition",
str(cm.unraisable.exc_value))
ignored = True
@@ -1387,6 +1403,27 @@ class RaiseSignalTest(unittest.TestCase):
self.assertTrue(is_ok)
class PidfdSignalTest(unittest.TestCase):
@unittest.skipUnless(
hasattr(signal, "pidfd_send_signal"),
"pidfd support not built in",
)
def test_pidfd_send_signal(self):
with self.assertRaises(OSError) as cm:
signal.pidfd_send_signal(0, signal.SIGINT)
if cm.exception.errno == errno.ENOSYS:
self.skipTest("kernel does not support pidfds")
elif cm.exception.errno == errno.EPERM:
self.skipTest("Not enough privileges to use pidfs")
self.assertEqual(cm.exception.errno, errno.EBADF)
my_pidfd = os.open(f'/proc/{os.getpid()}', os.O_DIRECTORY)
self.addCleanup(os.close, my_pidfd)
with self.assertRaisesRegex(TypeError, "^siginfo must be None$"):
signal.pidfd_send_signal(my_pidfd, signal.SIGINT, object(), 0)
with self.assertRaises(KeyboardInterrupt):
signal.pidfd_send_signal(my_pidfd, signal.SIGINT)
def tearDownModule():
support.reap_children()