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

Skip to content

Commit 657bd0a

Browse files
committed
Merge #11675
2 parents b9a1c56 + 633872e commit 657bd0a

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
@@ -928,6 +928,21 @@ def test_array(self, raw=False):
928928

929929
self.assertEqual(list(arr[:]), seq)
930930

931+
@unittest.skipIf(c_int is None, "requires _ctypes")
932+
def test_array_from_size(self):
933+
size = 10
934+
# Test for zeroing (see issue #11675).
935+
# The repetition below strengthens the test by increasing the chances
936+
# of previously allocated non-zero memory being used for the new array
937+
# on the 2nd and 3rd loops.
938+
for _ in range(3):
939+
arr = self.Array('i', size)
940+
self.assertEqual(len(arr), size)
941+
self.assertEqual(list(arr), [0] * size)
942+
arr[:] = range(10)
943+
self.assertEqual(list(arr), list(range(10)))
944+
del arr
945+
931946
@unittest.skipIf(c_int is None, "requires _ctypes")
932947
def test_rawarray(self):
933948
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.3 Alpha 1?
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 #10998: Remove mentions of -Q, sys.flags.division_warning and
1418
Py_DivisionWarningFlag left over from Python 2.
1519

0 commit comments

Comments
 (0)