[Python-checkins] cpython (3.1): Issue #11675: Zero-out newly-created multiprocessing.[Raw]Array objects.

mark.dickinson python-checkins at python.org
Sat Mar 26 11:23:33 CET 2011


http://hg.python.org/cpython/rev/0cb276628528
changeset:   68962:0cb276628528
branch:      3.1
parent:      68951:ec25055d30f1
user:        Mark Dickinson <mdickinson at enthought.com>
date:        Sat Mar 26 10:19:03 2011 +0000
summary:
  Issue #11675:  Zero-out newly-created multiprocessing.[Raw]Array objects.

files:
  Lib/multiprocessing/sharedctypes.py |   4 +++-
  Lib/test/test_multiprocessing.py    |  15 +++++++++++++++
  Misc/NEWS                           |   4 ++++
  3 files changed, 22 insertions(+), 1 deletions(-)


diff --git a/Lib/multiprocessing/sharedctypes.py b/Lib/multiprocessing/sharedctypes.py
--- a/Lib/multiprocessing/sharedctypes.py
+++ b/Lib/multiprocessing/sharedctypes.py
@@ -80,7 +80,9 @@
     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
--- a/Lib/test/test_multiprocessing.py
+++ b/Lib/test/test_multiprocessing.py
@@ -914,6 +914,21 @@
         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)
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,10 @@
 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).

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list