]> granicus.if.org Git - python/commitdiff
Issue #11675: Zero-out newly-created multiprocessing.[Raw]Array objects.
authorMark Dickinson <mdickinson@enthought.com>
Sat, 26 Mar 2011 10:19:03 +0000 (10:19 +0000)
committerMark Dickinson <mdickinson@enthought.com>
Sat, 26 Mar 2011 10:19:03 +0000 (10:19 +0000)
Lib/multiprocessing/sharedctypes.py
Lib/test/test_multiprocessing.py
Misc/NEWS

index e83ce584596eaed1d82468eb4c132eba94e05191..1e694da49d9e9a98b77c3f05b8759cdf2f52caf5 100644 (file)
@@ -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_)
index f4031de61fdba08e7bf73e9ffde898145a0f5e93..1136ab2920edf8e710fe6dc3f8c8653ec7deca07 100644 (file)
@@ -913,6 +913,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)
index fca77ef65e3bb6351f5e7f83ccf7b9696bffcfb9..6825931972bf43c576d1939bd9b7fd563ad798dc 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,10 @@ What's New in Python 3.1.4?
 Core and Builtins
 -----------------
 
+- Issue #11675: multiprocessing.[Raw]Array objects created from an integer size
+  are now zeroed on creation.  This matches the behaviour specified by the
+  documentation.
+
 - Issue #8651: PyArg_Parse*() functions raise an OverflowError if the file
   doesn't have PY_SSIZE_T_CLEAN define and the size doesn't fit in an int
   (length bigger than 2^31-1 bytes).