Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2a028c0

Browse files
committed
Merge pull request #4163 from mdboom/gtkcairo-imshow
Fix: Return a writable buffer from conv_color
2 parents 42a852d + b3b0c82 commit 2a028c0

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/_image_wrapper.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,22 @@ static PyObject *PyImage_color_conv(PyImage *self, PyObject *args, PyObject *kwd
165165
return NULL;
166166
}
167167

168-
PyObject *result = PyBytes_FromStringAndSize(NULL, self->x->rowsOut * self->x->colsOut * 4);
169-
if (result == NULL) {
168+
Py_ssize_t size = self->x->rowsOut * self->x->colsOut * 4;
169+
agg::int8u *buff = (agg::int8u *)malloc(size);
170+
if (buff == NULL) {
171+
PyErr_SetString(PyExc_MemoryError, "Out of memory");
170172
return NULL;
171173
}
172174

173175
CALL_CPP_CLEANUP("color_conv",
174-
(self->x->color_conv(format, (agg::int8u *)PyBytes_AsString(result))),
175-
Py_DECREF(result));
176+
(self->x->color_conv(format, buff)),
177+
free(buff));
178+
179+
PyObject *result = PyByteArray_FromStringAndSize((const char *)buff, size);
180+
if (result == NULL) {
181+
free(buff);
182+
return NULL;
183+
}
176184

177185
return Py_BuildValue("nnN", self->x->rowsOut, self->x->colsOut, result);
178186
}

0 commit comments

Comments
 (0)