@@ -571,20 +571,19 @@ _io__IOBase_readline_impl(PyObject *self, Py_ssize_t limit)
571571{
572572 /* For backwards compatibility, a (slowish) readline(). */
573573
574- PyObject * peek , * buffer , * result ;
575- Py_ssize_t old_size = -1 ;
574+ PyObject * peek ;
576575
577576 if (PyObject_GetOptionalAttr (self , & _Py_ID (peek ), & peek ) < 0 ) {
578577 return NULL ;
579578 }
580579
581- buffer = PyByteArray_FromStringAndSize ( NULL , 0 );
582- if (buffer == NULL ) {
580+ PyBytesWriter * writer = PyBytesWriter_Create ( 0 );
581+ if (writer == NULL ) {
583582 Py_XDECREF (peek );
584583 return NULL ;
585584 }
586585
587- while (limit < 0 || PyByteArray_GET_SIZE ( buffer ) < limit ) {
586+ while (limit < 0 || PyBytesWriter_GetSize ( writer ) < limit ) {
588587 Py_ssize_t nreadahead = 1 ;
589588 PyObject * b ;
590589
@@ -650,28 +649,25 @@ _io__IOBase_readline_impl(PyObject *self, Py_ssize_t limit)
650649 break ;
651650 }
652651
653- old_size = PyByteArray_GET_SIZE (buffer );
654- if (PyByteArray_Resize (buffer , old_size + PyBytes_GET_SIZE (b )) < 0 ) {
652+ if (PyBytesWriter_WriteBytes (writer ,
653+ PyBytes_AS_STRING (b ),
654+ PyBytes_GET_SIZE (b )) < 0 ) {
655655 Py_DECREF (b );
656656 goto fail ;
657657 }
658- memcpy (PyByteArray_AS_STRING (buffer ) + old_size ,
659- PyBytes_AS_STRING (b ), PyBytes_GET_SIZE (b ));
660658
661659 Py_DECREF (b );
662660
663- if (PyByteArray_AS_STRING (buffer )[PyByteArray_GET_SIZE (buffer ) - 1 ] == '\n' )
661+ char * data = PyBytesWriter_GetData (writer );
662+ if (data [PyBytesWriter_GetSize (writer ) - 1 ] == '\n' )
664663 break ;
665664 }
666665
667- result = PyBytes_FromStringAndSize (PyByteArray_AS_STRING (buffer ),
668- PyByteArray_GET_SIZE (buffer ));
669666 Py_XDECREF (peek );
670- Py_DECREF (buffer );
671- return result ;
667+ return PyBytesWriter_Finish (writer );
672668 fail :
673669 Py_XDECREF (peek );
674- Py_DECREF ( buffer );
670+ PyBytesWriter_Discard ( writer );
675671 return NULL ;
676672}
677673
0 commit comments