Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3c80ce4

Browse files
Issue #16641: Fix default values of sched.scheduler.enter arguments were modifiable.
2 parents d1ced9e + c04957b commit 3c80ce4

3 files changed

Lines changed: 23 additions & 11 deletions

File tree

Doc/library/sched.rst

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,22 @@ Example::
3636

3737
>>> import sched, time
3838
>>> s = sched.scheduler(time.time, time.sleep)
39-
>>> def print_time(): print("From print_time", time.time())
39+
>>> def print_time(a='default'):
40+
... print("From print_time", time.time(), a)
4041
...
4142
>>> def print_some_times():
4243
... print(time.time())
43-
... s.enter(5, 1, print_time, ())
44-
... s.enter(10, 1, print_time, ())
44+
... s.enter(10, 1, print_time)
45+
... s.enter(5, 2, print_time, argument=('positional',))
46+
... s.enter(5, 1, print_time, kwargs={'a': 'keyword'})
4547
... s.run()
4648
... print(time.time())
4749
...
4850
>>> print_some_times()
4951
930343690.257
50-
From print_time 930343695.274
51-
From print_time 930343700.273
52+
From print_time 930343695.274 positional
53+
From print_time 930343695.275 keyword
54+
From print_time 930343700.273 default
5255
930343700.276
5356

5457
.. _scheduler-objects:
@@ -59,16 +62,18 @@ Scheduler Objects
5962
:class:`scheduler` instances have the following methods and attributes:
6063

6164

62-
.. method:: scheduler.enterabs(time, priority, action, argument=[], kwargs={})
65+
.. method:: scheduler.enterabs(time, priority, action, argument=(), kwargs={})
6366

6467
Schedule a new event. The *time* argument should be a numeric type compatible
6568
with the return value of the *timefunc* function passed to the constructor.
6669
Events scheduled for the same *time* will be executed in the order of their
6770
*priority*.
6871

6972
Executing the event means executing ``action(*argument, **kwargs)``.
70-
*argument* must be a sequence holding the parameters for *action*.
71-
*kwargs* must be a dictionary holding the keyword parameters for *action*.
73+
Optional *argument* argument must be a sequence holding the parameters
74+
for *action* if any used.
75+
Optional *kwargs* argument must be a dictionary holding the keyword
76+
parameters for *action* if any used.
7277

7378
Return value is an event which may be used for later cancellation of the event
7479
(see :meth:`cancel`).
@@ -80,7 +85,7 @@ Scheduler Objects
8085
*kwargs* parameter was added.
8186

8287

83-
.. method:: scheduler.enter(delay, priority, action, argument=[], kwargs={})
88+
.. method:: scheduler.enter(delay, priority, action, argument=(), kwargs={})
8489

8590
Schedule an event for *delay* more time units. Other than the relative time, the
8691
other arguments, the effect and the return value are the same as those for

Lib/sched.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ def __le__(s, o): return (s.time, s.priority) <= (o.time, o.priority)
5050
def __gt__(s, o): return (s.time, s.priority) > (o.time, o.priority)
5151
def __ge__(s, o): return (s.time, s.priority) >= (o.time, o.priority)
5252

53+
_sentinel = object()
54+
5355
class scheduler:
5456

5557
def __init__(self, timefunc=_time, delayfunc=time.sleep):
@@ -60,19 +62,21 @@ def __init__(self, timefunc=_time, delayfunc=time.sleep):
6062
self.timefunc = timefunc
6163
self.delayfunc = delayfunc
6264

63-
def enterabs(self, time, priority, action, argument=[], kwargs={}):
65+
def enterabs(self, time, priority, action, argument=(), kwargs=_sentinel):
6466
"""Enter a new event in the queue at an absolute time.
6567
6668
Returns an ID for the event which can be used to remove it,
6769
if necessary.
6870
6971
"""
72+
if kwargs is _sentinel:
73+
kwargs = {}
7074
with self._lock:
7175
event = Event(time, priority, action, argument, kwargs)
7276
heapq.heappush(self._queue, event)
7377
return event # The ID
7478

75-
def enter(self, delay, priority, action, argument=[], kwargs={}):
79+
def enter(self, delay, priority, action, argument=(), kwargs=_sentinel):
7680
"""A variant that specifies the time as a relative time.
7781
7882
This is actually the more commonly used interface.

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ Core and Builtins
200200
Library
201201
-------
202202

203+
- Issue #16641: Fix default values of sched.scheduler.enter arguments were
204+
modifiable.
205+
203206
- Issue #16504: IDLE now catches SyntaxErrors raised by tokenizer. Patch by
204207
Roger Serwy.
205208

0 commit comments

Comments
 (0)