Merge pull request #2788 from wiredfool/issue_2783

Fix for return without none set
This commit is contained in:
Hugo 2017-10-04 15:53:15 +03:00 committed by GitHub
commit 8a4a1e0338
3 changed files with 33 additions and 19 deletions

View File

@ -380,7 +380,6 @@ class TestImageDraw(PillowTestCase):
self.assert_image_equal( self.assert_image_equal(
im, Image.open("Tests/images/imagedraw_floodfill2.png")) im, Image.open("Tests/images/imagedraw_floodfill2.png"))
def test_floodfill_thresh(self): def test_floodfill_thresh(self):
# floodfill() is experimental # floodfill() is experimental
@ -560,6 +559,19 @@ class TestImageDraw(PillowTestCase):
# Assert # Assert
self.assert_image_similar(im, Image.open(expected), 1) 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__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@ -2212,16 +2212,15 @@ void _font_text_asBytes(PyObject* encoded_string, unsigned char** text){
PyBytes_AsStringAndSize(encoded_string, &buffer, &len); PyBytes_AsStringAndSize(encoded_string, &buffer, &len);
} }
if (len) { *text = calloc(len,1);
*text = calloc(len,1); if (*text) {
if (*text) { memcpy(*text, buffer, len);
memcpy(*text, buffer, len);
}
if(bytes) {
Py_DECREF(bytes);
}
return;
} }
if(bytes) {
Py_DECREF(bytes);
}
return;
#if PY_VERSION_HEX < 0x03000000 #if PY_VERSION_HEX < 0x03000000
@ -2261,12 +2260,14 @@ _font_getmask(ImagingFontObject* self, PyObject* args)
_font_text_asBytes(encoded_string, &text); _font_text_asBytes(encoded_string, &text);
if (!text) { if (!text) {
ImagingError_MemoryError();
return NULL; return NULL;
} }
im = ImagingNew(self->bitmap->mode, textwidth(self, text), self->ysize); im = ImagingNew(self->bitmap->mode, textwidth(self, text), self->ysize);
if (!im) { if (!im) {
free(text); free(text);
ImagingError_MemoryError();
return NULL; return NULL;
} }
@ -2298,7 +2299,7 @@ _font_getmask(ImagingFontObject* self, PyObject* args)
failed: failed:
free(text); free(text);
ImagingDelete(im); ImagingDelete(im);
return NULL; Py_RETURN_NONE;
} }
static PyObject* static PyObject*
@ -2313,6 +2314,7 @@ _font_getsize(ImagingFontObject* self, PyObject* args)
_font_text_asBytes(encoded_string, &text); _font_text_asBytes(encoded_string, &text);
if (!text) { if (!text) {
ImagingError_MemoryError();
return NULL; return NULL;
} }

View File

@ -225,11 +225,11 @@ text_layout_raqm(PyObject* string, FontObject* self, const char* dir,
if (PyUnicode_Check(string)) { if (PyUnicode_Check(string)) {
Py_UNICODE *text = PyUnicode_AS_UNICODE(string); Py_UNICODE *text = PyUnicode_AS_UNICODE(string);
Py_ssize_t size = PyUnicode_GET_SIZE(string); Py_ssize_t size = PyUnicode_GET_SIZE(string);
if (! size) { if (! size) {
/* return 0 and clean up, no glyphs==no size, /* return 0 and clean up, no glyphs==no size,
and raqm fails with empty strings */ and raqm fails with empty strings */
goto failed; goto failed;
} }
if (!raqm_set_text(rq, (const uint32_t *)(text), size)) { if (!raqm_set_text(rq, (const uint32_t *)(text), size)) {
PyErr_SetString(PyExc_ValueError, "raqm_set_text() failed"); PyErr_SetString(PyExc_ValueError, "raqm_set_text() failed");
goto failed; goto failed;
@ -239,9 +239,9 @@ text_layout_raqm(PyObject* string, FontObject* self, const char* dir,
else if (PyString_Check(string)) { else if (PyString_Check(string)) {
char *text = PyString_AS_STRING(string); char *text = PyString_AS_STRING(string);
int size = PyString_GET_SIZE(string); int size = PyString_GET_SIZE(string);
if (! size) { if (! size) {
goto failed; goto failed;
} }
if (!raqm_set_text_utf8(rq, text, size)) { if (!raqm_set_text_utf8(rq, text, size)) {
PyErr_SetString(PyExc_ValueError, "raqm_set_text_utf8() failed"); PyErr_SetString(PyExc_ValueError, "raqm_set_text_utf8() failed");
goto failed; goto failed;