@@ -353,6 +353,13 @@ _build_rval_index_tuple(PyObject *rval, Py_ssize_t idx) {
353
353
return tpl ;
354
354
}
355
355
356
+ static inline int
357
+ _PyUnicodeWriter_IsEmpty (PyUnicodeWriter * writer_pub )
358
+ {
359
+ _PyUnicodeWriter * writer = (_PyUnicodeWriter * )writer_pub ;
360
+ return (writer -> pos == 0 );
361
+ }
362
+
356
363
static PyObject *
357
364
scanstring_unicode (PyObject * pystr , Py_ssize_t end , int strict , Py_ssize_t * next_end_ptr )
358
365
{
@@ -371,9 +378,10 @@ scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next
371
378
const void * buf ;
372
379
int kind ;
373
380
374
- _PyUnicodeWriter writer ;
375
- _PyUnicodeWriter_Init (& writer );
376
- writer .overallocate = 1 ;
381
+ PyUnicodeWriter * writer = PyUnicodeWriter_Create (0 );
382
+ if (writer == NULL ) {
383
+ goto bail ;
384
+ }
377
385
378
386
len = PyUnicode_GET_LENGTH (pystr );
379
387
buf = PyUnicode_DATA (pystr );
@@ -404,7 +412,7 @@ scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next
404
412
405
413
if (c == '"' ) {
406
414
// Fast path for simple case.
407
- if (writer . buffer == NULL ) {
415
+ if (_PyUnicodeWriter_IsEmpty ( writer ) ) {
408
416
PyObject * ret = PyUnicode_Substring (pystr , end , next );
409
417
if (ret == NULL ) {
410
418
goto bail ;
@@ -420,7 +428,7 @@ scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next
420
428
421
429
/* Pick up this chunk if it's not zero length */
422
430
if (next != end ) {
423
- if (_PyUnicodeWriter_WriteSubstring ( & writer , pystr , end , next ) < 0 ) {
431
+ if (PyUnicodeWriter_WriteSubstring ( writer , pystr , end , next ) < 0 ) {
424
432
goto bail ;
425
433
}
426
434
}
@@ -511,18 +519,18 @@ scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next
511
519
end -= 6 ;
512
520
}
513
521
}
514
- if (_PyUnicodeWriter_WriteChar ( & writer , c ) < 0 ) {
522
+ if (PyUnicodeWriter_WriteChar ( writer , c ) < 0 ) {
515
523
goto bail ;
516
524
}
517
525
}
518
526
519
- rval = _PyUnicodeWriter_Finish ( & writer );
527
+ rval = PyUnicodeWriter_Finish ( writer );
520
528
* next_end_ptr = end ;
521
529
return rval ;
522
530
523
531
bail :
524
532
* next_end_ptr = -1 ;
525
- _PyUnicodeWriter_Dealloc ( & writer );
533
+ PyUnicodeWriter_Discard ( writer );
526
534
return NULL ;
527
535
}
528
536
0 commit comments