Questions tagged [pep3118]

PEP 3118 is the Revised Buffer Protocol for the [tag:python] C-API.

PEP 3118 is the Revised Buffer Protocol for the C-API. Supported natively in Python 3 and backported to the 2.x series starting at Python 2.6, PEP 3118 specifies the use of the Py_buffer structure, and the variety of API functions provided for managing both the Py_buffer lifecycle (at the C-API level) and the new memoryview objects (at the Python level).

17 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
13
votes
2 answers

PEP 3118 warning when using ctypes array as numpy array

I'm getting the following warning message when I try to use a ctypes array as a numpy array: Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more…
Andrew
  • 2,842
  • 5
  • 31
  • 49
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
7
votes
0 answers

Parsing PEP 3118 buffer protocol format strings

I am interested in passing binary data between python, numpy, and cython using the buffer protocol. Looking at PEP 3118, there appear to be some additions to the struct string-syntax that add support for useful features such as named fields and…
CodeSurgeon
  • 2,435
  • 2
  • 15
  • 36
6
votes
2 answers

Why is it not possible to get a Py_buffer from an array object?

The python documentation on array clearly states that the array conforms to the buffer interface. It even suggest not using the buffer_info() method. But when I try to get a Py_Buffer from C/C++ code with PyObject_GetBuffer() or use python's…
David
  • 9,635
  • 5
  • 62
  • 68
5
votes
1 answer

Cython - Memoryview of a dynamic 2D C++Array

The Goal: Get a Memoryview from a 2D C++ char array using Cython. A little background: I have a native C++ library which generates some data and returns it via a char** to the Cython world. The array is initialized and operated in the library about…
brain4711
  • 123
  • 9
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

Python C-API: Populate a Py_buffer from a C function

I am having a really hard time figuring out how to pass a large byte array of data from C to an arbitrary Python3 function. For reference, here are some documentation links on the subject: https://docs.python.org/3.5/extending/embedding.html Most of…
sean
  • 61
  • 7
3
votes
1 answer

PIL Image constructs weird images from numpy array - why?

I want a method to generate small RGB square images, of either red, green or blue colour. It should produce solid blocks of colour, but the image output from PIL is very strange. Why? import numpy as np from PIL import Image class MakeSquares(): …
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
3
votes
2 answers

New style python buffer protocol and numpy arrays

I'm trying to write a fast non copy interface for my python binding of a commercial image processing library. I implemented the new-style buffer api protocol which looks ok according to memoryview(): import hirsch as H import numpy as np w,h =…
Dov Grobgeld
  • 4,783
  • 1
  • 25
  • 36
2
votes
3 answers

unsigned char* image to Python

I was able to generate python bindings for a camera library using SWIG and I am able to capture and save image using the library's inbuilt functions. I am trying to obtain data from the camera into Python Image Library format, the library provides…
Chenna V
  • 10,185
  • 11
  • 77
  • 104
2
votes
1 answer

Converting a PIL image to skimage?

I have an image loaded in my code (very long and unnecessary, won't post here) that I need to work on with skimage to detect blobs in the image. However for some reason all of the images and attempts that I have used don't work. The image is…
Matthew Winfield
  • 827
  • 4
  • 10
  • 25
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
1
2