@@ -1407,32 +1407,57 @@ static PyObject *
14071407_bufferedreader_read_all (buffered * self )
14081408{
14091409 Py_ssize_t current_size ;
1410- PyObject * res , * data = NULL ;
1411- PyObject * chunks = PyList_New (0 );
1412-
1413- if (chunks == NULL )
1414- return NULL ;
1410+ PyObject * res , * data = NULL , * chunk , * chunks ;
14151411
14161412 /* First copy what we have in the current buffer. */
14171413 current_size = Py_SAFE_DOWNCAST (READAHEAD (self ), Py_off_t , Py_ssize_t );
14181414 if (current_size ) {
14191415 data = PyBytes_FromStringAndSize (
14201416 self -> buffer + self -> pos , current_size );
1421- if (data == NULL ) {
1422- Py_DECREF (chunks );
1417+ if (data == NULL )
14231418 return NULL ;
1424- }
14251419 }
14261420 _bufferedreader_reset_buf (self );
14271421 /* We're going past the buffer's bounds, flush it */
14281422 if (self -> writable ) {
14291423 res = _bufferedwriter_flush_unlocked (self , 1 );
1430- if (res == NULL ) {
1431- Py_DECREF (chunks );
1424+ if (res == NULL )
14321425 return NULL ;
1433- }
14341426 Py_CLEAR (res );
14351427 }
1428+
1429+ if (PyObject_HasAttr (self -> raw , _PyIO_str_readall )) {
1430+ chunk = PyObject_CallMethodObjArgs (self -> raw , _PyIO_str_readall , NULL );
1431+ if (chunk == NULL )
1432+ return NULL ;
1433+ if (chunk != Py_None && !PyBytes_Check (chunk )) {
1434+ Py_XDECREF (data );
1435+ Py_DECREF (chunk );
1436+ PyErr_SetString (PyExc_TypeError , "readall() should return bytes" );
1437+ return NULL ;
1438+ }
1439+ if (chunk == Py_None ) {
1440+ if (current_size == 0 )
1441+ return chunk ;
1442+ else {
1443+ Py_DECREF (chunk );
1444+ return data ;
1445+ }
1446+ }
1447+ else if (current_size ) {
1448+ PyBytes_Concat (& data , chunk );
1449+ Py_DECREF (chunk );
1450+ if (data == NULL )
1451+ return NULL ;
1452+ return data ;
1453+ } else
1454+ return chunk ;
1455+ }
1456+
1457+ chunks = PyList_New (0 );
1458+ if (chunks == NULL )
1459+ return NULL ;
1460+
14361461 while (1 ) {
14371462 if (data ) {
14381463 if (PyList_Append (chunks , data ) < 0 ) {
0 commit comments