mirror of
https://github.com/python/cpython.git
synced 2025-08-31 14:07:50 +00:00
gh-112559: Avoid unnecessary conversion attempts to enum_klass in signal.py (#113040)
This commit is contained in:
parent
0187a7e4ec
commit
050783cb37
4 changed files with 51 additions and 2 deletions
|
@ -22,9 +22,11 @@ if 'pthread_sigmask' in _globals:
|
||||||
|
|
||||||
|
|
||||||
def _int_to_enum(value, enum_klass):
|
def _int_to_enum(value, enum_klass):
|
||||||
"""Convert a numeric value to an IntEnum member.
|
"""Convert a possible numeric value to an IntEnum member.
|
||||||
If it's not a known member, return the numeric value itself.
|
If it's not a known member, return the value itself.
|
||||||
"""
|
"""
|
||||||
|
if not isinstance(value, int):
|
||||||
|
return value
|
||||||
try:
|
try:
|
||||||
return enum_klass(value)
|
return enum_klass(value)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
|
|
@ -495,6 +495,24 @@ class RunnerTests(BaseTest):
|
||||||
self.assertEqual(1, policy.set_event_loop.call_count)
|
self.assertEqual(1, policy.set_event_loop.call_count)
|
||||||
runner.close()
|
runner.close()
|
||||||
|
|
||||||
|
def test_no_repr_is_call_on_the_task_result(self):
|
||||||
|
# See https://github.com/python/cpython/issues/112559.
|
||||||
|
class MyResult:
|
||||||
|
def __init__(self):
|
||||||
|
self.repr_count = 0
|
||||||
|
def __repr__(self):
|
||||||
|
self.repr_count += 1
|
||||||
|
return super().__repr__()
|
||||||
|
|
||||||
|
async def coro():
|
||||||
|
return MyResult()
|
||||||
|
|
||||||
|
|
||||||
|
with asyncio.Runner() as runner:
|
||||||
|
result = runner.run(coro())
|
||||||
|
|
||||||
|
self.assertEqual(0, result.repr_count)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import enum
|
import enum
|
||||||
import errno
|
import errno
|
||||||
|
import functools
|
||||||
import inspect
|
import inspect
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
|
@ -76,6 +77,9 @@ class PosixTests(unittest.TestCase):
|
||||||
def trivial_signal_handler(self, *args):
|
def trivial_signal_handler(self, *args):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def create_handler_with_partial(self, argument):
|
||||||
|
return functools.partial(self.trivial_signal_handler, argument)
|
||||||
|
|
||||||
def test_out_of_range_signal_number_raises_error(self):
|
def test_out_of_range_signal_number_raises_error(self):
|
||||||
self.assertRaises(ValueError, signal.getsignal, 4242)
|
self.assertRaises(ValueError, signal.getsignal, 4242)
|
||||||
|
|
||||||
|
@ -96,6 +100,28 @@ class PosixTests(unittest.TestCase):
|
||||||
signal.signal(signal.SIGHUP, hup)
|
signal.signal(signal.SIGHUP, hup)
|
||||||
self.assertEqual(signal.getsignal(signal.SIGHUP), hup)
|
self.assertEqual(signal.getsignal(signal.SIGHUP), hup)
|
||||||
|
|
||||||
|
def test_no_repr_is_called_on_signal_handler(self):
|
||||||
|
# See https://github.com/python/cpython/issues/112559.
|
||||||
|
|
||||||
|
class MyArgument:
|
||||||
|
def __init__(self):
|
||||||
|
self.repr_count = 0
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
self.repr_count += 1
|
||||||
|
return super().__repr__()
|
||||||
|
|
||||||
|
argument = MyArgument()
|
||||||
|
self.assertEqual(0, argument.repr_count)
|
||||||
|
|
||||||
|
handler = self.create_handler_with_partial(argument)
|
||||||
|
hup = signal.signal(signal.SIGHUP, handler)
|
||||||
|
self.assertIsInstance(hup, signal.Handlers)
|
||||||
|
self.assertEqual(signal.getsignal(signal.SIGHUP), handler)
|
||||||
|
signal.signal(signal.SIGHUP, hup)
|
||||||
|
self.assertEqual(signal.getsignal(signal.SIGHUP), hup)
|
||||||
|
self.assertEqual(0, argument.repr_count)
|
||||||
|
|
||||||
def test_strsignal(self):
|
def test_strsignal(self):
|
||||||
self.assertIn("Interrupt", signal.strsignal(signal.SIGINT))
|
self.assertIn("Interrupt", signal.strsignal(signal.SIGINT))
|
||||||
self.assertIn("Terminated", signal.strsignal(signal.SIGTERM))
|
self.assertIn("Terminated", signal.strsignal(signal.SIGTERM))
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
:func:`signal.signal` and :func:`signal.getsignal` no longer call ``repr`` on
|
||||||
|
callable handlers. :func:`asyncio.run` and :meth:`asyncio.Runner.run` no longer
|
||||||
|
call ``repr`` on the task results. Patch by Yilei Yang.
|
Loading…
Add table
Add a link
Reference in a new issue