pep3118

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

怎甘沉沦 提交于 2020-01-03 09:48:19
问题 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 memoryview, I get a failure. For example, in python (I use version 2.7): >>> a = array.array('c') >>> memoryview(a) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: cannot make memory view because object does not have the

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

女生的网名这么多〃 提交于 2020-01-03 09:48:12
问题 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 memoryview, I get a failure. For example, in python (I use version 2.7): >>> a = array.array('c') >>> memoryview(a) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: cannot make memory view because object does not have the

Assignment into Python 3.x Buffers with itemsize > 1

僤鯓⒐⒋嵵緔 提交于 2019-12-24 02:23:08
问题 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 flags) { int img_len; void* img_bytes; // Do my image fetch magic get_image_pixel_data(self, &img_bytes, &img_len); // Let python fill my buffer PyBuffer_FillInfo(view, self, img_bytes, img_len, 0, flags); } And in python I can play with it like so:

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

空扰寡人 提交于 2019-12-23 15:55:04
问题 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 like this: struct Result_buffer{ char** data_pointer; int length = 0; Result_buffer( int row_capacity) { data_pointer; = new char*[row_capacity]; return arr; } // the actual data is appended row by row void append_row(char* row_data) { data_pointer

Using Py_buffer and PyMemoryView_FromBuffer with different itemsizes

一笑奈何 提交于 2019-12-23 11:00:40
问题 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 PyBuffer_FillInfo (work = I can manipulate the data in Python and write it to stdout in C), but if I try to roll my own buffer I get a segfault after the C function returns. I need to create my own buffer because PyBuffer_FillInfo assumes that the format is

PEP 3118 warning when using ctypes array as numpy array

ⅰ亾dé卋堺 提交于 2019-12-08 23:42:25
问题 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 information. >>> import ctypes, numpy >>> TenByteBuffer = ctypes.c_ubyte * 10 >>> a = TenByteBuffer() >>> b = numpy.ctypeslib.as_array(a) C:\Python27\lib\site-packages\numpy\ctypeslib.py:402: RuntimeWarning: Item size computed from the PEP 3118 buffer format

PEP 3118 warning when using ctypes array as numpy array

岁酱吖の 提交于 2019-11-30 12:45:17
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 information. >>> import ctypes, numpy >>> TenByteBuffer = ctypes.c_ubyte * 10 >>> a = TenByteBuffer() >>> b = numpy.ctypeslib.as_array(a) C:\Python27\lib\site-packages\numpy\ctypeslib.py:402: RuntimeWarning: Item size computed from the PEP 3118 buffer format string does not match the actual item s ize. return array(obj, copy=False) >>> b array([0, 0, 0, 0, 0, 0