summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2009-08-13 09:05:38 (GMT)
committerGeorg Brandl <georg@python.org>2009-08-13 09:05:38 (GMT)
commitdf475156f430a5f3fa78f5163bb346ec350ac430 (patch)
tree33f0ec58868af0a80cef95f823f1375a00da38b6
parentae83d6ee371b5e6aeebe303fb853f5c45638414c (diff)
downloadcpython-df475156f430a5f3fa78f5163bb346ec350ac430.zip
cpython-df475156f430a5f3fa78f5163bb346ec350ac430.tar.gz
cpython-df475156f430a5f3fa78f5163bb346ec350ac430.tar.bz2
Merged revisions 73862,73872 via svnmerge from
svn+ssh://svn.python.org/python/branches/py3k ........ r73862 | alexandre.vassalotti | 2009-07-05 21:57:00 +0200 (So, 05 Jul 2009) | 2 lines Add the fix for issue 4509 to the mapping methods. ........ r73872 | gregory.p.smith | 2009-07-07 07:06:04 +0200 (Di, 07 Jul 2009) | 2 lines Add a unittest for r73566. ........
-rwxr-xr-xLib/test/test_array.py47
-rw-r--r--Lib/test/test_zipfile.py8
-rw-r--r--Modules/arraymodule.c10
3 files changed, 39 insertions, 26 deletions
diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py
index 3dd4f6d..93c6cc0 100755
--- a/Lib/test/test_array.py
+++ b/Lib/test/test_array.py
@@ -723,22 +723,37 @@ class BaseTest(unittest.TestCase):
def test_buffer(self):
a = array.array(self.typecode, self.example)
m = memoryview(a)
- b = bytes(m)
- self.assertEqual(b, a.tostring())
- self.assertEqual(b[0], a.tostring()[0])
- # Resizing is forbidden when there are buffer exports
+ expected = m.tobytes()
+ self.assertEqual(a.tostring(), expected)
+ self.assertEqual(a.tostring()[0], expected[0])
+ # Resizing is forbidden when there are buffer exports.
+ # For issue 4509, we also check after each error that
+ # the array was not modified.
self.assertRaises(BufferError, a.append, a[0])
+ self.assertEqual(m.tobytes(), expected)
self.assertRaises(BufferError, a.extend, a[0:1])
+ self.assertEqual(m.tobytes(), expected)
self.assertRaises(BufferError, a.remove, a[0])
+ self.assertEqual(m.tobytes(), expected)
+ self.assertRaises(BufferError, a.pop, 0)
+ self.assertEqual(m.tobytes(), expected)
self.assertRaises(BufferError, a.fromlist, a.tolist())
+ self.assertEqual(m.tobytes(), expected)
self.assertRaises(BufferError, a.fromstring, a.tostring())
+ self.assertEqual(m.tobytes(), expected)
if self.typecode == 'u':
self.assertRaises(BufferError, a.fromunicode, a.tounicode())
+ self.assertEqual(m.tobytes(), expected)
+ self.assertRaises(BufferError, operator.imul, a, 2)
+ self.assertEqual(m.tobytes(), expected)
+ self.assertRaises(BufferError, operator.imul, a, 0)
+ self.assertEqual(m.tobytes(), expected)
self.assertRaises(BufferError, operator.setitem, a, slice(0, 0), a)
+ self.assertEqual(m.tobytes(), expected)
self.assertRaises(BufferError, operator.delitem, a, 0)
+ self.assertEqual(m.tobytes(), expected)
self.assertRaises(BufferError, operator.delitem, a, slice(0, 1))
- self.assertRaises(BufferError, operator.imul, a, 2)
- self.assertRaises(BufferError, operator.imul, a, 0)
+ self.assertEqual(m.tobytes(), expected)
def test_weakref(self):
s = array.array(self.typecode, self.example)
@@ -767,26 +782,6 @@ class BaseTest(unittest.TestCase):
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)
- self.assertRaises(BufferError, a.__imul__, 2)
- self.assertEqual(m.tobytes(), expected)
- self.assertRaises(BufferError, a.__iadd__, a)
- self.assertEqual(m.tobytes(), expected)
- self.assertRaises(BufferError, a.extend, a)
- self.assertEqual(m.tobytes(), expected)
-
class StringTest(BaseTest):
diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py
index 82602c1..ed800c5 100644
--- a/Lib/test/test_zipfile.py
+++ b/Lib/test/test_zipfile.py
@@ -379,6 +379,14 @@ class TestsWithSourceFile(unittest.TestCase):
for f in (TESTFN2, TemporaryFile(), io.BytesIO()):
self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED)
+ def test_writestr_extended_local_header_issue1202(self):
+ orig_zip = zipfile.ZipFile(TESTFN2, 'w')
+ for data in 'abcdefghijklmnop':
+ zinfo = zipfile.ZipInfo(data)
+ zinfo.flag_bits |= 0x08 # Include an extended local header.
+ orig_zip.writestr(zinfo, data)
+ orig_zip.close()
+
def tearDown(self):
os.remove(TESTFN)
os.remove(TESTFN2)
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
index c1a0f53..b24b4c9 100644
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -1699,6 +1699,16 @@ array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value)
if ((step > 0 && stop < start) ||
(step < 0 && stop > start))
stop = start;
+
+ /* 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 ((needed == 0 || slicelength != needed) && self->ob_exports > 0) {
+ PyErr_SetString(PyExc_BufferError,
+ "cannot resize an array that is exporting buffers");
+ return -1;
+ }
+
if (step == 1) {
if (slicelength > needed) {
memmove(self->ob_item + (start + needed) * itemsize,