gh-122361: Use proper `PyUnicodeWriter_*` API in `constevaluator_call` (#122362)

This commit is contained in:
sobolevn 2024-07-27 21:33:38 +03:00 committed by GitHub
parent ae192262ad
commit 04eb5c8db1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 17 additions and 15 deletions

View File

@ -169,38 +169,40 @@ constevaluator_call(PyObject *self, PyObject *args, PyObject *kwargs)
} }
PyObject *value = ((constevaluatorobject *)self)->value; PyObject *value = ((constevaluatorobject *)self)->value;
if (format == 3) { // SOURCE if (format == 3) { // SOURCE
_PyUnicodeWriter writer; PyUnicodeWriter *writer = PyUnicodeWriter_Create(5); // cannot be <5
_PyUnicodeWriter_Init(&writer); if (writer == NULL) {
return NULL;
}
if (PyTuple_Check(value)) { if (PyTuple_Check(value)) {
if (_PyUnicodeWriter_WriteASCIIString(&writer, "(", 1) < 0) { if (PyUnicodeWriter_WriteChar(writer, '(') < 0) {
_PyUnicodeWriter_Dealloc(&writer); PyUnicodeWriter_Discard(writer);
return NULL; return NULL;
} }
for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(value); i++) { for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(value); i++) {
PyObject *item = PyTuple_GET_ITEM(value, i); PyObject *item = PyTuple_GET_ITEM(value, i);
if (i > 0) { if (i > 0) {
if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0) { if (PyUnicodeWriter_WriteUTF8(writer, ", ", 2) < 0) {
_PyUnicodeWriter_Dealloc(&writer); PyUnicodeWriter_Discard(writer);
return NULL; return NULL;
} }
} }
if (_Py_typing_type_repr(&writer, item) < 0) { if (_Py_typing_type_repr(writer, item) < 0) {
_PyUnicodeWriter_Dealloc(&writer); PyUnicodeWriter_Discard(writer);
return NULL; return NULL;
} }
} }
if (_PyUnicodeWriter_WriteASCIIString(&writer, ")", 1) < 0) { if (PyUnicodeWriter_WriteChar(writer, ')') < 0) {
_PyUnicodeWriter_Dealloc(&writer); PyUnicodeWriter_Discard(writer);
return NULL; return NULL;
} }
} }
else { else {
if (_Py_typing_type_repr(&writer, value) < 0) { if (_Py_typing_type_repr(writer, value) < 0) {
_PyUnicodeWriter_Dealloc(&writer); PyUnicodeWriter_Discard(writer);
return NULL; return NULL;
} }
} }
return _PyUnicodeWriter_Finish(&writer); return PyUnicodeWriter_Finish(writer);
} }
return Py_NewRef(value); return Py_NewRef(value);
} }
@ -259,7 +261,7 @@ _Py_typing_type_repr(PyUnicodeWriter *writer, PyObject *p)
} }
if (p == (PyObject *)&_PyNone_Type) { if (p == (PyObject *)&_PyNone_Type) {
return _PyUnicodeWriter_WriteASCIIString(writer, "None", 4); return PyUnicodeWriter_WriteUTF8(writer, "None", 4);
} }
if ((rc = PyObject_HasAttrWithError(p, &_Py_ID(__origin__))) > 0 && if ((rc = PyObject_HasAttrWithError(p, &_Py_ID(__origin__))) > 0 &&
@ -306,7 +308,7 @@ exit:
if (r == NULL) { if (r == NULL) {
return -1; return -1;
} }
rc = _PyUnicodeWriter_WriteStr(writer, r); rc = PyUnicodeWriter_WriteStr(writer, r);
Py_DECREF(r); Py_DECREF(r);
return rc; return rc;
} }