Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
8 changes: 8 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,14 @@ def test_args_error(self):
with self.assertRaisesRegex(TypeError, "BufferedWriter"):
self.tp(self.BytesIO(), 1024, 1024, 1024)

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

bufio = self.tp(MockRawIOWithoutClosed())
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 had the wrong type.
5 changes: 2 additions & 3 deletions Modules/_io/bufferedio.c
Original file line number Diff line number Diff line change
Expand Up @@ -555,10 +555,9 @@ _io__Buffered_close_impl(buffered *self)
}
/* gh-138720: Use IS_CLOSED to match flush CHECK_CLOSED. */
r = IS_CLOSED(self);
if (r < 0)
goto end;
if (r > 0) {
res = Py_NewRef(Py_None);
if (!PyErr_Occurred())
res = Py_NewRef(Py_None);
goto end;
}

Expand Down
Loading