Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 23 additions & 21 deletions Modules/_io/bufferedio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1026,58 +1026,60 @@ static PyObject *
_io__Buffered_read1_impl(buffered *self, Py_ssize_t n)
/*[clinic end generated code: output=bcc4fb4e54d103a3 input=3d0ad241aa52b36c]*/
{
Py_ssize_t have, r;
PyObject *res = NULL;

CHECK_INITIALIZED(self)
if (n < 0) {
n = self->buffer_size;
}

CHECK_CLOSED(self, "read of closed file")

if (n == 0)
return PyBytes_FromStringAndSize(NULL, 0);
if (n == 0) {
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comes up fairly frequently in the I/O code, thinking of making a _Py_EMPTY_BYTES internal macro for it (separate from this PR)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean a macro specific to the _io module?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking pycore_bytesobject.h; currently shows up 24 times across the CPython source code. BufferedIO and a couple other places also use a pattern: (PyObject *)&_Py_SINGLETON(bytes_empty)

}

/* Return up to n bytes. If at least one byte is buffered, we
only return buffered bytes. Otherwise, we do one raw read. */

have = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
Py_ssize_t have = Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t);
if (have > 0) {
n = Py_MIN(have, n);
res = _bufferedreader_read_fast(self, n);
PyObject *res = _bufferedreader_read_fast(self, n);
assert(res != Py_None);
return res;
}
res = PyBytes_FromStringAndSize(NULL, n);
if (res == NULL)
return NULL;

if (!ENTER_BUFFERED(self)) {
Py_DECREF(res);
return NULL;
}

/* Flush the write buffer if necessary */
if (self->writable) {
PyObject *r = buffered_flush_and_rewind_unlocked(self);
if (r == NULL) {
PyObject *res = buffered_flush_and_rewind_unlocked(self);
if (res == NULL) {
LEAVE_BUFFERED(self)
Py_DECREF(res);
return NULL;
}
Py_DECREF(r);
Py_DECREF(res);
}
_bufferedreader_reset_buf(self);
r = _bufferedreader_raw_read(self, PyBytes_AS_STRING(res), n);

PyBytesWriter *writer = PyBytesWriter_Create(n);
if (writer == NULL) {
return NULL;
}

Py_ssize_t r = _bufferedreader_raw_read(self,
PyBytesWriter_GetData(writer), n);
LEAVE_BUFFERED(self)
if (r == -1) {
Py_DECREF(res);
PyBytesWriter_Discard(writer);
return NULL;
}
if (r == -2)
if (r == -2) {
r = 0;
if (n > r)
_PyBytes_Resize(&res, r);
return res;
}

return PyBytesWriter_FinishWithSize(writer, r);
}

static PyObject *
Expand Down
Loading