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

Skip to content

Commit da3caed

Browse files
committed
Remove Queue.empty() and Queue.full() in favor of using qsize() or trapping the Empty and Full exceptions.
1 parent d32ed6f commit da3caed

5 files changed

Lines changed: 37 additions & 63 deletions

File tree

Doc/library/queue.rst

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,6 @@ See the source code for details. The public methods are:
5353
this number is not reliable.
5454

5555

56-
.. method:: Queue.empty()
57-
58-
Return ``True`` if the queue is empty, ``False`` otherwise. Because of
59-
multithreading semantics, this is not reliable.
60-
61-
62-
.. method:: Queue.full()
63-
64-
Return ``True`` if the queue is full, ``False`` otherwise. Because of
65-
multithreading semantics, this is not reliable.
66-
67-
6856
.. method:: Queue.put(item[, block[, timeout]])
6957

7058
Put *item* into the queue. If optional args *block* is true and *timeout* is

Lib/Queue.py

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def __init__(self, maxsize=0):
2323
import threading
2424
except ImportError:
2525
import dummy_threading as threading
26+
self.maxsize = maxsize
2627
self._init(maxsize)
2728
# mutex must be held whenever the queue is mutating. All methods
2829
# that acquire mutex must release it before returning. mutex
@@ -88,20 +89,6 @@ def qsize(self):
8889
self.mutex.release()
8990
return n
9091

91-
def empty(self):
92-
"""Return True if the queue is empty, False otherwise (not reliable!)."""
93-
self.mutex.acquire()
94-
n = self._empty()
95-
self.mutex.release()
96-
return n
97-
98-
def full(self):
99-
"""Return True if the queue is full, False otherwise (not reliable!)."""
100-
self.mutex.acquire()
101-
n = self._full()
102-
self.mutex.release()
103-
return n
104-
10592
def put(self, item, block=True, timeout=None):
10693
"""Put an item into the queue.
10794
@@ -116,20 +103,22 @@ def put(self, item, block=True, timeout=None):
116103
self.not_full.acquire()
117104
try:
118105
if not block:
119-
if self._full():
106+
if self.maxsize > 0 and self._qsize() == self.maxsize:
120107
raise Full
121108
elif timeout is None:
122-
while self._full():
123-
self.not_full.wait()
109+
if self.maxsize > 0:
110+
while self._qsize() == self.maxsize:
111+
self.not_full.wait()
124112
else:
125113
if timeout < 0:
126114
raise ValueError("'timeout' must be a positive number")
127115
endtime = _time() + timeout
128-
while self._full():
129-
remaining = endtime - _time()
130-
if remaining <= 0.0:
131-
raise Full
132-
self.not_full.wait(remaining)
116+
if self.maxsize > 0:
117+
while self._qsize() == self.maxsize:
118+
remaining = endtime - _time()
119+
if remaining <= 0.0:
120+
raise Full
121+
self.not_full.wait(remaining)
133122
self._put(item)
134123
self.unfinished_tasks += 1
135124
self.not_empty.notify()
@@ -158,16 +147,16 @@ def get(self, block=True, timeout=None):
158147
self.not_empty.acquire()
159148
try:
160149
if not block:
161-
if self._empty():
150+
if not self._qsize():
162151
raise Empty
163152
elif timeout is None:
164-
while self._empty():
153+
while not self._qsize():
165154
self.not_empty.wait()
166155
else:
167156
if timeout < 0:
168157
raise ValueError("'timeout' must be a positive number")
169158
endtime = _time() + timeout
170-
while self._empty():
159+
while not self._qsize():
171160
remaining = endtime - _time()
172161
if remaining <= 0.0:
173162
raise Empty
@@ -192,20 +181,11 @@ def get_nowait(self):
192181

193182
# Initialize the queue representation
194183
def _init(self, maxsize):
195-
self.maxsize = maxsize
196184
self.queue = deque()
197185

198186
def _qsize(self):
199187
return len(self.queue)
200188

201-
# Check whether the queue is empty
202-
def _empty(self):
203-
return not self.queue
204-
205-
# Check whether the queue is full
206-
def _full(self):
207-
return self.maxsize > 0 and len(self.queue) == self.maxsize
208-
209189
# Put a new item in the queue
210190
def _put(self, item):
211191
self.queue.append(item)

Lib/test/test_queue.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
QUEUE_SIZE = 5
1111

12+
def qfull(q):
13+
return q.maxsize > 0 and q.qsize() == q.maxsize
14+
1215
# A thread to run a function that unclogs a blocked Queue.
1316
class _TriggerThread(threading.Thread):
1417
def __init__(self, fn, args):
@@ -96,7 +99,7 @@ def _get(self):
9699
return Queue.Queue._get(self)
97100

98101
def FailingQueueTest(q):
99-
if not q.empty():
102+
if q.qsize():
100103
raise RuntimeError("Call this function with an empty queue")
101104
for i in range(QUEUE_SIZE-1):
102105
q.put(i)
@@ -114,7 +117,7 @@ def FailingQueueTest(q):
114117
except FailingQueueException:
115118
pass
116119
q.put("last")
117-
verify(q.full(), "Queue should be full")
120+
verify(qfull(q), "Queue should be full")
118121
# Test a failing blocking put
119122
q.fail_next_put = True
120123
try:
@@ -136,34 +139,34 @@ def FailingQueueTest(q):
136139
# Check the Queue isn't damaged.
137140
# put failed, but get succeeded - re-add
138141
q.put("last")
139-
verify(q.full(), "Queue should be full")
142+
verify(qfull(q), "Queue should be full")
140143
q.get()
141-
verify(not q.full(), "Queue should not be full")
144+
verify(not qfull(q), "Queue should not be full")
142145
q.put("last")
143-
verify(q.full(), "Queue should be full")
146+
verify(qfull(q), "Queue should be full")
144147
# Test a blocking put
145148
_doBlockingTest( q.put, ("full",), q.get, ())
146149
# Empty it
147150
for i in range(QUEUE_SIZE):
148151
q.get()
149-
verify(q.empty(), "Queue should be empty")
152+
verify(not q.qsize(), "Queue should be empty")
150153
q.put("first")
151154
q.fail_next_get = True
152155
try:
153156
q.get()
154157
raise TestFailed("The queue didn't fail when it should have")
155158
except FailingQueueException:
156159
pass
157-
verify(not q.empty(), "Queue should not be empty")
160+
verify(q.qsize(), "Queue should not be empty")
158161
q.fail_next_get = True
159162
try:
160163
q.get(timeout=0.1)
161164
raise TestFailed("The queue didn't fail when it should have")
162165
except FailingQueueException:
163166
pass
164-
verify(not q.empty(), "Queue should not be empty")
167+
verify(q.qsize(), "Queue should not be empty")
165168
q.get()
166-
verify(q.empty(), "Queue should be empty")
169+
verify(not q.qsize(), "Queue should be empty")
167170
q.fail_next_get = True
168171
try:
169172
_doExceptionalBlockingTest(q.get, (), q.put, ('empty',),
@@ -172,12 +175,12 @@ def FailingQueueTest(q):
172175
except FailingQueueException:
173176
pass
174177
# put succeeded, but get failed.
175-
verify(not q.empty(), "Queue should not be empty")
178+
verify(q.qsize(), "Queue should not be empty")
176179
q.get()
177-
verify(q.empty(), "Queue should be empty")
180+
verify(not q.qsize(), "Queue should be empty")
178181

179182
def SimpleQueueTest(q):
180-
if not q.empty():
183+
if q.qsize():
181184
raise RuntimeError("Call this function with an empty queue")
182185
# I guess we better check things actually queue correctly a little :)
183186
q.put(111)
@@ -186,10 +189,10 @@ def SimpleQueueTest(q):
186189
"Didn't seem to queue the correct data!")
187190
for i in range(QUEUE_SIZE-1):
188191
q.put(i)
189-
verify(not q.empty(), "Queue should not be empty")
190-
verify(not q.full(), "Queue should not be full")
192+
verify(q.qsize(), "Queue should not be empty")
193+
verify(not qfull(q), "Queue should not be full")
191194
q.put("last")
192-
verify(q.full(), "Queue should be full")
195+
verify(qfull(q), "Queue should be full")
193196
try:
194197
q.put("full", block=0)
195198
raise TestFailed("Didn't appear to block with a full queue")
@@ -206,7 +209,7 @@ def SimpleQueueTest(q):
206209
# Empty it
207210
for i in range(QUEUE_SIZE):
208211
q.get()
209-
verify(q.empty(), "Queue should be empty")
212+
verify(not q.qsize(), "Queue should be empty")
210213
try:
211214
q.get(block=0)
212215
raise TestFailed("Didn't appear to block with an empty queue")

Lib/test/test_socket.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def _tearDown(self):
118118
self.__tearDown()
119119
self.done.wait()
120120

121-
if not self.queue.empty():
121+
if self.queue.qsize():
122122
msg = self.queue.get()
123123
self.fail(msg)
124124

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,9 @@ Core and Builtins
352352
Library
353353
-------
354354

355+
- Removed Queue.empty() and Queue.full(). Instead use Queue.qsize() or
356+
trap the Empty and Full exceptions.
357+
355358
- Removed defunct parts of the random module (the Wichmann-Hill generator
356359
and the jumpahead() method).
357360

0 commit comments

Comments
 (0)