Questions tagged [pybuffer]

Shorthand name for the Python buffer interface, so named for the `Py_buffer` struct used throughout.

The Python buffer interface comprises both the “Revised Buffer Protocol” – also known as – and the legacy buffer API.

The legacy API exposes a top-level builtin buffer type to Python programmers; a set of corresponding PyBuffer_* C functions are furnished to extension developers.

The revised protocol introduced the Py_buffer struct, with a new general low-level C-API that isn’t restricted to a specific Python type, to the extension interface. The high-level features of the old buffer objects are made available by the new memoryview Python type.

Depending on the context, “PyBuffer” can refer literally to the revised protocol’s Py_buffer struct, or (more often than not) the general notion of using buffers in Python.

13 questions
21
votes
1 answer

Using the buffer API in Cython

I'm working in with a C library that repeatedly calls a user supplied function pointer to get more data. I'd like to write a Cython wrapper in such a way that the Python implementation of that callback can return any reasonable data type like str,…
SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
9
votes
1 answer

Definition of PyBufferProcs in Python 2.7 when class implements PEP 3118

I am in the process of extending the classes in our library (which supports Python 2.7) to support PEP 3118, which has been back-ported to 2.7. From the documentation, I need to initialize the tp_as_buffer field to point to a PyBufferProcs. From…
James Kanze
  • 150,581
  • 18
  • 184
  • 329
5
votes
1 answer

Regarding Buffer interface in python

I am quite confused with the term 'buffer Interface' in python. what does it mean to say that " A python object exposing its underlying memory structure' can someone explain with an example. Thanks in advance
user634615
  • 647
  • 7
  • 17
5
votes
1 answer

Using Py_buffer and PyMemoryView_FromBuffer with different itemsizes

This question is related to a previous question I asked. Namely this one if anyone is interested. Basically, what I want to do is to expose a C array to Python using a Py_buffer wrapped in a memoryview-object. I've gotten it to work using…
manneorama
  • 1,291
  • 12
  • 22
3
votes
1 answer

Is PyBuffer_Release required after Py_BuildValue("y#", ...)?

If it makes a difference, I am interested in an answer regarding Python 3. The docs state (here and here) that PyBuffer_Release() should be called after PyArg_Parse*() with s*, y*. Nothing of the sort is written about Py_BuildValue(). Is it an…
Zoltan K.
  • 1,036
  • 9
  • 19
3
votes
1 answer

Writing to new Python buffer interface

I have implemented the new python buffer interface in C++ outlined here: https://docs.python.org/2/c-api/buffer.html I have implemented my Py_buffer struct and filled it in: template static int getbuffer(PyObject *obj, Py_buffer *view,…
marsh
  • 2,592
  • 5
  • 29
  • 53
2
votes
1 answer

Assignment into Python 3.x Buffers with itemsize > 1

I am trying to expose a buffer of image pixel information (32 bit RGBA) through the Python 3.x buffer interface. After quite a bit of playing around, I was able to get this working like so: int Image_get_buffer(PyObject* self, Py_buffer* view, int…
Toji
  • 33,927
  • 22
  • 105
  • 115
2
votes
2 answers

Access contents of PyBuffer from C

I have created a buffer object in python like so: f = io.open('some_file', 'rb') byte_stream = buffer(f.read(4096)) I'm now passing byte_stream as a parameter to a C function, through SWIG. I have a typemap for converting the data which looks like…
Pavel
  • 729
  • 2
  • 11
  • 22
2
votes
1 answer

What's the usage "multi-dimensional array" of Py_buffer in 2.x?

http://docs.python.org/2/c-api/buffer.html int ndim The number of dimensions the memory represents as a multi-dimensional array. If it is 0, strides and suboffsets must be NULL. What's the real world usage for this? Is it used for scatter gather…
est
  • 11,429
  • 14
  • 70
  • 118
1
vote
1 answer

Loading binary data from a file into a buffer in Python

I have a set of 640x480 images. I’m converting these images to a binary file format via Matlab… Now, I need to load each of the binary files into a buffer in Python, and then read data from that buffer. Can anyone help me out with how I might do…
user3397145
  • 787
  • 1
  • 5
  • 16
1
vote
1 answer

Creating a PyBuffer from a C struct

EDIT: Upon re-reading my original question I realized very quickly that it was very poorly worded, ambiguous, and too confusing to ever get a decent answer. That's what I get for rushing out a question at the end of my lunch break. Hopefully this…
Toji
  • 33,927
  • 22
  • 105
  • 115
0
votes
0 answers

Creating a Py_buffer from an array in c++

I am trying to extend my python code by creating a c++ module. I am able to import my c++ code by calling 'import my_code' in python with no problem. My issue occurs when I try to return the PyObject. I want to take a vector *settled_nodes_vector…
0
votes
1 answer

PyBuffer_New: Do I need to free manually?

I am searching for a memory leak in code of someone else. I found: def current(self): ... data = PyBuffer_New(buflen) PyObject_AsCharBuffer(data, &data_ptr, &buflen) ... return VideoFrame(data, self.frame_size, self.frame_mode, …
P.R.
  • 3,785
  • 1
  • 27
  • 47