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

Skip to content

Commit 633872e

Browse files
committed
Merge #11675
2 parents 01606de + 89461ef commit 633872e

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

917917
self.assertEqual(list(arr[:]), seq)
918918

919+
@unittest.skipIf(c_int is None, "requires _ctypes")
920+
def test_array_from_size(self):
921+
size = 10
922+
# Test for zeroing (see issue #11675).
923+
# The repetition below strengthens the test by increasing the chances
924+
# of previously allocated non-zero memory being used for the new array
925+
# on the 2nd and 3rd loops.
926+
for _ in range(3):
927+
arr = self.Array('i', size)
928+
self.assertEqual(len(arr), size)
929+
self.assertEqual(list(arr), [0] * size)
930+
arr[:] = range(10)
931+
self.assertEqual(list(arr), list(range(10)))
932+
del arr
933+
919934
@unittest.skipIf(c_int is None, "requires _ctypes")
920935
def test_rawarray(self):
921936
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.2.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 #11395: io.FileIO().write() clamps the data length to 32,767 bytes on
1418
Windows if the file is a TTY to workaround a Windows bug. The Windows console
1519
returns an error (12: not enough space error) on writing into stdout if

0 commit comments

Comments
 (0)