@@ -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