[Python-checkins] bpo-41344: Raise ValueError when creating shared memory of size 0 (GH-21556)

Vinay Sharma webhook-mailer at python.org
Sun Aug 30 15:03:19 EDT 2020


https://github.com/python/cpython/commit/475a5fbb5644ea200c990d85d8c264e78ab6c7ea
commit: 475a5fbb5644ea200c990d85d8c264e78ab6c7ea
branch: master
author: Vinay Sharma <vinay04sharma at icloud.com>
committer: GitHub <noreply at github.com>
date: 2020-08-30T20:03:11+01:00
summary:

bpo-41344: Raise ValueError when creating shared memory of size 0 (GH-21556)

files:
A Misc/NEWS.d/next/Library/2020-07-20-13-27-48.bpo-41344.iKipNd.rst
M Lib/multiprocessing/shared_memory.py
M Lib/test/_test_multiprocessing.py

diff --git a/Lib/multiprocessing/shared_memory.py b/Lib/multiprocessing/shared_memory.py
index a3a5fcf4aba1e..122b3fcebf3fe 100644
--- a/Lib/multiprocessing/shared_memory.py
+++ b/Lib/multiprocessing/shared_memory.py
@@ -76,6 +76,8 @@ def __init__(self, name=None, create=False, size=0):
             raise ValueError("'size' must be a positive integer")
         if create:
             self._flags = _O_CREX | os.O_RDWR
+            if size == 0:
+                raise ValueError("'size' must be a positive number different from zero")
         if name is None and not self._flags & os.O_EXCL:
             raise ValueError("'name' can only be None if create=True")
 
diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py
index 6a0e1016aff8d..fd3b4303f034c 100644
--- a/Lib/test/_test_multiprocessing.py
+++ b/Lib/test/_test_multiprocessing.py
@@ -3880,6 +3880,18 @@ class OptionalAttachSharedMemory(shared_memory.SharedMemory):
 
         sms.close()
 
+        # Test creating a shared memory segment with negative size
+        with self.assertRaises(ValueError):
+            sms_invalid = shared_memory.SharedMemory(create=True, size=-1)
+
+        # Test creating a shared memory segment with size 0
+        with self.assertRaises(ValueError):
+            sms_invalid = shared_memory.SharedMemory(create=True, size=0)
+
+        # Test creating a shared memory segment without size argument
+        with self.assertRaises(ValueError):
+            sms_invalid = shared_memory.SharedMemory(create=True)
+
     def test_shared_memory_across_processes(self):
         # bpo-40135: don't define shared memory block's name in case of
         # the failure when we run multiprocessing tests in parallel.
diff --git a/Misc/NEWS.d/next/Library/2020-07-20-13-27-48.bpo-41344.iKipNd.rst b/Misc/NEWS.d/next/Library/2020-07-20-13-27-48.bpo-41344.iKipNd.rst
new file mode 100644
index 0000000000000..475bc9bddb0d5
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-07-20-13-27-48.bpo-41344.iKipNd.rst
@@ -0,0 +1 @@
+Prevent creating :class:`shared_memory.SharedMemory` objects with :code:`size=0`.



More information about the Python-checkins mailing list