diff options
author | R David Murray <rdmurray@bitdance.com> | 2013-03-30 21:19:38 (GMT) |
---|---|---|
committer | R David Murray <rdmurray@bitdance.com> | 2013-03-30 21:19:38 (GMT) |
commit | 19aeb439c6ca0da540ad95eec599d3654babe20b (patch) | |
tree | 1c074a911bbd6f8b6e652ca100add42fc0d7afe6 /Lib | |
parent | 8028a5cf15753c283b7308a32ecc4651db33c6c9 (diff) | |
download | cpython-19aeb439c6ca0da540ad95eec599d3654babe20b.zip cpython-19aeb439c6ca0da540ad95eec599d3654babe20b.tar.gz cpython-19aeb439c6ca0da540ad95eec599d3654babe20b.tar.bz2 |
Issue #17435: Don't use mutable default values in Timer.
Patch by Denver Coneybeare with some test modifications by me.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_threading.py | 39 | ||||
-rw-r--r-- | Lib/threading.py | 8 |
2 files changed, 31 insertions, 16 deletions
diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py index 11e63d3..e6b209d 100644 --- a/Lib/test/test_threading.py +++ b/Lib/test/test_threading.py @@ -786,6 +786,32 @@ class ThreadingExceptionTests(BaseTestCase): self.assertEqual(p.returncode, 0, "Unexpected error: " + stderr.decode()) self.assertEqual(data, expected_output) +class TimerTests(BaseTestCase): + + def setUp(self): + BaseTestCase.setUp(self) + self.callback_args = [] + self.callback_event = threading.Event() + + def test_init_immutable_default_args(self): + # Issue 17435: constructor defaults were mutable objects, they could be + # mutated via the object attributes and affect other Timer objects. + timer1 = threading.Timer(0.01, self._callback_spy) + timer1.start() + self.callback_event.wait() + timer1.args.append("blah") + timer1.kwargs["foo"] = "bar" + self.callback_event.clear() + timer2 = threading.Timer(0.01, self._callback_spy) + timer2.start() + self.callback_event.wait() + self.assertEqual(len(self.callback_args), 2) + self.assertEqual(self.callback_args, [((), {}), ((), {})]) + + def _callback_spy(self, *args, **kwargs): + self.callback_args.append((args[:], kwargs.copy())) + self.callback_event.set() + class LockTests(lock_tests.LockTests): locktype = staticmethod(threading.Lock) @@ -815,16 +841,5 @@ class BoundedSemaphoreTests(lock_tests.BoundedSemaphoreTests): class BarrierTests(lock_tests.BarrierTests): barriertype = staticmethod(threading.Barrier) - -def test_main(): - test.support.run_unittest(LockTests, PyRLockTests, CRLockTests, EventTests, - ConditionAsRLockTests, ConditionTests, - SemaphoreTests, BoundedSemaphoreTests, - ThreadTests, - ThreadJoinOnShutdown, - ThreadingExceptionTests, - BarrierTests, - ) - if __name__ == "__main__": - test_main() + unittest.main() diff --git a/Lib/threading.py b/Lib/threading.py index 6c34d49..46df676 100644 --- a/Lib/threading.py +++ b/Lib/threading.py @@ -802,17 +802,17 @@ class Thread: class Timer(Thread): """Call a function after a specified number of seconds: - t = Timer(30.0, f, args=[], kwargs={}) + t = Timer(30.0, f, args=None, kwargs=None) t.start() t.cancel() # stop the timer's action if it's still waiting """ - def __init__(self, interval, function, args=[], kwargs={}): + def __init__(self, interval, function, args=None, kwargs=None): Thread.__init__(self) self.interval = interval self.function = function - self.args = args - self.kwargs = kwargs + self.args = args if args is not None else [] + self.kwargs = kwargs if kwargs is not None else {} self.finished = Event() def cancel(self): |