]> 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:02:37 +0000 (10:02 +0000)
committerMark Dickinson <mdickinson@enthought.com>
Sat, 26 Mar 2011 10:02:37 +0000 (10:02 +0000)
Lib/multiprocessing/sharedctypes.py
Lib/test/test_multiprocessing.py
Misc/NEWS

index 2430be7510d938f1a75cfbe4a23203ef9995d7ac..1eb044dd54fd5a1de06997f857a74c8631413474 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, long)):
         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 bdfc171253745d9400a95c85241e6a8d9f72cf7b..0df2f7803bbeaa94f7491d54dde8468361a56909 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), range(10))
+            del arr
+
     @unittest.skipIf(c_int is None, "requires _ctypes")
     def test_rawarray(self):
         self.test_array(raw=True)
index 444728966da4dfd5d1c86a1203c1f5b64afba0cf..d435890033f86c10223319418c165b1ae7538cda 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -9,6 +9,10 @@ What's New in Python 2.7.2?
 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 #11450: Don't truncate hg version info in Py_GetBuildInfo() when
   there are many tags (e.g. when using mq).  Patch by Nadeem Vawda.