[Python-checkins] r73850 - in python/branches/py3k: Lib/test/test_array.py Modules/arraymodule.c

alexandre.vassalotti python-checkins at python.org
Sun Jul 5 07:38:19 CEST 2009


Author: alexandre.vassalotti
Date: Sun Jul  5 07:38:18 2009
New Revision: 73850

Log:
Issue 4509: Do not modify an array if we know the change would result
in a failure due to exported buffers.


Modified:
   python/branches/py3k/Lib/test/test_array.py
   python/branches/py3k/Modules/arraymodule.c

Modified: python/branches/py3k/Lib/test/test_array.py
==============================================================================
--- python/branches/py3k/Lib/test/test_array.py	(original)
+++ python/branches/py3k/Lib/test/test_array.py	Sun Jul  5 07:38:18 2009
@@ -749,9 +749,25 @@
         ArraySubclassWithKwargs('b', newarg=1)
 
     def test_create_from_bytes(self):
+        # XXX This test probably needs to be moved in a subclass or
+        # generalized to use self.typecode.
         a = array.array('H', b"1234")
         self.assertEqual(len(a) * a.itemsize, 4)
 
+    def test_memoryview_no_resize(self):
+        # Test for issue 4509.
+        a = array.array(self.typecode, self.example)
+        m = memoryview(a)
+        expected = m.tobytes()
+        self.assertRaises(BufferError, a.pop, 0)
+        self.assertEqual(m.tobytes(), expected)
+        self.assertRaises(BufferError, a.remove, a[0])
+        self.assertEqual(m.tobytes(), expected)
+        self.assertRaises(BufferError, a.__setitem__, slice(0, 0), a)
+        self.assertEqual(m.tobytes(), expected)
+        self.assertRaises(BufferError, a.__delitem__, slice(0, len(a)))
+        self.assertEqual(m.tobytes(), expected)
+
 
 class StringTest(BaseTest):
 

Modified: python/branches/py3k/Modules/arraymodule.c
==============================================================================
--- python/branches/py3k/Modules/arraymodule.c	(original)
+++ python/branches/py3k/Modules/arraymodule.c	Sun Jul  5 07:38:18 2009
@@ -735,6 +735,14 @@
 		ihigh = Py_SIZE(a);
 	item = a->ob_item;
 	d = n - (ihigh-ilow);
+	/* Issue #4509: If the array has exported buffers and the slice
+	   assignment would change the size of the array, fail early to make
+	   sure we don't modify it. */
+	if (d != 0 && a->ob_exports > 0) {
+		PyErr_SetString(PyExc_BufferError, 
+			"cannot resize an array that is exporting buffers");
+		return -1;
+	}
 	if (d < 0) { /* Delete -d items */
 		memmove(item + (ihigh+d)*a->ob_descr->itemsize,
 			item + ihigh*a->ob_descr->itemsize,


More information about the Python-checkins mailing list