Check object is bytes

This commit is contained in:
Andrew Murray 2024-08-16 18:31:07 +10:00
parent 0a03b77daf
commit 5cb79c6a69
2 changed files with 11 additions and 2 deletions

View File

@ -1113,6 +1113,9 @@ def test_bytes(font: ImageFont.FreeTypeFont) -> None:
) )
assert font.getmask2(b"test")[1] == font.getmask2("test")[1] 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( @pytest.mark.parametrize(
"test_file", "test_file",

View File

@ -272,7 +272,7 @@ text_layout_raqm(
} }
set_text = raqm_set_text(rq, text, size); set_text = raqm_set_text(rq, text, size);
PyMem_Free(text); PyMem_Free(text);
} else { } else if (PyBytes_Check(string)) {
char *buffer; char *buffer;
PyBytes_AsStringAndSize(string, &buffer, &size); PyBytes_AsStringAndSize(string, &buffer, &size);
if (!buffer || !size) { if (!buffer || !size) {
@ -281,6 +281,9 @@ text_layout_raqm(
goto failed; goto failed;
} }
set_text = raqm_set_text_utf8(rq, buffer, size); set_text = raqm_set_text_utf8(rq, buffer, size);
} else {
PyErr_SetString(PyExc_TypeError, "expected string or bytes");
goto failed;
} }
if (!set_text) { if (!set_text) {
PyErr_SetString(PyExc_ValueError, "raqm_set_text() failed"); PyErr_SetString(PyExc_ValueError, "raqm_set_text() failed");
@ -425,8 +428,11 @@ text_layout_fallback(
if (PyUnicode_Check(string)) { if (PyUnicode_Check(string)) {
count = PyUnicode_GET_LENGTH(string); count = PyUnicode_GET_LENGTH(string);
} else { } else if (PyBytes_Check(string)) {
PyBytes_AsStringAndSize(string, &buffer, &count); PyBytes_AsStringAndSize(string, &buffer, &count);
} else {
PyErr_SetString(PyExc_TypeError, "expected string or bytes");
return 0;
} }
if (count == 0) { if (count == 0) {
return 0; return 0;