gh-129813, PEP 782: Use Py_GetConstant(Py_CONSTANT_EMPTY_BYTES) (#138830)

Replace PyBytes_FromStringAndSize(NULL, 0) with
Py_GetConstant(Py_CONSTANT_EMPTY_BYTES). Py_GetConstant() cannot
fail.
This commit is contained in:
Victor Stinner 2025-09-13 18:30:25 +02:00 committed by GitHub
parent 8fc6ae68b0
commit 06b7891f12
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 11 additions and 23 deletions

View file

@ -4913,7 +4913,7 @@ _PyUnicode_EncodeUTF7(PyObject *str,
len = PyUnicode_GET_LENGTH(str);
if (len == 0)
return PyBytes_FromStringAndSize(NULL, 0);
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
/* It might be possible to tighten this worst case */
if (len > PY_SSIZE_T_MAX / 8)
@ -6914,7 +6914,7 @@ PyUnicode_AsUnicodeEscapeString(PyObject *unicode)
len = PyUnicode_GET_LENGTH(unicode);
if (len == 0) {
return PyBytes_FromStringAndSize(NULL, 0);
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
}
kind = PyUnicode_KIND(unicode);
@ -7364,7 +7364,7 @@ unicode_encode_ucs1(PyObject *unicode,
/* allocate enough for a simple encoding without
replacements, if we need more, we'll resize */
if (size == 0)
return PyBytes_FromStringAndSize(NULL, 0);
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
_PyBytesWriter_Init(&writer);
str = _PyBytesWriter_Alloc(&writer, size);
@ -8305,7 +8305,7 @@ encode_code_page(int code_page,
}
if (len == 0)
return PyBytes_FromStringAndSize(NULL, 0);
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
offset = 0;
do