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

Skip to content

Commit 89461ef

Browse files
committed
Issue #11675: Zero-out newly-created multiprocessing.[Raw]Array objects.
1 parent 211643b commit 89461ef

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

Lib/multiprocessing/sharedctypes.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ def RawArray(typecode_or_type, size_or_initializer):
8080
type_ = typecode_to_type.get(typecode_or_type, typecode_or_type)
8181
if isinstance(size_or_initializer, int):
8282
type_ = type_ * size_or_initializer
83-
return _new_value(type_)
83+
obj = _new_value(type_)
84+
ctypes.memset(ctypes.addressof(obj), 0, ctypes.sizeof(obj))
85+
return obj
8486
else:
8587
type_ = type_ * len(size_or_initializer)
8688
result = _new_value(type_)

Lib/test/test_multiprocessing.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,21 @@ def test_array(self, raw=False):
913913

914914
self.assertEqual(list(arr[:]), seq)
915915

916+
@unittest.skipIf(c_int is None, "requires _ctypes")
917+
def test_array_from_size(self):
918+
size = 10
919+
# Test for zeroing (see issue #11675).
920+
# The repetition below strengthens the test by increasing the chances
921+
# of previously allocated non-zero memory being used for the new array
922+
# on the 2nd and 3rd loops.
923+
for _ in range(3):
924+
arr = self.Array('i', size)
925+
self.assertEqual(len(arr), size)
926+
self.assertEqual(list(arr), [0] * size)
927+
arr[:] = range(10)
928+
self.assertEqual(list(arr), list(range(10)))
929+
del arr
930+
916931
@unittest.skipIf(c_int is None, "requires _ctypes")
917932
def test_rawarray(self):
918933
self.test_array(raw=True)

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ What's New in Python 3.1.4?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #11675: multiprocessing.[Raw]Array objects created from an integer size
14+
are now zeroed on creation. This matches the behaviour specified by the
15+
documentation.
16+
1317
- Issue #8651: PyArg_Parse*() functions raise an OverflowError if the file
1418
doesn't have PY_SSIZE_T_CLEAN define and the size doesn't fit in an int
1519
(length bigger than 2^31-1 bytes).

0 commit comments

Comments
 (0)