diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py index c6649962331464..e6ac0ffdd93fed 100644 --- a/Lib/test/test_threading.py +++ b/Lib/test/test_threading.py @@ -1628,6 +1628,15 @@ def test_init_immutable_default_args(self): timer1.join() timer2.join() + def test_daemon_param(self): + # PR 98231: add the daemon parameter to the Timer argument + t = threading.Timer(1, print) + self.assertFalse(t.daemon) + t = threading.Timer(1, print, daemon=False) + self.assertFalse(t.daemon) + t = threading.Timer(1, print, daemon=True) + self.assertTrue(t.daemon) + def _callback_spy(self, *args, **kwargs): self.callback_args.append((args[:], kwargs.copy())) self.callback_event.set() diff --git a/Lib/threading.py b/Lib/threading.py index d030e1243623f9..dd1d9f3cb0b55c 100644 --- a/Lib/threading.py +++ b/Lib/threading.py @@ -1387,8 +1387,8 @@ class Timer(Thread): """ - def __init__(self, interval, function, args=None, kwargs=None): - Thread.__init__(self) + def __init__(self, interval, function, args=None, kwargs=None, *, daemon=None): + Thread.__init__(self, daemon=daemon) self.interval = interval self.function = function self.args = args if args is not None else [] diff --git a/Misc/NEWS.d/next/Library/2022-10-13-13-32-05.gh-issue-98230.oTb1Az.rst b/Misc/NEWS.d/next/Library/2022-10-13-13-32-05.gh-issue-98230.oTb1Az.rst new file mode 100644 index 00000000000000..d32ae2cc0bcdec --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-10-13-13-32-05.gh-issue-98230.oTb1Az.rst @@ -0,0 +1 @@ +Add a daemon keyword-only argument to the :class:`threading.Timer` constructor, to pass it to the inherited :class:`threading.Thread` constructor.