FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-129813, PEP 782: Use PyBytesWriter in _io.FileIO.readall by cmaloney · Pull Request #138901 · python/cpython · GitHub

Repository navigation

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
26 changes: 11 additions & 15 deletions Modules/_io/fileio.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ _io_FileIO_readall_impl(fileio *self)
/*[clinic end generated code: output=faa0292b213b4022 input=10d8b2ec403302dc]*/
{
Py_off_t pos, end;
PyObject *result;
PyBytesWriter *writer;
Py_ssize_t bytes_read = 0;
Py_ssize_t n;
size_t bufsize;
Expand Down Expand Up @@ -794,10 +794,10 @@ _io_FileIO_readall_impl(fileio *self)
}
}


result = PyBytes_FromStringAndSize(NULL, bufsize);
if (result == NULL)
writer = PyBytesWriter_Create(bufsize);
if (writer == NULL) {
return NULL;
}

while (1) {
if (bytes_read >= (Py_ssize_t)bufsize) {
Expand All @@ -806,18 +806,18 @@ _io_FileIO_readall_impl(fileio *self)
PyErr_SetString(PyExc_OverflowError,
"unbounded read returned more bytes "
"than a Python bytes object can hold");
Py_DECREF(result);
PyBytesWriter_Discard(writer);
return NULL;
}

if (PyBytes_GET_SIZE(result) < (Py_ssize_t)bufsize) {
if (_PyBytes_Resize(&result, bufsize) < 0)
if (PyBytesWriter_GetSize(writer) < (Py_ssize_t)bufsize) {
if (PyBytesWriter_Resize(writer, bufsize) < 0)
return NULL;
}
}

n = _Py_read(self->fd,
PyBytes_AS_STRING(result) + bytes_read,
(char*)PyBytesWriter_GetData(writer) + bytes_read,
bufsize - bytes_read);

if (n == 0)
Expand All @@ -827,20 +827,16 @@ _io_FileIO_readall_impl(fileio *self)
PyErr_Clear();
if (bytes_read > 0)
break;
Py_DECREF(result);
PyBytesWriter_Discard(writer);
Py_RETURN_NONE;
}
Py_DECREF(result);
PyBytesWriter_Discard(writer);
return NULL;
}
bytes_read += n;
}

if (PyBytes_GET_SIZE(result) > bytes_read) {
if (_PyBytes_Resize(&result, bytes_read) < 0)
return NULL;
}
return result;
return PyBytesWriter_FinishWithSize(writer, bytes_read);
}

/*[clinic input]
Expand Down
Loading

Back | FazBrowse Home | New Git URL