gh-140650: Fix write(), flush() and close() methods of io.BufferedWriter (GH-140653)

They could raise SystemError or crash when getting the "closed" attribute
or converting it to boolean raises an exception.
This commit is contained in:
Sachin Shah 2025-11-05 14:15:27 -05:00 committed by GitHub
parent 3cb1ab0e5d
commit 1d25b751c5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 43 additions and 6 deletions

View file

@ -962,6 +962,27 @@ def test_args_error(self):
with self.assertRaisesRegex(TypeError, "BufferedWriter"):
self.tp(self.BytesIO(), 1024, 1024, 1024)
def test_non_boolean_closed_attr(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)
def test_closed_attr_raises(self):
class MockRawIOClosedRaises(self.MockRawIO):
@property
def closed(self):
raise ValueError("test")
bufio = self.tp(MockRawIOClosedRaises())
self.assertRaisesRegex(ValueError, "test", bufio.write, b"")
self.assertRaisesRegex(ValueError, "test", bufio.flush)
self.assertRaisesRegex(ValueError, "test", bufio.close)
class PyBufferedWriterTest(BufferedWriterTest, PyTestCase):
tp = pyio.BufferedWriter