mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
Issue #14181: Test creating memoryviews from a static exporter with both
view.obj==NULL and view.obj==base.
This commit is contained in:
parent
8d217c6a71
commit
bf6c7eca43
2 changed files with 219 additions and 1 deletions
|
@ -3515,6 +3515,100 @@ class TestBufferProtocol(unittest.TestCase):
|
|||
del y, z
|
||||
self.assertEqual(m[1:3], x[1:3])
|
||||
|
||||
def test_memoryview_from_static_exporter(self):
|
||||
|
||||
fmt = 'B'
|
||||
lst = [0,1,2,3,4,5,6,7,8,9,10,11]
|
||||
|
||||
# exceptions
|
||||
self.assertRaises(TypeError, staticarray, 1, 2, 3)
|
||||
|
||||
# view.obj==x
|
||||
x = staticarray()
|
||||
y = memoryview(x)
|
||||
self.verify(y, obj=x,
|
||||
itemsize=1, fmt=fmt, readonly=1,
|
||||
ndim=1, shape=[12], strides=[1],
|
||||
lst=lst)
|
||||
for i in range(12):
|
||||
self.assertEqual(y[i], i)
|
||||
del x
|
||||
del y
|
||||
|
||||
x = staticarray()
|
||||
y = memoryview(x)
|
||||
del y
|
||||
del x
|
||||
|
||||
x = staticarray()
|
||||
y = ndarray(x, getbuf=PyBUF_FULL_RO)
|
||||
z = ndarray(y, getbuf=PyBUF_FULL_RO)
|
||||
m = memoryview(z)
|
||||
self.assertIs(y.obj, x)
|
||||
self.assertIs(m.obj, z)
|
||||
self.verify(m, obj=z,
|
||||
itemsize=1, fmt=fmt, readonly=1,
|
||||
ndim=1, shape=[12], strides=[1],
|
||||
lst=lst)
|
||||
del x, y, z, m
|
||||
|
||||
x = staticarray()
|
||||
y = ndarray(x, getbuf=PyBUF_FULL_RO, flags=ND_REDIRECT)
|
||||
z = ndarray(y, getbuf=PyBUF_FULL_RO, flags=ND_REDIRECT)
|
||||
m = memoryview(z)
|
||||
self.assertIs(y.obj, x)
|
||||
self.assertIs(z.obj, x)
|
||||
self.assertIs(m.obj, x)
|
||||
self.verify(m, obj=x,
|
||||
itemsize=1, fmt=fmt, readonly=1,
|
||||
ndim=1, shape=[12], strides=[1],
|
||||
lst=lst)
|
||||
del x, y, z, m
|
||||
|
||||
# view.obj==NULL
|
||||
x = staticarray(legacy_mode=True)
|
||||
y = memoryview(x)
|
||||
self.verify(y, obj=None,
|
||||
itemsize=1, fmt=fmt, readonly=1,
|
||||
ndim=1, shape=[12], strides=[1],
|
||||
lst=lst)
|
||||
for i in range(12):
|
||||
self.assertEqual(y[i], i)
|
||||
del x
|
||||
del y
|
||||
|
||||
x = staticarray(legacy_mode=True)
|
||||
y = memoryview(x)
|
||||
del y
|
||||
del x
|
||||
|
||||
x = staticarray(legacy_mode=True)
|
||||
y = ndarray(x, getbuf=PyBUF_FULL_RO)
|
||||
z = ndarray(y, getbuf=PyBUF_FULL_RO)
|
||||
m = memoryview(z)
|
||||
self.assertIs(y.obj, None)
|
||||
self.assertIs(m.obj, z)
|
||||
self.verify(m, obj=z,
|
||||
itemsize=1, fmt=fmt, readonly=1,
|
||||
ndim=1, shape=[12], strides=[1],
|
||||
lst=lst)
|
||||
del x, y, z, m
|
||||
|
||||
x = staticarray(legacy_mode=True)
|
||||
y = ndarray(x, getbuf=PyBUF_FULL_RO, flags=ND_REDIRECT)
|
||||
z = ndarray(y, getbuf=PyBUF_FULL_RO, flags=ND_REDIRECT)
|
||||
m = memoryview(z)
|
||||
# Clearly setting view.obj==NULL is inferior, since it
|
||||
# messes up the redirection chain:
|
||||
self.assertIs(y.obj, None)
|
||||
self.assertIs(z.obj, y)
|
||||
self.assertIs(m.obj, y)
|
||||
self.verify(m, obj=y,
|
||||
itemsize=1, fmt=fmt, readonly=1,
|
||||
ndim=1, shape=[12], strides=[1],
|
||||
lst=lst)
|
||||
del x, y, z, m
|
||||
|
||||
def test_issue_7385(self):
|
||||
x = ndarray([1,2,3], shape=[3], flags=ND_GETBUF_FAIL)
|
||||
self.assertRaises(BufferError, memoryview, x)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue