Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
10 changes: 10 additions & 0 deletions Lib/test/test_io/test_bufferedio.py
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,16 @@ def test_args_error(self):
with self.assertRaisesRegex(TypeError, "BufferedWriter"):
self.tp(self.BytesIO(), 1024, 1024, 1024)

def test_closed_errors(self):
# gh-140650: check TypeError is raised
class MockRawIOWithoutClosed(self.MockRawIO):
closed = NotImplemented

bufio = self.tp(MockRawIOWithoutClosed())
self.assertRaises(TypeError, bufio.write, b"")
self.assertRaises(TypeError, bufio.flush)
self.assertRaises(TypeError, bufio.close)


class PyBufferedWriterTest(BufferedWriterTest, PyTestCase):
tp = pyio.BufferedWriter
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix an issue where closing :class:`io.BufferedWriter` could crash
if the closed attribute raised an exception.
Copy link
Member

Choose a reason for hiding this comment

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

This is not accurate. Bad things happen when the value of the closed attribute cannot be converted to boolean.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In addition to the "cannot be converted to a boolean" case, this patch also fixes cases where closed is defined as a managed attribute equivalent to

@property
def closed(self):
    raise Exception()

Should the news entry just focus on the first type error case, or mention fixing this case as well?

Copy link
Member

Choose a reason for hiding this comment

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

Maybe we should add tests on this behavior too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated the news to reflect both cases, and added a test for the other behavior. I wonder if we need a line mentioning that exceptions from .closed are no longer swallowed by the corresponding methods (like .write(), .flush())

30 changes: 22 additions & 8 deletions Modules/_io/bufferedio.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,16 +362,24 @@ _enter_buffered_busy(buffered *self)
}

#define IS_CLOSED(self) \
(!self->buffer || \
(!self->buffer ? 1 : \
(self->fast_closed_checks \
? _PyFileIO_closed(self->raw) \
: buffered_closed(self)))

#define CHECK_CLOSED(self, error_msg) \
if (IS_CLOSED(self) && (Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t) == 0)) { \
PyErr_SetString(PyExc_ValueError, error_msg); \
return NULL; \
} \
do { \
int _closed = IS_CLOSED(self); \
if (_closed < 0) { \
return NULL; \
} \
if (_closed && \
(Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t) == 0)) \
{ \
PyErr_SetString(PyExc_ValueError, error_msg); \
return NULL; \
} \
} while (0);

#define VALID_READ_BUFFER(self) \
(self->readable && self->read_end != -1)
Expand Down Expand Up @@ -555,10 +563,11 @@ _io__Buffered_close_impl(buffered *self)
}
/* gh-138720: Use IS_CLOSED to match flush CHECK_CLOSED. */
r = IS_CLOSED(self);
if (r < 0)
if (r < 0) {
goto end;
}
if (r > 0) {
res = Py_NewRef(Py_None);
res = Py_None;
goto end;
}

Expand Down Expand Up @@ -2079,6 +2088,7 @@ _io_BufferedWriter_write_impl(buffered *self, Py_buffer *buffer)
PyObject *res = NULL;
Py_ssize_t written, avail, remaining;
Py_off_t offset;
int r;

CHECK_INITIALIZED(self)

Expand All @@ -2087,7 +2097,11 @@ _io_BufferedWriter_write_impl(buffered *self, Py_buffer *buffer)

/* Issue #31976: Check for closed file after acquiring the lock. Another
thread could be holding the lock while closing the file. */
if (IS_CLOSED(self)) {
r = IS_CLOSED(self);
if (r < 0) {
goto error;
}
if (r > 0) {
PyErr_SetString(PyExc_ValueError, "write to closed file");
goto error;
}
Expand Down
Loading