Skip to content

Commit 04eb5c8

Browse files
authored
pythongh-122361: Use proper PyUnicodeWriter_* API in constevaluator_call (python#122362)
1 parent ae19226 commit 04eb5c8

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

Objects/typevarobject.c

+17-15
Original file line numberDiff line numberDiff line change
@@ -169,38 +169,40 @@ constevaluator_call(PyObject *self, PyObject *args, PyObject *kwargs)
169169
}
170170
PyObject *value = ((constevaluatorobject *)self)->value;
171171
if (format == 3) { // SOURCE
172-
_PyUnicodeWriter writer;
173-
_PyUnicodeWriter_Init(&writer);
172+
PyUnicodeWriter *writer = PyUnicodeWriter_Create(5); // cannot be <5
173+
if (writer == NULL) {
174+
return NULL;
175+
}
174176
if (PyTuple_Check(value)) {
175-
if (_PyUnicodeWriter_WriteASCIIString(&writer, "(", 1) < 0) {
176-
_PyUnicodeWriter_Dealloc(&writer);
177+
if (PyUnicodeWriter_WriteChar(writer, '(') < 0) {
178+
PyUnicodeWriter_Discard(writer);
177179
return NULL;
178180
}
179181
for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(value); i++) {
180182
PyObject *item = PyTuple_GET_ITEM(value, i);
181183
if (i > 0) {
182-
if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0) {
183-
_PyUnicodeWriter_Dealloc(&writer);
184+
if (PyUnicodeWriter_WriteUTF8(writer, ", ", 2) < 0) {
185+
PyUnicodeWriter_Discard(writer);
184186
return NULL;
185187
}
186188
}
187-
if (_Py_typing_type_repr(&writer, item) < 0) {
188-
_PyUnicodeWriter_Dealloc(&writer);
189+
if (_Py_typing_type_repr(writer, item) < 0) {
190+
PyUnicodeWriter_Discard(writer);
189191
return NULL;
190192
}
191193
}
192-
if (_PyUnicodeWriter_WriteASCIIString(&writer, ")", 1) < 0) {
193-
_PyUnicodeWriter_Dealloc(&writer);
194+
if (PyUnicodeWriter_WriteChar(writer, ')') < 0) {
195+
PyUnicodeWriter_Discard(writer);
194196
return NULL;
195197
}
196198
}
197199
else {
198-
if (_Py_typing_type_repr(&writer, value) < 0) {
199-
_PyUnicodeWriter_Dealloc(&writer);
200+
if (_Py_typing_type_repr(writer, value) < 0) {
201+
PyUnicodeWriter_Discard(writer);
200202
return NULL;
201203
}
202204
}
203-
return _PyUnicodeWriter_Finish(&writer);
205+
return PyUnicodeWriter_Finish(writer);
204206
}
205207
return Py_NewRef(value);
206208
}
@@ -259,7 +261,7 @@ _Py_typing_type_repr(PyUnicodeWriter *writer, PyObject *p)
259261
}
260262

261263
if (p == (PyObject *)&_PyNone_Type) {
262-
return _PyUnicodeWriter_WriteASCIIString(writer, "None", 4);
264+
return PyUnicodeWriter_WriteUTF8(writer, "None", 4);
263265
}
264266

265267
if ((rc = PyObject_HasAttrWithError(p, &_Py_ID(__origin__))) > 0 &&
@@ -306,7 +308,7 @@ _Py_typing_type_repr(PyUnicodeWriter *writer, PyObject *p)
306308
if (r == NULL) {
307309
return -1;
308310
}
309-
rc = _PyUnicodeWriter_WriteStr(writer, r);
311+
rc = PyUnicodeWriter_WriteStr(writer, r);
310312
Py_DECREF(r);
311313
return rc;
312314
}

0 commit comments

Comments
 (0)