In `_io__Buffered_flush_impl` the macro `CHECK_CLOSED` is used to check
the `buffered*` is in a good state to be flushed. That differs slightly
from `buffered_closed`.
In some cases, that difference would result in `close()` thinking the
file needed to be flushed and closed while `flush()` thought the file
was already closed.
This could happen during GC and would result in an unraisable exception.
self.assertEqual(getattr(pair, method)(data), 5)
self.assertEqual(bytes(data), b"abcde")
+ # gh-138720: C BufferedRWPair would destruct in a bad order resulting in
+ # an unraisable exception.
+ support.gc_collect()
+
def test_write(self):
w = self.MockRawIO()
pair = self.tp(self.MockRawIO(), w)
--- /dev/null
+Fix an issue where :class:`io.BufferedWriter` and :class:`io.BufferedRandom`
+had different definitions of "closed" for :meth:`~io.IOBase.close` and
+:meth:`~io.IOBase.flush` which resulted in an exception when close called
+flush but flush thought the file was already closed.
if (!ENTER_BUFFERED(self)) {
return NULL;
}
-
- r = buffered_closed(self);
+ /* gh-138720: Use IS_CLOSED to match flush CHECK_CLOSED. */
+ r = IS_CLOSED(self);
if (r < 0)
goto end;
if (r > 0) {