🌐
Python Reference
python-reference.readthedocs.io › en › latest › docs › functions › buffer.html
buffer — Python Reference (The Right Way) 0.1 documentation
>>> b = buffer('hello') >>> b <read-only buffer for 0x00EE38A0, size -1, offset 0 at 0x00EE3020> >>> print b hello >>> b = buffer('hello', 1) >>> print b ello >>> b = buffer('hello', 1, 3) >>> print b ell
🌐
Python
docs.python.org › 3 › c-api › buffer.html
Buffer Protocol — Python 3.14.8 documentation
Release the buffer view and release the strong reference (i.e. decrement the reference count) to the view’s supporting object, view->obj. This function MUST be called when the buffer is no longer being used, otherwise reference leaks may occur.
Discussions

What is Buffer?
It is really context dependent word. In post-USSR countries it even means tits in slang :) But regarding programming - buffer usually means some memory, where you store values to access them next. Classical use-case - buffered I/O operations. Write to disk is slow in comparing to write to memory, if program will be writing to disk byte by byte it will be running too long, so it write to special buffer in memory, from where it will be pick-up by another thread or OS routine and written to disk. It is transparent for user: he press save button, and program continue to be responsible, he can continue to do whatever he need. But if program will be interrupted before buffers really be save to HDD/SSD - file could be corrupted. More on reddit.com
🌐 r/learnpython
5
9
June 16, 2021
Buffer function for python 3+ - Stack Overflow
I'm getting an error while trying to use the buffer built-in function of python2, but as this function doesn't exist on python3+ i'm stuck.. If anyone could help me with this i would be very appreciated. More on stackoverflow.com
🌐 stackoverflow.com
What is the Python 'buffer' type for? - Stack Overflow
For memory usage see stackoverflow.com/questions/110259 for example. Sometimes it's easiest just to watch Python's usage in Task Manager/Activity Monitor/top. 2010-08-06T17:15:11.837Z+00:00 ... Save this answer. ... Show activity on this post. I think buffers are e.g. More on stackoverflow.com
🌐 stackoverflow.com
Some data getting lost when using PySerial's read() function
in_waiting gives the number of bytes in the input buffer. The reason the number starts high is probably that the MCU probably starts sending before you start reading, so it's starting to fill up? This could also be related to why you sometimes get partial packets, you might be filling up the buffer, and it's not a multiple of 76 bytes, so the last message added to the buffer is truncated. More on reddit.com
🌐 r/learnpython
5
2
April 19, 2024
🌐
Python Data Science Handbook
jakevdp.github.io › blog › 2014 › 05 › 05 › introduction-to-the-python-buffer-protocol
An Introduction to the Python Buffer Protocol | Pythonic Perambulations
Because the buffer protocol is fundamentally a C-API entity, implementing new functionality with it generally requires digging into the guts of Python's C API. For example, the code that enables the above numpy example is found in NumPy's buffer.c file, and reading through it is not particularly enlightening.
🌐
Python
docs.python.org › 2.0 › api › bufferObjects.html
7.2.3 Buffer Objects
The string object exposes the character contents in the buffer interface's byte-oriented form. An array can also expose its contents, but it should be noted that array elements may be multi-byte values. An example user of the buffer interface is the file object's write() method.
🌐
Delft Stack
delftstack.com › home › howto › python › buffer interface in python
Buffer in Python | Delft Stack
March 11, 2025 - This interface defines a set of methods that allow you to access the raw bytes of an object, making it easier to work with binary data. By utilizing the buffer interface, you can read and write data more efficiently, leveraging Python’s capabilities to handle low-level operations seamlessly.
🌐
Reddit
reddit.com › r/learnpython › what is buffer?
r/learnpython on Reddit: What is Buffer?
June 16, 2021 -

How can you explain to the beginner what is buffer? I have been searching about the buffer but its so deep for me to analyze their wordings.

Top answer
1 of 5
11
It is really context dependent word. In post-USSR countries it even means tits in slang :) But regarding programming - buffer usually means some memory, where you store values to access them next. Classical use-case - buffered I/O operations. Write to disk is slow in comparing to write to memory, if program will be writing to disk byte by byte it will be running too long, so it write to special buffer in memory, from where it will be pick-up by another thread or OS routine and written to disk. It is transparent for user: he press save button, and program continue to be responsible, he can continue to do whatever he need. But if program will be interrupted before buffers really be save to HDD/SSD - file could be corrupted.
2 of 5
4
You: hey I need some memory for a value I'm trying to store, can I get idk 20 bytes? Operating system: you can have 20 bytes, here's where they are insert address here You: great! Here's 20 characters worth of text OS: thanks I'll hold onto that for you. You: actually I need a few more bytes... Got any space? OS: well sure but I'll have to make a new address and get rid of the first one You: how about I just put 30 bytes in the original address? OS: nah fam it'll eat into someone else's space. You: well alright I'll just copy my data OS: cool, here's your new address Long story short, it's a section in your computer's memory set aside for some kind of data. The type of data isn't important, just has to be enough space. Python handles most of this already
🌐
Rose-Hulman Institute of Technology
rose-hulman.edu › class › cs › archive › csse120-old › csse120-old-terms › 201220 › Resources › python-3.1.2-docs-html › c-api › buffer.html
Buffer Objects — Python v3.1.2 documentation
September 28, 2010 - Python objects implemented in C ... data directly, without needing to copy it first. Two examples of objects that support the buffer interface are bytes and arrays....
🌐
Python
peps.python.org › pep-0688
PEP 688 – Making the buffer protocol accessible in Python | peps.python.org
April 23, 2022 - Functions that accept binary data are usually written to handle any object implementing the buffer protocol. For example, at the time of writing, there are around 130 functions in CPython using the Argument Clinic Py_buffer type, which accepts the buffer protocol.
Find elsewhere
🌐
Medium
medium.com › @AlexanderObregon › how-pythons-memoryview-and-buffers-work-23c489fbbabb
How Python’s Memoryview and Buffers Work | Medium
August 11, 2024 - In this example, the invert_buffer function inverts the byte values in the bytearray, demonstrating how the buffer protocol can be used for efficient data manipulation in C extensions.
🌐
Python
docs.python.org › 3.11 › c-api › buffer.html
Buffer Protocol — Python 3.11.15 documentation
Release the buffer view and release the strong reference (i.e. decrement the reference count) to the view’s supporting object, view->obj. This function MUST be called when the buffer is no longer being used, otherwise reference leaks may occur.
🌐
Medium
medium.com › @bramblexu › understand-the-buffer-policy-in-python-78e91e7759ca
Understand the Buffer Policy in Python | by Xu LIANG | Medium
May 31, 2020 - For example, considering we download a 500 MB file, if we write 1MB once a time to hard disk, we have to write 500 times. But if we write 50MB once a time, we only write 50 times to hard disk.
Top answer
1 of 2
13

At least these two points must be modified on your code to have the same behavior with Python 3:

  • The buffer(...) built-in function in Python 2 has been replaced by memoryview(...) in Python 3: What is Python buffer type for?. Replace the buffer call by memoryview
  • the str(...) built-in function has to replaced by a bytes(...) call to get a bytes object: https://docs.python.org/2/howto/pyporting.html#text-versus-binary-data

So the data = ... line should read:

data = bytes(memoryview(writer.GetResult()))
2 of 2
1

To clarify, I believe this example was an adaptation of a very informative blog example showing how to extract surfaces from medical images using VTK's marching cubes algorithm. The accompanying Jupyter notebook was intended for Python 2.7, and as mentioned for it to be used in Python 3.6+, the data=... portion needs to be changed.

import vtk
from IPython.display import Image

def vtk_show(renderer, width=400, height=300):
    """
    Takes vtkRenderer instance and returns an IPython Image with the 
    rendering.
    """
    renderWindow = vtk.vtkRenderWindow()
    renderWindow.SetOffScreenRendering(1)
    renderWindow.AddRenderer(renderer)
    renderWindow.SetSize(width, height)
    renderWindow.Render()

    windowToImageFilter = vtk.vtkWindowToImageFilter()
    windowToImageFilter.SetInput(renderWindow)
    windowToImageFilter.Update()

    writer = vtk.vtkPNGWriter()
    writer.SetWriteToMemory(1)
    writer.SetInputConnection(windowToImageFilter.GetOutputPort())
    writer.Write()
    data = memoryview(writer.GetResults()).tobytes()

    return Image(data)

Credit for the solution definitely goes to @MafiaSkafia and @jcgiret, but I wanted to post a full and final solution.

🌐
Eli Bendersky
eli.thegreenplace.net › 2011 › 11 › 28 › less-copies-in-python-with-the-buffer-protocol-and-memoryviews
Less copies in Python with the buffer protocol and memoryviews - Eli Bendersky's website
November 28, 2011 - Use the PyArg_ParseTuple function with the w* format specifier to parse its argument as a R/W buffer object, which itself calls PyObject_GetBuffer - a Python API that invokes the producer's "get buffer" function.
🌐
Python
docs.python.org › 3.14 › c-api › buffer.html
Buffer Protocol — Python 3.14.7 documentation
Release the buffer view and release the strong reference (i.e. decrement the reference count) to the view’s supporting object, view->obj. This function MUST be called when the buffer is no longer being used, otherwise reference leaks may occur.
🌐
Python
docs.python.org › 3.10 › c-api › buffer.html
Buffer Protocol — Python 3.10.20 documentation
Handle buffer requests for an exporter that wants to expose buf of size len with writability set according to readonly. buf is interpreted as a sequence of unsigned bytes. The flags argument indicates the request type. This function always fills in view as specified by flags, unless buf has ...
🌐
Python
docs.python.org › 3.0 › c-api › buffer.html
Buffer Objects — Python v3.0.1 documentation
February 14, 2009 - The bytes object exposes the character contents in the buffer interface’s byte-oriented form. An array can also expose its contents, but it should be noted that array elements may be multi-byte values. An example user of the buffer interface is the file object’s write() method.
🌐
Python
docs.python.org › 3.6 › c-api › buffer.html
Buffer Protocol — Python 3.6.15 documentation
This function fails if len != src->len. void PyBuffer_FillContiguousStrides(int ndims, Py_ssize_t *shape, Py_ssize_t *strides, int itemsize, char order)¶ · Fill the strides array with byte-strides of a contiguous (C-style if order is 'C' or Fortran-style if order is 'F') array of the given shape with the given number of bytes per element. int PyBuffer_FillInfo(Py_buffer *view, PyObject *exporter, void *buf, Py_ssize_t len, int readonly, int flags)¶
🌐
Python
docs.python.org › 3.8 › c-api › buffer.html
Buffer Protocol — Python 3.8.20 documentation
Return 1 if the memory defined by the view is C-style (order is 'C') or Fortran-style (order is 'F') contiguous or either one (order is 'A'). Return 0 otherwise. This function always succeeds. void* PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices)