Skip to content

Commit 61122b1

Browse files
committed
apply feedback: allow IS_CLOSED to return -1 for error checking
1 parent 0e5bed9 commit 61122b1

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

Lib/test/test_io/test_bufferedio.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -962,12 +962,13 @@ def test_args_error(self):
962962
with self.assertRaisesRegex(TypeError, "BufferedWriter"):
963963
self.tp(self.BytesIO(), 1024, 1024, 1024)
964964

965-
def test_close_without_closed(self):
965+
def test_closed_errors(self):
966966
# gh-140650: check TypeError is raised
967967
class MockRawIOWithoutClosed(self.MockRawIO):
968968
closed = NotImplemented
969969

970970
bufio = self.tp(MockRawIOWithoutClosed())
971+
self.assertRaises(TypeError, bufio.write, b"")
971972
self.assertRaises(TypeError, bufio.close)
972973

973974

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
Fix an issue where closing :class:`io.BufferedWriter` could crash
2-
if the closed attribute had the wrong type.
2+
if the closed attribute raised an exception.

Modules/_io/bufferedio.c

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -362,16 +362,24 @@ _enter_buffered_busy(buffered *self)
362362
}
363363

364364
#define IS_CLOSED(self) \
365-
(!self->buffer || \
365+
(!self->buffer ? !self->buffer : \
366366
(self->fast_closed_checks \
367367
? _PyFileIO_closed(self->raw) \
368368
: buffered_closed(self)))
369369

370370
#define CHECK_CLOSED(self, error_msg) \
371-
if (IS_CLOSED(self) && (Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t) == 0)) { \
372-
PyErr_SetString(PyExc_ValueError, error_msg); \
373-
return NULL; \
374-
} \
371+
do { \
372+
int _closed = IS_CLOSED(self); \
373+
if (_closed < 0) { \
374+
return NULL; \
375+
} \
376+
if (_closed && \
377+
(Py_SAFE_DOWNCAST(READAHEAD(self), Py_off_t, Py_ssize_t) == 0)) \
378+
{ \
379+
PyErr_SetString(PyExc_ValueError, error_msg); \
380+
return NULL; \
381+
} \
382+
} while (0);
375383

376384
#define VALID_READ_BUFFER(self) \
377385
(self->readable && self->read_end != -1)
@@ -555,9 +563,11 @@ _io__Buffered_close_impl(buffered *self)
555563
}
556564
/* gh-138720: Use IS_CLOSED to match flush CHECK_CLOSED. */
557565
r = IS_CLOSED(self);
566+
if (r < 0) {
567+
goto end;
568+
}
558569
if (r > 0) {
559-
if (!PyErr_Occurred())
560-
res = Py_NewRef(Py_None);
570+
res = Py_None;
561571
goto end;
562572
}
563573

@@ -2078,6 +2088,7 @@ _io_BufferedWriter_write_impl(buffered *self, Py_buffer *buffer)
20782088
PyObject *res = NULL;
20792089
Py_ssize_t written, avail, remaining;
20802090
Py_off_t offset;
2091+
int r;
20812092

20822093
CHECK_INITIALIZED(self)
20832094

@@ -2086,7 +2097,11 @@ _io_BufferedWriter_write_impl(buffered *self, Py_buffer *buffer)
20862097

20872098
/* Issue #31976: Check for closed file after acquiring the lock. Another
20882099
thread could be holding the lock while closing the file. */
2089-
if (IS_CLOSED(self)) {
2100+
r = IS_CLOSED(self);
2101+
if (r < 0) {
2102+
goto error;
2103+
}
2104+
if (r > 0) {
20902105
PyErr_SetString(PyExc_ValueError, "write to closed file");
20912106
goto error;
20922107
}

0 commit comments

Comments
 (0)