Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit bb594e8

Browse files
authoredOct 10, 2024
pythongh-125196: Use PyUnicodeWriter for repr(dict) (python#125270)
1 parent 3b87fb7 commit bb594e8

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed
 

‎Objects/dictobject.c

+36-36
Original file line numberDiff line numberDiff line change
@@ -3200,83 +3200,83 @@ static PyObject *
32003200
dict_repr_lock_held(PyObject *self)
32013201
{
32023202
PyDictObject *mp = (PyDictObject *)self;
3203-
Py_ssize_t i;
32043203
PyObject *key = NULL, *value = NULL;
3205-
_PyUnicodeWriter writer;
3206-
int first;
3207-
32083204
ASSERT_DICT_LOCKED(mp);
32093205

3210-
i = Py_ReprEnter((PyObject *)mp);
3211-
if (i != 0) {
3212-
return i > 0 ? PyUnicode_FromString("{...}") : NULL;
3206+
int res = Py_ReprEnter((PyObject *)mp);
3207+
if (res != 0) {
3208+
return (res > 0 ? PyUnicode_FromString("{...}") : NULL);
32133209
}
32143210

32153211
if (mp->ma_used == 0) {
32163212
Py_ReprLeave((PyObject *)mp);
32173213
return PyUnicode_FromString("{}");
32183214
}
32193215

3220-
_PyUnicodeWriter_Init(&writer);
3221-
writer.overallocate = 1;
3222-
/* "{" + "1: 2" + ", 3: 4" * (len - 1) + "}" */
3223-
writer.min_length = 1 + 4 + (2 + 4) * (mp->ma_used - 1) + 1;
3216+
// "{" + "1: 2" + ", 3: 4" * (len - 1) + "}"
3217+
Py_ssize_t prealloc = 1 + 4 + 6 * (mp->ma_used - 1) + 1;
3218+
PyUnicodeWriter *writer = PyUnicodeWriter_Create(prealloc);
3219+
if (writer == NULL) {
3220+
goto error;
3221+
}
32243222

3225-
if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0)
3223+
if (PyUnicodeWriter_WriteChar(writer, '{') < 0) {
32263224
goto error;
3225+
}
32273226

32283227
/* Do repr() on each key+value pair, and insert ": " between them.
32293228
Note that repr may mutate the dict. */
3230-
i = 0;
3231-
first = 1;
3229+
Py_ssize_t i = 0;
3230+
int first = 1;
32323231
while (_PyDict_Next((PyObject *)mp, &i, &key, &value, NULL)) {
3233-
PyObject *s;
3234-
int res;
3235-
3236-
/* Prevent repr from deleting key or value during key format. */
3232+
// Prevent repr from deleting key or value during key format.
32373233
Py_INCREF(key);
32383234
Py_INCREF(value);
32393235

32403236
if (!first) {
3241-
if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0)
3237+
// Write ", "
3238+
if (PyUnicodeWriter_WriteChar(writer, ',') < 0) {
3239+
goto error;
3240+
}
3241+
if (PyUnicodeWriter_WriteChar(writer, ' ') < 0) {
32423242
goto error;
3243+
}
32433244
}
32443245
first = 0;
32453246

3246-
s = PyObject_Repr(key);
3247-
if (s == NULL)
3248-
goto error;
3249-
res = _PyUnicodeWriter_WriteStr(&writer, s);
3250-
Py_DECREF(s);
3251-
if (res < 0)
3247+
// Write repr(key)
3248+
if (PyUnicodeWriter_WriteRepr(writer, key) < 0) {
32523249
goto error;
3250+
}
32533251

3254-
if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0)
3252+
// Write ": "
3253+
if (PyUnicodeWriter_WriteChar(writer, ':') < 0) {
32553254
goto error;
3256-
3257-
s = PyObject_Repr(value);
3258-
if (s == NULL)
3255+
}
3256+
if (PyUnicodeWriter_WriteChar(writer, ' ') < 0) {
32593257
goto error;
3260-
res = _PyUnicodeWriter_WriteStr(&writer, s);
3261-
Py_DECREF(s);
3262-
if (res < 0)
3258+
}
3259+
3260+
// Write repr(value)
3261+
if (PyUnicodeWriter_WriteRepr(writer, value) < 0) {
32633262
goto error;
3263+
}
32643264

32653265
Py_CLEAR(key);
32663266
Py_CLEAR(value);
32673267
}
32683268

3269-
writer.overallocate = 0;
3270-
if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0)
3269+
if (PyUnicodeWriter_WriteChar(writer, '}') < 0) {
32713270
goto error;
3271+
}
32723272

32733273
Py_ReprLeave((PyObject *)mp);
32743274

3275-
return _PyUnicodeWriter_Finish(&writer);
3275+
return PyUnicodeWriter_Finish(writer);
32763276

32773277
error:
32783278
Py_ReprLeave((PyObject *)mp);
3279-
_PyUnicodeWriter_Dealloc(&writer);
3279+
PyUnicodeWriter_Discard(writer);
32803280
Py_XDECREF(key);
32813281
Py_XDECREF(value);
32823282
return NULL;

0 commit comments

Comments
 (0)