@@ -266,83 +266,66 @@ static PyObject *
266
266
structseq_repr (PyStructSequence * obj )
267
267
{
268
268
PyTypeObject * typ = Py_TYPE (obj );
269
- _PyUnicodeWriter writer ;
270
269
271
- /* Write "typename(" */
272
- PyObject * type_name = PyUnicode_DecodeUTF8 (typ -> tp_name ,
273
- strlen (typ -> tp_name ),
274
- NULL );
275
- if (type_name == NULL ) {
270
+ // count 5 characters per item: "x=1, "
271
+ Py_ssize_t type_name_len = strlen (typ -> tp_name );
272
+ Py_ssize_t prealloc = (type_name_len + 1
273
+ + VISIBLE_SIZE (obj ) * 5 + 1 );
274
+ PyUnicodeWriter * writer = PyUnicodeWriter_Create (prealloc );
275
+ if (writer == NULL ) {
276
276
return NULL ;
277
277
}
278
278
279
- _PyUnicodeWriter_Init (& writer );
280
- writer .overallocate = 1 ;
281
- /* count 5 characters per item: "x=1, " */
282
- writer .min_length = (PyUnicode_GET_LENGTH (type_name ) + 1
283
- + VISIBLE_SIZE (obj ) * 5 + 1 );
284
-
285
- if (_PyUnicodeWriter_WriteStr (& writer , type_name ) < 0 ) {
286
- Py_DECREF (type_name );
279
+ // Write "typename("
280
+ if (PyUnicodeWriter_WriteUTF8 (writer , typ -> tp_name , type_name_len ) < 0 ) {
287
281
goto error ;
288
282
}
289
- Py_DECREF (type_name );
290
-
291
- if (_PyUnicodeWriter_WriteChar (& writer , '(' ) < 0 ) {
283
+ if (PyUnicodeWriter_WriteChar (writer , '(' ) < 0 ) {
292
284
goto error ;
293
285
}
294
286
295
287
for (Py_ssize_t i = 0 ; i < VISIBLE_SIZE (obj ); i ++ ) {
296
288
if (i > 0 ) {
297
- /* Write ", " */
298
- if (_PyUnicodeWriter_WriteASCIIString (& writer , ", " , 2 ) < 0 ) {
289
+ // Write ", "
290
+ if (PyUnicodeWriter_WriteChar (writer , ',' ) < 0 ) {
291
+ goto error ;
292
+ }
293
+ if (PyUnicodeWriter_WriteChar (writer , ' ' ) < 0 ) {
299
294
goto error ;
300
295
}
301
296
}
302
297
303
- /* Write " name=repr" */
298
+ // Write name
304
299
const char * name_utf8 = typ -> tp_members [i ].name ;
305
300
if (name_utf8 == NULL ) {
306
- PyErr_Format (PyExc_SystemError , "In structseq_repr(), member %zd name is NULL"
301
+ PyErr_Format (PyExc_SystemError ,
302
+ "In structseq_repr(), member %zd name is NULL"
307
303
" for type %.500s" , i , typ -> tp_name );
308
304
goto error ;
309
305
}
310
-
311
- PyObject * name = PyUnicode_DecodeUTF8 (name_utf8 , strlen (name_utf8 ), NULL );
312
- if (name == NULL ) {
313
- goto error ;
314
- }
315
- if (_PyUnicodeWriter_WriteStr (& writer , name ) < 0 ) {
316
- Py_DECREF (name );
306
+ if (PyUnicodeWriter_WriteUTF8 (writer , name_utf8 , -1 ) < 0 ) {
317
307
goto error ;
318
308
}
319
- Py_DECREF (name );
320
309
321
- if (_PyUnicodeWriter_WriteChar (& writer , '=' ) < 0 ) {
310
+ // Write "=" + repr(value)
311
+ if (PyUnicodeWriter_WriteChar (writer , '=' ) < 0 ) {
322
312
goto error ;
323
313
}
324
-
325
314
PyObject * value = PyStructSequence_GetItem ((PyObject * )obj , i );
326
315
assert (value != NULL );
327
- PyObject * repr = PyObject_Repr (value );
328
- if (repr == NULL ) {
329
- goto error ;
330
- }
331
- if (_PyUnicodeWriter_WriteStr (& writer , repr ) < 0 ) {
332
- Py_DECREF (repr );
316
+ if (PyUnicodeWriter_WriteRepr (writer , value ) < 0 ) {
333
317
goto error ;
334
318
}
335
- Py_DECREF (repr );
336
319
}
337
320
338
- if (_PyUnicodeWriter_WriteChar ( & writer , ')' ) < 0 ) {
321
+ if (PyUnicodeWriter_WriteChar ( writer , ')' ) < 0 ) {
339
322
goto error ;
340
323
}
341
324
342
- return _PyUnicodeWriter_Finish ( & writer );
325
+ return PyUnicodeWriter_Finish ( writer );
343
326
344
327
error :
345
- _PyUnicodeWriter_Dealloc ( & writer );
328
+ PyUnicodeWriter_Discard ( writer );
346
329
return NULL ;
347
330
}
348
331
0 commit comments