@@ -3200,83 +3200,83 @@ static PyObject *
3200
3200
dict_repr_lock_held (PyObject * self )
3201
3201
{
3202
3202
PyDictObject * mp = (PyDictObject * )self ;
3203
- Py_ssize_t i ;
3204
3203
PyObject * key = NULL , * value = NULL ;
3205
- _PyUnicodeWriter writer ;
3206
- int first ;
3207
-
3208
3204
ASSERT_DICT_LOCKED (mp );
3209
3205
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 ) ;
3213
3209
}
3214
3210
3215
3211
if (mp -> ma_used == 0 ) {
3216
3212
Py_ReprLeave ((PyObject * )mp );
3217
3213
return PyUnicode_FromString ("{}" );
3218
3214
}
3219
3215
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
+ }
3224
3222
3225
- if (_PyUnicodeWriter_WriteChar ( & writer , '{' ) < 0 )
3223
+ if (PyUnicodeWriter_WriteChar ( writer , '{' ) < 0 ) {
3226
3224
goto error ;
3225
+ }
3227
3226
3228
3227
/* Do repr() on each key+value pair, and insert ": " between them.
3229
3228
Note that repr may mutate the dict. */
3230
- i = 0 ;
3231
- first = 1 ;
3229
+ Py_ssize_t i = 0 ;
3230
+ int first = 1 ;
3232
3231
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.
3237
3233
Py_INCREF (key );
3238
3234
Py_INCREF (value );
3239
3235
3240
3236
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 ) {
3242
3242
goto error ;
3243
+ }
3243
3244
}
3244
3245
first = 0 ;
3245
3246
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 ) {
3252
3249
goto error ;
3250
+ }
3253
3251
3254
- if (_PyUnicodeWriter_WriteASCIIString (& writer , ": " , 2 ) < 0 )
3252
+ // Write ": "
3253
+ if (PyUnicodeWriter_WriteChar (writer , ':' ) < 0 ) {
3255
3254
goto error ;
3256
-
3257
- s = PyObject_Repr (value );
3258
- if (s == NULL )
3255
+ }
3256
+ if (PyUnicodeWriter_WriteChar (writer , ' ' ) < 0 ) {
3259
3257
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 ) {
3263
3262
goto error ;
3263
+ }
3264
3264
3265
3265
Py_CLEAR (key );
3266
3266
Py_CLEAR (value );
3267
3267
}
3268
3268
3269
- writer .overallocate = 0 ;
3270
- if (_PyUnicodeWriter_WriteChar (& writer , '}' ) < 0 )
3269
+ if (PyUnicodeWriter_WriteChar (writer , '}' ) < 0 ) {
3271
3270
goto error ;
3271
+ }
3272
3272
3273
3273
Py_ReprLeave ((PyObject * )mp );
3274
3274
3275
- return _PyUnicodeWriter_Finish ( & writer );
3275
+ return PyUnicodeWriter_Finish ( writer );
3276
3276
3277
3277
error :
3278
3278
Py_ReprLeave ((PyObject * )mp );
3279
- _PyUnicodeWriter_Dealloc ( & writer );
3279
+ PyUnicodeWriter_Discard ( writer );
3280
3280
Py_XDECREF (key );
3281
3281
Py_XDECREF (value );
3282
3282
return NULL ;
0 commit comments