]> granicus.if.org Git - python/commitdiff
bpo-22872: multiprocessing.Queue's put() and get() now raise ValueError if the queue...
authorZackery Spytz <zspytz@gmail.com>
Sat, 13 Oct 2018 09:26:09 +0000 (03:26 -0600)
committerSerhiy Storchaka <storchaka@gmail.com>
Sat, 13 Oct 2018 09:26:09 +0000 (12:26 +0300)
Previously, put() and get() would raise AssertionError and OSError,
respectively.

Doc/library/multiprocessing.rst
Lib/multiprocessing/queues.py
Lib/test/_test_multiprocessing.py
Misc/NEWS.d/next/Library/2018-08-30-14-44-11.bpo-22872.NhIaZ9.rst [new file with mode: 0644]

index 8402370d98f60a4835993b713b90309593eacf12..578b5483286af09f1d0a81fbad2672a17b9a1695 100644 (file)
@@ -787,6 +787,10 @@ For an example of the usage of queues for interprocess communication see
       available, else raise the :exc:`queue.Full` exception (*timeout* is
       ignored in that case).
 
+      .. versionchanged:: 3.8
+         If the queue is closed, :exc:`ValueError` is raised instead of
+         :exc:`AssertionError`.
+
    .. method:: put_nowait(obj)
 
       Equivalent to ``put(obj, False)``.
@@ -801,6 +805,10 @@ For an example of the usage of queues for interprocess communication see
       ``False``), return an item if one is immediately available, else raise the
       :exc:`queue.Empty` exception (*timeout* is ignored in that case).
 
+      .. versionchanged:: 3.8
+         If the queue is closed, :exc:`ValueError` is raised instead of
+         :exc:`OSError`.
+
    .. method:: get_nowait()
 
       Equivalent to ``get(False)``.
index 88f7d267bfd351d81740cebe514ee6dca0cbedfb..d112db2cd981d116c226806ef3c84037d7b515e7 100644 (file)
@@ -78,7 +78,8 @@ class Queue(object):
         self._poll = self._reader.poll
 
     def put(self, obj, block=True, timeout=None):
-        assert not self._closed, "Queue {0!r} has been closed".format(self)
+        if self._closed:
+            raise ValueError(f"Queue {self!r} is closed")
         if not self._sem.acquire(block, timeout):
             raise Full
 
@@ -89,6 +90,8 @@ class Queue(object):
             self._notempty.notify()
 
     def get(self, block=True, timeout=None):
+        if self._closed:
+            raise ValueError(f"Queue {self!r} is closed")
         if block and timeout is None:
             with self._rlock:
                 res = self._recv_bytes()
@@ -298,7 +301,8 @@ class JoinableQueue(Queue):
         self._cond, self._unfinished_tasks = state[-2:]
 
     def put(self, obj, block=True, timeout=None):
-        assert not self._closed, "Queue {0!r} is closed".format(self)
+        if self._closed:
+            raise ValueError(f"Queue {self!r} is closed")
         if not self._sem.acquire(block, timeout):
             raise Full
 
index 814aae8fa3754e4eb708ca2caddaf96481b8b892..dc59e9fd740a0fc5f9b42014176d4648e313948b 100644 (file)
@@ -1114,6 +1114,14 @@ class _TestQueue(BaseTestCase):
         # Assert that the serialization and the hook have been called correctly
         self.assertTrue(not_serializable_obj.reduce_was_called)
         self.assertTrue(not_serializable_obj.on_queue_feeder_error_was_called)
+
+    def test_closed_queue_put_get_exceptions(self):
+        for q in multiprocessing.Queue(), multiprocessing.JoinableQueue():
+            q.close()
+            with self.assertRaisesRegex(ValueError, 'is closed'):
+                q.put('foo')
+            with self.assertRaisesRegex(ValueError, 'is closed'):
+                q.get()
 #
 #
 #
diff --git a/Misc/NEWS.d/next/Library/2018-08-30-14-44-11.bpo-22872.NhIaZ9.rst b/Misc/NEWS.d/next/Library/2018-08-30-14-44-11.bpo-22872.NhIaZ9.rst
new file mode 100644 (file)
index 0000000..547c7b1
--- /dev/null
@@ -0,0 +1,4 @@
+When the queue is closed, :exc:`ValueError` is now raised by
+:meth:`multiprocessing.Queue.put` and :meth:`multiprocessing.Queue.get`
+instead of :exc:`AssertionError` and :exc:`OSError`, respectively.
+Patch by Zackery Spytz.