Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jul 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions Objects/typevarobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,38 +169,40 @@ constevaluator_call(PyObject *self, PyObject *args, PyObject *kwargs)
}
PyObject *value = ((constevaluatorobject *)self)->value;
if (format == 3) { // SOURCE
_PyUnicodeWriter writer;
_PyUnicodeWriter_Init(&writer);
PyUnicodeWriter *writer = PyUnicodeWriter_Create(5); // cannot be <5
if (writer == NULL) {
return NULL;
}
if (PyTuple_Check(value)) {
if (_PyUnicodeWriter_WriteASCIIString(&writer, "(", 1) < 0) {
_PyUnicodeWriter_Dealloc(&writer);
if (PyUnicodeWriter_WriteChar(writer, '(') < 0) {
PyUnicodeWriter_Discard(writer);
return NULL;
}
for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(value); i++) {
PyObject *item = PyTuple_GET_ITEM(value, i);
if (i > 0) {
if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0) {
_PyUnicodeWriter_Dealloc(&writer);
if (PyUnicodeWriter_WriteUTF8(writer, ", ", 2) < 0) {
PyUnicodeWriter_Discard(writer);
return NULL;
}
}
if (_Py_typing_type_repr(&writer, item) < 0) {
_PyUnicodeWriter_Dealloc(&writer);
if (_Py_typing_type_repr(writer, item) < 0) {
PyUnicodeWriter_Discard(writer);
return NULL;
}
}
if (_PyUnicodeWriter_WriteASCIIString(&writer, ")", 1) < 0) {
_PyUnicodeWriter_Dealloc(&writer);
if (PyUnicodeWriter_WriteChar(writer, ')') < 0) {
PyUnicodeWriter_Discard(writer);
return NULL;
}
}
else {
if (_Py_typing_type_repr(&writer, value) < 0) {
_PyUnicodeWriter_Dealloc(&writer);
if (_Py_typing_type_repr(writer, value) < 0) {
PyUnicodeWriter_Discard(writer);
return NULL;
}
}
return _PyUnicodeWriter_Finish(&writer);
return PyUnicodeWriter_Finish(writer);
}
return Py_NewRef(value);
}
Expand Down Expand Up @@ -259,7 +261,7 @@ _Py_typing_type_repr(PyUnicodeWriter *writer, PyObject *p)
}

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 &&
Expand Down Expand Up @@ -306,7 +308,7 @@ _Py_typing_type_repr(PyUnicodeWriter *writer, PyObject *p)
if (r == NULL) {
return -1;
}
rc = _PyUnicodeWriter_WriteStr(writer, r);
rc = PyUnicodeWriter_WriteStr(writer, r);
Py_DECREF(r);
return rc;
}
Expand Down
Loading