Skip to content

Commit

Permalink
Check object is bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Aug 16, 2024
1 parent 0a03b77 commit 5cb79c6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Tests/test_imagefont.py
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,9 @@ def test_bytes(font: ImageFont.FreeTypeFont) -> None:
)
assert font.getmask2(b"test")[1] == font.getmask2("test")[1]

with pytest.raises(TypeError):
font.getlength((0, 0)) # type: ignore[arg-type]


@pytest.mark.parametrize(
"test_file",
Expand Down
10 changes: 8 additions & 2 deletions src/_imagingft.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ text_layout_raqm(
}
set_text = raqm_set_text(rq, text, size);
PyMem_Free(text);
} else {
} else if (PyBytes_Check(string)) {
char *buffer;
PyBytes_AsStringAndSize(string, &buffer, &size);
if (!buffer || !size) {
Expand All @@ -281,6 +281,9 @@ text_layout_raqm(
goto failed;
}
set_text = raqm_set_text_utf8(rq, buffer, size);
} else {
PyErr_SetString(PyExc_TypeError, "expected string or bytes");
goto failed;
}
if (!set_text) {
PyErr_SetString(PyExc_ValueError, "raqm_set_text() failed");
Expand Down Expand Up @@ -425,8 +428,11 @@ text_layout_fallback(

if (PyUnicode_Check(string)) {
count = PyUnicode_GET_LENGTH(string);
} else {
} else if (PyBytes_Check(string)) {
PyBytes_AsStringAndSize(string, &buffer, &count);
} else {
PyErr_SetString(PyExc_TypeError, "expected string or bytes");
return 0;
}
if (count == 0) {
return 0;
Expand Down

0 comments on commit 5cb79c6

Please sign in to comment.