summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2011-01-24 22:14:42 (GMT)
committerRaymond Hettinger <python@rcn.com>2011-01-24 22:14:42 (GMT)
commitf4f0e6c7a754dd13344b61487f4729555604f392 (patch)
tree948f42f04bd335546a57e055bdd085f73dbafe19 /Doc
parent122539e2870bcaf0ccd64bae0ade3cf4691f59da (diff)
downloadcpython-f4f0e6c7a754dd13344b61487f4729555604f392.zip
cpython-f4f0e6c7a754dd13344b61487f4729555604f392.tar.gz
cpython-f4f0e6c7a754dd13344b61487f4729555604f392.tar.bz2
Add entry for io.BytesIO.getbuffer().
Diffstat (limited to 'Doc')
-rw-r--r--Doc/whatsnew/3.2.rst29
1 files changed, 29 insertions, 0 deletions
diff --git a/Doc/whatsnew/3.2.rst b/Doc/whatsnew/3.2.rst
index e846a0f..1693f7c 100644
--- a/Doc/whatsnew/3.2.rst
+++ b/Doc/whatsnew/3.2.rst
@@ -992,6 +992,35 @@ implemented::
(Patch submitted by Daniel Urban; :issue:`5867`.)
+io
+--
+
+The :class:`io.BytesIO` has a new method, :meth:`~io.BytesIO.getbuffer`, which
+provides functionality similar to :func:`memoryview`. It creates an editable
+view of the data without making a copy. The buffer's random access and support
+for slice notation are well-suited to in-place editing::
+
+ import io
+
+ REC_LEN, LOC_START, LOC_LEN = 34, 7, 11
+
+ def change_location(buffer, record_number, location):
+ start = record_number * REC_LEN + LOC_START
+ buffer[start: start+LOC_LEN] = location
+
+ >>> byte_stream = io.BytesIO(
+ b'G3805 storeroom Main chassis '
+ b'X7899 shipping Reserve cog '
+ b'L6988 receiving Primary sprocket'
+ )
+ >>> buffer = byte_stream.getbuffer()
+ >>> change_location(buffer, 1, b'warehouse ')
+ >>> change_location(buffer, 0, b'showroom ')
+ >>> print(byte_stream.getvalue())
+ b'G3805 showroom Main chassis ' ->
+ b'X7899 warehouse Reserve cog ' ->
+ b'L6988 receiving Primary sprocket'
+
reprlib
-------