mirror of https://github.com/python/cpython
gh-91880: add try/except around `signal.signal` (#91881)
Fixes gh-91880.
This commit is contained in:
parent
54d068adfb
commit
1cd8c29dac
|
@ -100,7 +100,13 @@ class Runner:
|
|||
and signal.getsignal(signal.SIGINT) is signal.default_int_handler
|
||||
):
|
||||
sigint_handler = functools.partial(self._on_sigint, main_task=task)
|
||||
signal.signal(signal.SIGINT, sigint_handler)
|
||||
try:
|
||||
signal.signal(signal.SIGINT, sigint_handler)
|
||||
except ValueError:
|
||||
# `signal.signal` may throw if `threading.main_thread` does
|
||||
# not support signals (e.g. embedded interpreter with signals
|
||||
# not registered - see gh-91880)
|
||||
signal_handler = None
|
||||
else:
|
||||
sigint_handler = None
|
||||
|
||||
|
|
|
@ -3,10 +3,12 @@ import asyncio
|
|||
import contextvars
|
||||
import gc
|
||||
import re
|
||||
import signal
|
||||
import threading
|
||||
import unittest
|
||||
|
||||
from unittest import mock
|
||||
from unittest.mock import patch
|
||||
from test.test_asyncio import utils as test_utils
|
||||
|
||||
|
||||
|
@ -374,6 +376,23 @@ class RunnerTests(BaseTest):
|
|||
with asyncio.Runner() as runner:
|
||||
with self.assertRaises(asyncio.CancelledError):
|
||||
runner.run(coro())
|
||||
|
||||
def test_signal_install_not_supported_ok(self):
|
||||
# signal.signal() can throw if the "main thread" doensn't have signals enabled
|
||||
assert threading.current_thread() is threading.main_thread()
|
||||
|
||||
async def coro():
|
||||
pass
|
||||
|
||||
with asyncio.Runner() as runner:
|
||||
with patch.object(
|
||||
signal,
|
||||
"signal",
|
||||
side_effect=ValueError(
|
||||
"signal only works in main thread of the main interpreter"
|
||||
)
|
||||
):
|
||||
runner.run(coro())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
Loading…
Reference in New Issue