Skip to content

Commit db68bfc

Browse files
authored
gh-138720: Make Buffered closed check match flush (GH-138724)
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.
1 parent 9f7bbaf commit db68bfc

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

Lib/test/test_io/test_general.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,6 +1980,10 @@ def test_readinto(self):
19801980
self.assertEqual(getattr(pair, method)(data), 5)
19811981
self.assertEqual(bytes(data), b"abcde")
19821982

1983+
# gh-138720: C BufferedRWPair would destruct in a bad order resulting in
1984+
# an unraisable exception.
1985+
support.gc_collect()
1986+
19831987
def test_write(self):
19841988
w = self.MockRawIO()
19851989
pair = self.tp(self.MockRawIO(), w)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix an issue where :class:`io.BufferedWriter` and :class:`io.BufferedRandom`
2+
had different definitions of "closed" for :meth:`~io.IOBase.close` and
3+
:meth:`~io.IOBase.flush` which resulted in an exception when close called
4+
flush but flush thought the file was already closed.

Modules/_io/bufferedio.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,8 +553,8 @@ _io__Buffered_close_impl(buffered *self)
553553
if (!ENTER_BUFFERED(self)) {
554554
return NULL;
555555
}
556-
557-
r = buffered_closed(self);
556+
/* gh-138720: Use IS_CLOSED to match flush CHECK_CLOSED. */
557+
r = IS_CLOSED(self);
558558
if (r < 0)
559559
goto end;
560560
if (r > 0) {

0 commit comments

Comments
 (0)