diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2010-09-09 12:59:39 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2010-09-09 12:59:39 (GMT) |
commit | 6e6cc830c4968f040ded1e474db86a9aeb37c33e (patch) | |
tree | 4d374c13135309484270452752e97135055d78e0 /Doc | |
parent | bad3c88094f43f3bc7dcce22f47b8c2a8dddabcf (diff) | |
download | cpython-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 'Doc')
-rw-r--r-- | Doc/library/stdtypes.rst | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index a732fb2..b4c3eda 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -2311,7 +2311,40 @@ is generally interpreted as simple bytes. Notice how the size of the memoryview object cannot be changed. - :class:`memoryview` has two methods: + :class:`memoryview` has several methods: + + .. method:: release() + + Release the underlying buffer exposed by the memoryview object. Many + objects take special actions when a view is held on them (for example, + a :class:`bytearray` would temporarily forbid resizing); therefore, + calling release() is handy to remove these restrictions (and free any + dangling resources) as soon as possible. + + After this method has been called, any further operation on the view + raises a :class:`ValueError` (except :meth:`release()` itself which can + be called multiple times):: + + >>> m = memoryview(b'abc') + >>> m.release() + >>> m[0] + Traceback (most recent call last): + File "<stdin>", line 1, in <module> + ValueError: operation forbidden on released memoryview object + + The context management protocol can be used for a similar effect, + using the ``with`` statement:: + + >>> with memoryview(b'abc') as m: + ... m[0] + ... + b'a' + >>> m[0] + Traceback (most recent call last): + File "<stdin>", line 1, in <module> + ValueError: operation forbidden on released memoryview object + + .. versionadded:: 3.2 .. method:: tobytes() |