mirror of
https://github.com/python/cpython.git
synced 2025-10-06 23:21:06 +00:00
Issue #19014: memoryview.cast() is now allowed on zero-length views.
This commit is contained in:
commit
0e61ed8400
3 changed files with 13 additions and 5 deletions
|
@ -2432,15 +2432,22 @@ class TestBufferProtocol(unittest.TestCase):
|
||||||
self.assertRaises(ValueError, get_contiguous, nd[::-1], PyBUF_READ, 'C')
|
self.assertRaises(ValueError, get_contiguous, nd[::-1], PyBUF_READ, 'C')
|
||||||
|
|
||||||
def test_memoryview_cast_zero_shape(self):
|
def test_memoryview_cast_zero_shape(self):
|
||||||
# Casts are undefined if shape contains zeros. These arrays are
|
# Casts are undefined if buffer is multidimensional and shape
|
||||||
# regarded as C-contiguous by Numpy and PyBuffer_GetContiguous(),
|
# contains zeros. These arrays are regarded as C-contiguous by
|
||||||
# so they are not caught by the test for C-contiguity in memory_cast().
|
# Numpy and PyBuffer_GetContiguous(), so they are not caught by
|
||||||
|
# the test for C-contiguity in memory_cast().
|
||||||
items = [1,2,3]
|
items = [1,2,3]
|
||||||
for shape in ([0,3,3], [3,0,3], [0,3,3]):
|
for shape in ([0,3,3], [3,0,3], [0,3,3]):
|
||||||
ex = ndarray(items, shape=shape)
|
ex = ndarray(items, shape=shape)
|
||||||
self.assertTrue(ex.c_contiguous)
|
self.assertTrue(ex.c_contiguous)
|
||||||
msrc = memoryview(ex)
|
msrc = memoryview(ex)
|
||||||
self.assertRaises(TypeError, msrc.cast, 'c')
|
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):
|
def test_memoryview_struct_module(self):
|
||||||
|
|
||||||
|
|
|
@ -10,13 +10,14 @@ Projected release date: 2013-10-20
|
||||||
Core and Builtins
|
Core and Builtins
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
|
- Issue #19014: memoryview.cast() is now allowed on zero-length views.
|
||||||
|
|
||||||
- Issue #18690: memoryview is now automatically registered with
|
- Issue #18690: memoryview is now automatically registered with
|
||||||
collections.abc.Sequence
|
collections.abc.Sequence
|
||||||
|
|
||||||
- Issue #19078: memoryview now correctly supports the reversed builtin
|
- Issue #19078: memoryview now correctly supports the reversed builtin
|
||||||
(Patch by Claudiu Popa)
|
(Patch by Claudiu Popa)
|
||||||
|
|
||||||
|
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
|
|
@ -1330,7 +1330,7 @@ memory_cast(PyMemoryViewObject *self, PyObject *args, PyObject *kwds)
|
||||||
"memoryview: casts are restricted to C-contiguous views");
|
"memoryview: casts are restricted to C-contiguous views");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (zero_in_shape(self)) {
|
if ((shape || self->view.ndim != 1) && zero_in_shape(self)) {
|
||||||
PyErr_SetString(PyExc_TypeError,
|
PyErr_SetString(PyExc_TypeError,
|
||||||
"memoryview: cannot cast view with zeros in shape or strides");
|
"memoryview: cannot cast view with zeros in shape or strides");
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue