Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(195626)

Side by Side Diff: Lib/threading.py

Issue 17435: threading.Timer.__init__() should use immutable argument defaults for args and kwargs
Patch Set: Created 6 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_threading.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 """Thread module emulating a subset of Java's threading model.""" 1 """Thread module emulating a subset of Java's threading model."""
2 2
3 import sys as _sys 3 import sys as _sys
4 import _thread 4 import _thread
5 5
6 from time import sleep as _sleep 6 from time import sleep as _sleep
7 try: 7 try:
8 from time import monotonic as _time 8 from time import monotonic as _time
9 except ImportError: 9 except ImportError:
10 from time import time as _time 10 from time import time as _time
(...skipping 790 matching lines...) Expand 10 before | Expand all | Expand 10 after
801 return self.name 801 return self.name
802 802
803 def setName(self, name): 803 def setName(self, name):
804 self.name = name 804 self.name = name
805 805
806 # The timer class was contributed by Itamar Shtull-Trauring 806 # The timer class was contributed by Itamar Shtull-Trauring
807 807
808 class Timer(Thread): 808 class Timer(Thread):
809 """Call a function after a specified number of seconds: 809 """Call a function after a specified number of seconds:
810 810
811 t = Timer(30.0, f, args=[], kwargs={}) 811 t = Timer(30.0, f, args=None, kwargs=None)
812 t.start() 812 t.start()
813 t.cancel() # stop the timer's action if it's still waiting 813 t.cancel() # stop the timer's action if it's still waiting
814 """ 814 """
815 815
816 def __init__(self, interval, function, args=[], kwargs={}): 816 def __init__(self, interval, function, args=None, kwargs=None):
817 Thread.__init__(self) 817 Thread.__init__(self)
818 self.interval = interval 818 self.interval = interval
819 self.function = function 819 self.function = function
820 self.args = args 820 self.args = args if args is not None else []
821 self.kwargs = kwargs 821 self.kwargs = kwargs if kwargs is not None else {}
822 self.finished = Event() 822 self.finished = Event()
823 823
824 def cancel(self): 824 def cancel(self):
825 """Stop the timer if it hasn't finished yet""" 825 """Stop the timer if it hasn't finished yet"""
826 self.finished.set() 826 self.finished.set()
827 827
828 def run(self): 828 def run(self):
829 self.finished.wait(self.interval) 829 self.finished.wait(self.interval)
830 if not self.finished.is_set(): 830 if not self.finished.is_set():
831 self.function(*self.args, **self.kwargs) 831 self.function(*self.args, **self.kwargs)
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
954 thread._ident = ident 954 thread._ident = ident
955 new_active[ident] = thread 955 new_active[ident] = thread
956 else: 956 else:
957 # All the others are already stopped. 957 # All the others are already stopped.
958 thread._stop() 958 thread._stop()
959 959
960 _limbo.clear() 960 _limbo.clear()
961 _active.clear() 961 _active.clear()
962 _active.update(new_active) 962 _active.update(new_active)
963 assert len(_active) == 1 963 assert len(_active) == 1
OLDNEW
« no previous file with comments | « Lib/test/test_threading.py ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+