diff options
author | Nathaniel J. Smith <njs@pobox.com> | 2017-12-18 04:10:18 (GMT) |
---|---|---|
committer | Yury Selivanov <yury@magic.io> | 2017-12-18 04:10:18 (GMT) |
commit | 902ab80b590e474bb2077b1fae8aac497b856d66 (patch) | |
tree | 427ee762a41bc71af3e35dcace840dac8ac92173 /Lib | |
parent | 1b7c11ff0ee3efafbf5b38c3c6f37de5d63efb81 (diff) | |
download | cpython-902ab80b590e474bb2077b1fae8aac497b856d66.zip cpython-902ab80b590e474bb2077b1fae8aac497b856d66.tar.gz cpython-902ab80b590e474bb2077b1fae8aac497b856d66.tar.bz2 |
bpo-30050: Allow disabling full buffer warnings in signal.set_wakeup_fd (#4792)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_signal.py | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/Lib/test/test_signal.py b/Lib/test/test_signal.py index dc048e5..f17123c 100644 --- a/Lib/test/test_signal.py +++ b/Lib/test/test_signal.py @@ -91,6 +91,15 @@ class WindowsSignalTests(unittest.TestCase): class WakeupFDTests(unittest.TestCase): + def test_invalid_call(self): + # First parameter is positional-only + with self.assertRaises(TypeError): + signal.set_wakeup_fd(signum=signal.SIGINT) + + # warn_on_full_buffer is a keyword-only parameter + with self.assertRaises(TypeError): + signal.set_wakeup_fd(signal.SIGINT, False) + def test_invalid_fd(self): fd = support.make_bad_fd() self.assertRaises((ValueError, OSError), @@ -423,6 +432,89 @@ class WakeupSocketSignalTests(unittest.TestCase): """.format(action=action) assert_python_ok('-c', code) + @unittest.skipIf(_testcapi is None, 'need _testcapi') + def test_warn_on_full_buffer(self): + # Use a subprocess to have only one thread. + if os.name == 'nt': + action = 'send' + else: + action = 'write' + code = """if 1: + import errno + import signal + import socket + import sys + import time + import _testcapi + from test.support import captured_stderr + + signum = signal.SIGINT + + # This handler will be called, but we intentionally won't read from + # the wakeup fd. + def handler(signum, frame): + pass + + signal.signal(signum, handler) + + read, write = socket.socketpair() + read.setblocking(False) + write.setblocking(False) + + # Fill the send buffer + try: + while True: + write.send(b"x") + except BlockingIOError: + pass + + # By default, we get a warning when a signal arrives + signal.set_wakeup_fd(write.fileno()) + + with captured_stderr() as err: + _testcapi.raise_signal(signum) + + err = err.getvalue() + if ('Exception ignored when trying to {action} to the signal wakeup fd' + not in err): + raise AssertionError(err) + + # And also if warn_on_full_buffer=True + signal.set_wakeup_fd(write.fileno(), warn_on_full_buffer=True) + + with captured_stderr() as err: + _testcapi.raise_signal(signum) + + err = err.getvalue() + if ('Exception ignored when trying to {action} to the signal wakeup fd' + not in err): + raise AssertionError(err) + + # But not if warn_on_full_buffer=False + signal.set_wakeup_fd(write.fileno(), warn_on_full_buffer=False) + + with captured_stderr() as err: + _testcapi.raise_signal(signum) + + err = err.getvalue() + if err != "": + raise AssertionError("got unexpected output %r" % (err,)) + + # And then check the default again, to make sure warn_on_full_buffer + # settings don't leak across calls. + signal.set_wakeup_fd(write.fileno()) + + with captured_stderr() as err: + _testcapi.raise_signal(signum) + + err = err.getvalue() + if ('Exception ignored when trying to {action} to the signal wakeup fd' + not in err): + raise AssertionError(err) + + """.format(action=action) + assert_python_ok('-c', code) + @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") class SiginterruptTest(unittest.TestCase): |