Added memoryview support to frombytes()

This commit is contained in:
Andrew Murray 2023-02-25 19:10:47 +11:00
parent dbcd7372e5
commit 132fb9360b
2 changed files with 12 additions and 6 deletions

View File

@ -1,10 +1,17 @@
import pytest
from PIL import Image
from .helper import assert_image_equal, hopper
def test_sanity():
@pytest.mark.parametrize("data_type", ("bytes", "memoryview"))
def test_sanity(data_type):
im1 = hopper()
im2 = Image.frombytes(im1.mode, im1.size, im1.tobytes())
data = im1.tobytes()
if data_type == "memoryview":
data = memoryview(data)
im2 = Image.frombytes(im1.mode, im1.size, data)
assert_image_equal(im1, im2)

View File

@ -116,12 +116,11 @@ _dealloc(ImagingDecoderObject *decoder) {
static PyObject *
_decode(ImagingDecoderObject *decoder, PyObject *args) {
UINT8 *buffer;
Py_ssize_t bufsize;
Py_buffer buffer;
int status;
ImagingSectionCookie cookie;
if (!PyArg_ParseTuple(args, "y#", &buffer, &bufsize)) {
if (!PyArg_ParseTuple(args, "y*", &buffer)) {
return NULL;
}
@ -129,7 +128,7 @@ _decode(ImagingDecoderObject *decoder, PyObject *args) {
ImagingSectionEnter(&cookie);
}
status = decoder->decode(decoder->im, &decoder->state, buffer, bufsize);
status = decoder->decode(decoder->im, &decoder->state, buffer.buf, buffer.len);
if (!decoder->pulls_fd) {
ImagingSectionLeave(&cookie);