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

gh-129813, PEP 782: Use PyBytesWriter in bufferedio.c by vstinner · Pull Request #139121 · 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/bufferedio.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 @@ -1789,18 +1789,18 @@ _bufferedreader_read_fast(buffered *self, Py_ssize_t n)
static PyObject *
_bufferedreader_read_generic(buffered *self, Py_ssize_t n)
{
PyObject *res = NULL;
Py_ssize_t current_size, remaining, written;
char *out;

current_size = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
if (n <= current_size)
return _bufferedreader_read_fast(self, n);

res = PyBytes_FromStringAndSize(NULL, n);
if (res == NULL)
PyBytesWriter *writer = PyBytesWriter_Create(n);
if (writer == NULL) {
goto error;
out = PyBytes_AS_STRING(res);
}
char *out = PyBytesWriter_GetData(writer);

remaining = n;
written = 0;
if (current_size > 0) {
Expand Down Expand Up @@ -1829,11 +1829,9 @@ _bufferedreader_read_generic(buffered *self, Py_ssize_t n)
if (r == 0 || r == -2) {
/* EOF occurred or read() would block. */
if (r == 0 || written > 0) {
if (_PyBytes_Resize(&res, written))
goto error;
return res;
return PyBytesWriter_FinishWithSize(writer, written);
}
Py_DECREF(res);
PyBytesWriter_Discard(writer);
Py_RETURN_NONE;
}
remaining -= r;
Expand All @@ -1853,11 +1851,9 @@ _bufferedreader_read_generic(buffered *self, Py_ssize_t n)
if (r == 0 || r == -2) {
/* EOF occurred or read() would block. */
if (r == 0 || written > 0) {
if (_PyBytes_Resize(&res, written))
goto error;
return res;
return PyBytesWriter_FinishWithSize(writer, written);
}
Py_DECREF(res);
PyBytesWriter_Discard(writer);
Py_RETURN_NONE;
}
if (remaining > r) {
Expand All @@ -1876,10 +1872,10 @@ _bufferedreader_read_generic(buffered *self, Py_ssize_t n)
break;
}

return res;
return PyBytesWriter_Finish(writer);

error:
Py_XDECREF(res);
PyBytesWriter_Discard(writer);
return NULL;
}

Expand Down
Loading

Back | FazBrowse Home | New Git URL