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

(cherry picked from commit 475a5fbb56)

Co-authored-by: Vinay Sharma <vinay04sharma@icloud.com>

Co-authored-by: Vinay Sharma <vinay04sharma@icloud.com>
This commit is contained in:
Miss Islington (bot) 2020-08-30 12:42:27 -07:00 committed by GitHub
parent 85ca9c049c
commit 38e32872eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 0 deletions

View File

@ -75,6 +75,8 @@ class SharedMemory:
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")

View File

@ -3827,6 +3827,18 @@ class _TestSharedMemory(BaseTestCase):
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.

View File

@ -0,0 +1 @@
Prevent creating :class:`shared_memory.SharedMemory` objects with :code:`size=0`.