Skip to content

Commit

Permalink
Merge pull request python-pillow#2788 from wiredfool/issue_2783
Browse files Browse the repository at this point in the history
Fix for return without none set
  • Loading branch information
hugovk committed Oct 4, 2017
2 parents 248d56f + 5524d80 commit 8a4a1e0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
14 changes: 13 additions & 1 deletion Tests/test_imagedraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,6 @@ def test_floodfill_border(self):
self.assert_image_equal(
im, Image.open("Tests/images/imagedraw_floodfill2.png"))


def test_floodfill_thresh(self):
# floodfill() is experimental

Expand Down Expand Up @@ -560,6 +559,19 @@ def test_wide_line_dot(self):
# Assert
self.assert_image_similar(im, Image.open(expected), 1)

def test_textsize_empty_string(self):
# https://github.com/python-pillow/Pillow/issues/2783
# Arrange
im = Image.new("RGB", (W, H))
draw = ImageDraw.Draw(im)

# Act
# Should not cause 'SystemError: <built-in method getsize of
# ImagingFont object at 0x...> returned NULL without setting an error'
draw.textsize("")
draw.textsize("\n")
draw.textsize("test\n")


if __name__ == '__main__':
unittest.main()
22 changes: 12 additions & 10 deletions _imaging.c
Original file line number Diff line number Diff line change
Expand Up @@ -2212,16 +2212,15 @@ void _font_text_asBytes(PyObject* encoded_string, unsigned char** text){
PyBytes_AsStringAndSize(encoded_string, &buffer, &len);
}

if (len) {
*text = calloc(len,1);
if (*text) {
memcpy(*text, buffer, len);
}
if(bytes) {
Py_DECREF(bytes);
}
return;
*text = calloc(len,1);
if (*text) {
memcpy(*text, buffer, len);
}
if(bytes) {
Py_DECREF(bytes);
}

return;


#if PY_VERSION_HEX < 0x03000000
Expand Down Expand Up @@ -2261,12 +2260,14 @@ _font_getmask(ImagingFontObject* self, PyObject* args)

_font_text_asBytes(encoded_string, &text);
if (!text) {
ImagingError_MemoryError();
return NULL;
}

im = ImagingNew(self->bitmap->mode, textwidth(self, text), self->ysize);
if (!im) {
free(text);
ImagingError_MemoryError();
return NULL;
}

Expand Down Expand Up @@ -2298,7 +2299,7 @@ _font_getmask(ImagingFontObject* self, PyObject* args)
failed:
free(text);
ImagingDelete(im);
return NULL;
Py_RETURN_NONE;
}

static PyObject*
Expand All @@ -2313,6 +2314,7 @@ _font_getsize(ImagingFontObject* self, PyObject* args)

_font_text_asBytes(encoded_string, &text);
if (!text) {
ImagingError_MemoryError();
return NULL;
}

Expand Down
16 changes: 8 additions & 8 deletions _imagingft.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,11 @@ text_layout_raqm(PyObject* string, FontObject* self, const char* dir,
if (PyUnicode_Check(string)) {
Py_UNICODE *text = PyUnicode_AS_UNICODE(string);
Py_ssize_t size = PyUnicode_GET_SIZE(string);
if (! size) {
/* return 0 and clean up, no glyphs==no size,
and raqm fails with empty strings */
goto failed;
}
if (! size) {
/* return 0 and clean up, no glyphs==no size,
and raqm fails with empty strings */
goto failed;
}
if (!raqm_set_text(rq, (const uint32_t *)(text), size)) {
PyErr_SetString(PyExc_ValueError, "raqm_set_text() failed");
goto failed;
Expand All @@ -239,9 +239,9 @@ text_layout_raqm(PyObject* string, FontObject* self, const char* dir,
else if (PyString_Check(string)) {
char *text = PyString_AS_STRING(string);
int size = PyString_GET_SIZE(string);
if (! size) {
goto failed;
}
if (! size) {
goto failed;
}
if (!raqm_set_text_utf8(rq, text, size)) {
PyErr_SetString(PyExc_ValueError, "raqm_set_text_utf8() failed");
goto failed;
Expand Down

0 comments on commit 8a4a1e0

Please sign in to comment.