summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_memoryview.py
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2010-09-09 12:59:39 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2010-09-09 12:59:39 (GMT)
commit6e6cc830c4968f040ded1e474db86a9aeb37c33e (patch)
tree4d374c13135309484270452752e97135055d78e0 /Lib/test/test_memoryview.py
parentbad3c88094f43f3bc7dcce22f47b8c2a8dddabcf (diff)
downloadcpython-6e6cc830c4968f040ded1e474db86a9aeb37c33e.zip
cpython-6e6cc830c4968f040ded1e474db86a9aeb37c33e.tar.gz
cpython-6e6cc830c4968f040ded1e474db86a9aeb37c33e.tar.bz2
Issue #9757: memoryview objects get a release() method to release the
underlying buffer (previously this was only done when deallocating the memoryview), and gain support for the context management protocol.
Diffstat (limited to 'Lib/test/test_memoryview.py')
-rw-r--r--Lib/test/test_memoryview.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/Lib/test/test_memoryview.py b/Lib/test/test_memoryview.py
index 8e56df9..d56d64f 100644
--- a/Lib/test/test_memoryview.py
+++ b/Lib/test/test_memoryview.py
@@ -225,6 +225,51 @@ class AbstractMemoryTests:
gc.collect()
self.assertTrue(wr() is None, wr())
+ def _check_released(self, m, tp):
+ check = self.assertRaisesRegexp(ValueError, "released")
+ with check: bytes(m)
+ with check: m.tobytes()
+ with check: m.tolist()
+ with check: m[0]
+ with check: m[0] = b'x'
+ with check: len(m)
+ with check: m.format
+ with check: m.itemsize
+ with check: m.ndim
+ with check: m.readonly
+ with check: m.shape
+ with check: m.strides
+ with check:
+ with m:
+ pass
+ # str() and repr() still function
+ self.assertIn("released memory", str(m))
+ self.assertIn("released memory", repr(m))
+ self.assertEqual(m, m)
+ self.assertNotEqual(m, memoryview(tp(self._source)))
+ self.assertNotEqual(m, tp(self._source))
+
+ def test_contextmanager(self):
+ for tp in self._types:
+ b = tp(self._source)
+ m = self._view(b)
+ with m as cm:
+ self.assertIs(cm, m)
+ self._check_released(m, tp)
+ m = self._view(b)
+ # Can release explicitly inside the context manager
+ with m:
+ m.release()
+
+ def test_release(self):
+ for tp in self._types:
+ b = tp(self._source)
+ m = self._view(b)
+ m.release()
+ self._check_released(m, tp)
+ # Can be called a second time (it's a no-op)
+ m.release()
+ self._check_released(m, tp)
# Variations on source objects for the buffer: bytes-like objects, then arrays
# with itemsize > 1.