Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Lib/test/test_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2846,6 +2846,32 @@ class BEPoint(ctypes.BigEndianStructure):
self.assertEqual(m2.strides, (1,))
self.assertEqual(m2.suboffsets, ())

def test_memoryview_cast_f_contiguous_ND_1D(self):
nd = ndarray(list(range(12)),shape=[3, 4], format='B', flags=ND_FORTRAN)
m = memoryview(nd)
self.assertTrue(m.f_contiguous)
self.assertTrue(m.contiguous)

m1 = m.cast('B')
self.assertEqual(m1.ndim, 1)
self.assertEqual(m1.shape, (m.nbytes,))
self.assertEqual(m1.strides, (1,))
self.assertTrue(m1.c_contiguous)
self.assertTrue(m1.contiguous)
self.assertEqual(m1.tobytes(), memoryview(nd).tobytes(order='F'))

for fmt in ('B', 'b', 'c', 'H', 'I'):
size = struct.calcsize(fmt)
if m.nbytes % size == 0:
m2 = m.cast(fmt)
self.assertEqual(m2.ndim, 1)
self.assertEqual(m2.shape, (m.nbytes // size,))
self.assertTrue(m2.contiguous)

m3 = m[::-1]
with self.assertRaises(TypeError):
m3.cast('B')

def test_memoryview_tolist(self):

# Most tolist() tests are in self.verify() etc.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
memoryview.cast() now allows casting from N-D to 1-D for F-contiguous.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
memoryview.cast() now allows casting from N-D to 1-D for F-contiguous.
:meth:`memoryview.cast` now allows casting from N-D to 1-D for F-contiguous.

8 changes: 5 additions & 3 deletions Objects/memoryobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1456,9 +1456,11 @@ memoryview_cast_impl(PyMemoryViewObject *self, PyObject *format,
CHECK_RESTRICTED(self);

if (!MV_C_CONTIGUOUS(self->flags)) {
PyErr_SetString(PyExc_TypeError,
"memoryview: casts are restricted to C-contiguous views");
return NULL;
if(shape || self->view.ndim == 1 || !MV_F_CONTIGUOUS(self->flags)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed space, and self->view.ndim == 1 looks redundant -- for 1D view C- and F-contiguity are equivalent.

Suggested change
if(shape || self->view.ndim == 1 || !MV_F_CONTIGUOUS(self->flags)) {
if (shape || !MV_F_CONTIGUOUS(self->flags)) {

PyErr_SetString(PyExc_TypeError,
"memoryview: casts are restricted to C-contiguous views");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is now slightly misleading, since F-contiguous -> 1D is allowed.

Maybe just "contiguous views"?

Suggested change
"memoryview: casts are restricted to C-contiguous views");
"memoryview: casts are restricted to contiguous views");

return NULL;
}
}
if ((shape || self->view.ndim != 1) && zero_in_shape(self)) {
PyErr_SetString(PyExc_TypeError,
Expand Down
Loading