Skip to content

Commit 189316a

Browse files
committed
Issue 10110: Let Queue.put recognize a full queue when the maxsize parameter has been reduced.
1 parent d285bdb commit 189316a

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

Lib/queue.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,14 @@ def empty(self):
110110
def full(self):
111111
"""Return True if the queue is full, False otherwise (not reliable!).
112112
113-
This method is likely to be removed at some point. Use qsize() == n
113+
This method is likely to be removed at some point. Use qsize() >= n
114114
as a direct substitute, but be aware that either approach risks a race
115115
condition where a queue can shrink before the result of full() or
116116
qsize() can be used.
117117
118118
"""
119119
self.mutex.acquire()
120-
n = 0 < self.maxsize == self._qsize()
120+
n = 0 < self.maxsize <= self._qsize()
121121
self.mutex.release()
122122
return n
123123

@@ -136,16 +136,16 @@ def put(self, item, block=True, timeout=None):
136136
try:
137137
if self.maxsize > 0:
138138
if not block:
139-
if self._qsize() == self.maxsize:
139+
if self._qsize() >= self.maxsize:
140140
raise Full
141141
elif timeout is None:
142-
while self._qsize() == self.maxsize:
142+
while self._qsize() >= self.maxsize:
143143
self.not_full.wait()
144144
elif timeout < 0:
145145
raise ValueError("'timeout' must be a positive number")
146146
else:
147147
endtime = _time() + timeout
148-
while self._qsize() == self.maxsize:
148+
while self._qsize() >= self.maxsize:
149149
remaining = endtime - _time()
150150
if remaining <= 0.0:
151151
raise Full

Lib/test/test_queue.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,18 @@ def test_nowait(self):
216216
with self.assertRaises(queue.Empty):
217217
q.get_nowait()
218218

219+
def test_shrinking_queue(self):
220+
# issue 10110
221+
q = self.type2test(3)
222+
q.put(1)
223+
q.put(2)
224+
q.put(3)
225+
with self.assertRaises(queue.Full):
226+
q.put_nowait(4)
227+
self.assertEqual(q.qsize(), 3)
228+
q.maxsize = 2 # shrink the queue
229+
with self.assertRaises(queue.Full):
230+
q.put_nowait(4)
219231

220232
class QueueTest(BaseQueueTest):
221233
type2test = queue.Queue

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ Library
6262
- Issue #10266: uu.decode didn't close in_file explicitly when it was given
6363
as a filename. Patch by Brian Brazil.
6464

65+
- Issue #10110: Queue objects didn't recognize full queues when the
66+
maxsize parameter had been reduced.
67+
6568
- Issue #10160: Speed up operator.attrgetter. Patch by Christos Georgiou.
6669

6770
- logging: Added style option to basicConfig() to allow %, {} or $-formatting.

0 commit comments

Comments
 (0)