Merge pull request #6988 from radarhere/memoryview

This commit is contained in:
Hugo van Kemenade 2023-03-11 22:42:09 +02:00 committed by GitHub
commit bfcea9d718
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 9 deletions

View File

@ -100,6 +100,9 @@ class TestImageWinDib:
# Act
# Make one the same as the using tobytes()/frombytes()
test_buffer = dib1.tobytes()
for datatype in ("bytes", "memoryview"):
if datatype == "memoryview":
test_buffer = memoryview(test_buffer)
dib2.frombytes(test_buffer)
# Assert

View File

@ -195,20 +195,21 @@ _releasedc(ImagingDisplayObject *display, PyObject *args) {
static PyObject *
_frombytes(ImagingDisplayObject *display, PyObject *args) {
char *ptr;
Py_ssize_t bytes;
Py_buffer buffer;
if (!PyArg_ParseTuple(args, "y#:frombytes", &ptr, &bytes)) {
if (!PyArg_ParseTuple(args, "y*:frombytes", &buffer)) {
return NULL;
}
if (display->dib->ysize * display->dib->linesize != bytes) {
if (display->dib->ysize * display->dib->linesize != buffer.len) {
PyBuffer_Release(&buffer);
PyErr_SetString(PyExc_ValueError, "wrong size");
return NULL;
}
memcpy(display->dib->bits, ptr, bytes);
memcpy(display->dib->bits, buffer.buf, buffer.len);
PyBuffer_Release(&buffer);
Py_INCREF(Py_None);
return Py_None;
}