Merge pull request #8308 from radarhere/bytes

This commit is contained in:
Hugo van Kemenade 2024-09-04 14:52:57 +03:00 committed by GitHub
commit 06a9fcbff4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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;