(original) (raw)
changeset: 85940:1e13a58c1b92 parent: 85938:7d3695937362 parent: 85939:b08e092df155 user: Antoine Pitrou solipsis@pitrou.net date: Thu Oct 03 19:56:54 2013 +0200 files: Lib/test/test_buffer.py Misc/NEWS Objects/memoryobject.c description: Issue #19014: memoryview.cast() is now allowed on zero-length views. diff -r 7d3695937362 -r 1e13a58c1b92 Lib/test/test_buffer.py --- a/Lib/test/test_buffer.py Thu Oct 03 12:10:49 2013 +0300 +++ b/Lib/test/test_buffer.py Thu Oct 03 19:56:54 2013 +0200 @@ -2432,15 +2432,22 @@ self.assertRaises(ValueError, get_contiguous, nd[::-1], PyBUF_READ, 'C') def test_memoryview_cast_zero_shape(self): - # Casts are undefined if shape contains zeros. These arrays are - # regarded as C-contiguous by Numpy and PyBuffer_GetContiguous(), - # so they are not caught by the test for C-contiguity in memory_cast(). + # Casts are undefined if buffer is multidimensional and shape + # contains zeros. These arrays are regarded as C-contiguous by + # Numpy and PyBuffer_GetContiguous(), so they are not caught by + # the test for C-contiguity in memory_cast(). items = [1,2,3] for shape in ([0,3,3], [3,0,3], [0,3,3]): ex = ndarray(items, shape=shape) self.assertTrue(ex.c_contiguous) msrc = memoryview(ex) self.assertRaises(TypeError, msrc.cast, 'c') + # Monodimensional empty view can be cast (issue #19014). + for fmt, _, _ in iter_format(1, 'memoryview'): + msrc = memoryview(b'') + m = msrc.cast(fmt) + self.assertEqual(m.tobytes(), b'') + self.assertEqual(m.tolist(), []) def test_memoryview_struct_module(self): diff -r 7d3695937362 -r 1e13a58c1b92 Misc/NEWS --- a/Misc/NEWS Thu Oct 03 12:10:49 2013 +0300 +++ b/Misc/NEWS Thu Oct 03 19:56:54 2013 +0200 @@ -10,13 +10,14 @@ Core and Builtins ----------------- +- Issue #19014: memoryview.cast() is now allowed on zero-length views. + - Issue #18690: memoryview is now automatically registered with collections.abc.Sequence - Issue #19078: memoryview now correctly supports the reversed builtin (Patch by Claudiu Popa) - Library ------- diff -r 7d3695937362 -r 1e13a58c1b92 Objects/memoryobject.c --- a/Objects/memoryobject.c Thu Oct 03 12:10:49 2013 +0300 +++ b/Objects/memoryobject.c Thu Oct 03 19:56:54 2013 +0200 @@ -1330,7 +1330,7 @@ "memoryview: casts are restricted to C-contiguous views"); return NULL; } - if (zero_in_shape(self)) { + if ((shape || self->view.ndim != 1) && zero_in_shape(self)) { PyErr_SetString(PyExc_TypeError, "memoryview: cannot cast view with zeros in shape or strides"); return NULL; /solipsis@pitrou.net