summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMark Dickinson <mdickinson@enthought.com>2011-03-26 10:19:03 (GMT)
committerMark Dickinson <mdickinson@enthought.com>2011-03-26 10:19:03 (GMT)
commit89461ef8fcc87996791a383f1973542a3487ce4f (patch)
tree812ba093d8d39f811ed3ea68c6ab36223b77f16a /Lib
parent211643b67320b253a2f4f723ece4423559d28ebf (diff)
downloadcpython-89461ef8fcc87996791a383f1973542a3487ce4f.zip
cpython-89461ef8fcc87996791a383f1973542a3487ce4f.tar.gz
cpython-89461ef8fcc87996791a383f1973542a3487ce4f.tar.bz2
Issue #11675: Zero-out newly-created multiprocessing.[Raw]Array objects.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/multiprocessing/sharedctypes.py4
-rw-r--r--Lib/test/test_multiprocessing.py15
2 files changed, 18 insertions, 1 deletions
diff --git a/Lib/multiprocessing/sharedctypes.py b/Lib/multiprocessing/sharedctypes.py
index e83ce58..1e694da 100644
--- a/Lib/multiprocessing/sharedctypes.py
+++ b/Lib/multiprocessing/sharedctypes.py
@@ -80,7 +80,9 @@ def RawArray(typecode_or_type, size_or_initializer):
type_ = typecode_to_type.get(typecode_or_type, typecode_or_type)
if isinstance(size_or_initializer, int):
type_ = type_ * size_or_initializer
- return _new_value(type_)
+ obj = _new_value(type_)
+ ctypes.memset(ctypes.addressof(obj), 0, ctypes.sizeof(obj))
+ return obj
else:
type_ = type_ * len(size_or_initializer)
result = _new_value(type_)
diff --git a/Lib/test/test_multiprocessing.py b/Lib/test/test_multiprocessing.py
index f4031de..1136ab2 100644
--- a/Lib/test/test_multiprocessing.py
+++ b/Lib/test/test_multiprocessing.py
@@ -914,6 +914,21 @@ class _TestArray(BaseTestCase):
self.assertEqual(list(arr[:]), seq)
@unittest.skipIf(c_int is None, "requires _ctypes")
+ def test_array_from_size(self):
+ size = 10
+ # Test for zeroing (see issue #11675).
+ # The repetition below strengthens the test by increasing the chances
+ # of previously allocated non-zero memory being used for the new array
+ # on the 2nd and 3rd loops.
+ for _ in range(3):
+ arr = self.Array('i', size)
+ self.assertEqual(len(arr), size)
+ self.assertEqual(list(arr), [0] * size)
+ arr[:] = range(10)
+ self.assertEqual(list(arr), list(range(10)))
+ del arr
+
+ @unittest.skipIf(c_int is None, "requires _ctypes")
def test_rawarray(self):
self.test_array(raw=True)