]> granicus.if.org Git - python/commitdiff
Issue 10110: Let Queue.put recognize a full queue when the maxsize parameter has...
authorRaymond Hettinger <python@rcn.com>
Sun, 31 Oct 2010 17:57:52 +0000 (17:57 +0000)
committerRaymond Hettinger <python@rcn.com>
Sun, 31 Oct 2010 17:57:52 +0000 (17:57 +0000)
Lib/queue.py
Lib/test/test_queue.py
Misc/NEWS

index f051f1c89f2a879ef3bb6656f44eba433d270826..bee7ed4086a99b239d058ddb8b5931ae6fb4ff0d 100644 (file)
@@ -110,14 +110,14 @@ class Queue:
     def full(self):
         """Return True if the queue is full, False otherwise (not reliable!).
 
-        This method is likely to be removed at some point.  Use qsize() == n
+        This method is likely to be removed at some point.  Use qsize() >= n
         as a direct substitute, but be aware that either approach risks a race
         condition where a queue can shrink before the result of full() or
         qsize() can be used.
 
         """
         self.mutex.acquire()
-        n = 0 < self.maxsize == self._qsize()
+        n = 0 < self.maxsize <= self._qsize()
         self.mutex.release()
         return n
 
@@ -136,16 +136,16 @@ class Queue:
         try:
             if self.maxsize > 0:
                 if not block:
-                    if self._qsize() == self.maxsize:
+                    if self._qsize() >= self.maxsize:
                         raise Full
                 elif timeout is None:
-                    while self._qsize() == self.maxsize:
+                    while self._qsize() >= self.maxsize:
                         self.not_full.wait()
                 elif timeout < 0:
                     raise ValueError("'timeout' must be a positive number")
                 else:
                     endtime = _time() + timeout
-                    while self._qsize() == self.maxsize:
+                    while self._qsize() >= self.maxsize:
                         remaining = endtime - _time()
                         if remaining <= 0.0:
                             raise Full
index 0c8093a2a9e43182b2939b3ec8f2e62cabefc76f..5cef90ca39dc7a5f3813e7f917d473f57fdaf479 100644 (file)
@@ -216,6 +216,18 @@ class BaseQueueTest(unittest.TestCase, BlockingTestMixin):
         with self.assertRaises(queue.Empty):
             q.get_nowait()
 
+    def test_shrinking_queue(self):
+        # issue 10110
+        q = self.type2test(3)
+        q.put(1)
+        q.put(2)
+        q.put(3)
+        with self.assertRaises(queue.Full):
+            q.put_nowait(4)
+        self.assertEqual(q.qsize(), 3)
+        q.maxsize = 2                       # shrink the queue
+        with self.assertRaises(queue.Full):
+            q.put_nowait(4)
 
 class QueueTest(BaseQueueTest):
     type2test = queue.Queue
index 5c0acfb04b1766050f0efd13d87622e517395724..abb3ebc3f2ade1fe954a5b4e456a7a032c5b7c5b 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -62,6 +62,9 @@ Library
 - Issue #10266: uu.decode didn't close in_file explicitly when it was given
   as a filename.  Patch by Brian Brazil.
 
+- Issue #10110: Queue objects didn't recognize full queues when the
+  maxsize parameter had been reduced.
+
 - Issue #10160: Speed up operator.attrgetter.  Patch by Christos Georgiou.
 
 - logging: Added style option to basicConfig() to allow %, {} or $-formatting.