| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -962,6 +962,27 @@ def test_args_error(self): | |||
| 962 | 962 | with self.assertRaisesRegex(TypeError, "BufferedWriter"): | |
| 963 | 963 | self.tp(self.BytesIO(), 1024, 1024, 1024) | |
| 964 | 964 | ||
| 965 | + def test_non_boolean_closed_attr(self): | ||
| 966 | + # gh-140650: check TypeError is raised | ||
| 967 | + class MockRawIOWithoutClosed(self.MockRawIO): | ||
| 968 | + closed = NotImplemented | ||
| 969 | + | ||
| 970 | + bufio = self.tp(MockRawIOWithoutClosed()) | ||
| 971 | + self.assertRaises(TypeError, bufio.write, b"") | ||
| 972 | + self.assertRaises(TypeError, bufio.flush) | ||
| 973 | + self.assertRaises(TypeError, bufio.close) | ||
| 974 | + | ||
| 975 | + def test_closed_attr_raises(self): | ||
| 976 | + class MockRawIOClosedRaises(self.MockRawIO): | ||
| 977 | + @property | ||
| 978 | + def closed(self): | ||
| 979 | + raise ValueError("test") | ||
| 980 | + | ||
| 981 | + bufio = self.tp(MockRawIOClosedRaises()) | ||
| 982 | + self.assertRaisesRegex(ValueError, "test", bufio.write, b"") | ||
| 983 | + self.assertRaisesRegex(ValueError, "test", bufio.flush) | ||
| 984 | + self.assertRaisesRegex(ValueError, "test", bufio.close) | ||
| 985 | + | ||
| 965 | 986 | ||
| 966 | 987 | class PyBufferedWriterTest(BufferedWriterTest, PyTestCase): | |
| 967 | 988 | tp = pyio.BufferedWriter | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,3 @@ | |||
| 1 | + Fix an issue where closing :class:`io.BufferedWriter` could crash if | ||
| 2 | + the closed attribute raised an exception on access or could not be | ||
| 3 | + converted to a boolean. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -362,16 +362,24 @@ _enter_buffered_busy(buffered *self) | |||
| 362 | 362 | } | |
| 363 | 363 | ||
| 364 | 364 | #define IS_CLOSED(self) \ | |
| 365 | - (!self->buffer || \ | ||
| 365 | + (!self->buffer ? 1 : \ | ||
| 366 | 366 | (self->fast_closed_checks \ | |
| 367 | 367 | ? _PyFileIO_closed(self->raw) \ | |
| 368 | 368 | : buffered_closed(self))) | |
| 369 | 369 | ||
| 370 | 370 | #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); | ||
| 375 | 383 | ||
| 376 | 384 | #define VALID_READ_BUFFER(self) \ | |
| 377 | 385 | (self->readable && self->read_end != -1) | |
@@ -2079,6 +2087,7 @@ _io_BufferedWriter_write_impl(buffered *self, Py_buffer *buffer) | |||
| 2079 | 2087 | PyObject *res = NULL; | |
| 2080 | 2088 | Py_ssize_t written, avail, remaining; | |
| 2081 | 2089 | Py_off_t offset; | |
| 2090 | + int r; | ||
| 2082 | 2091 | ||
| 2083 | 2092 | CHECK_INITIALIZED(self) | |
| 2084 | 2093 | ||
@@ -2087,7 +2096,11 @@ _io_BufferedWriter_write_impl(buffered *self, Py_buffer *buffer) | |||
| 2087 | 2096 | ||
| 2088 | 2097 | /* Issue #31976: Check for closed file after acquiring the lock. Another | |
| 2089 | 2098 | thread could be holding the lock while closing the file. */ | |
| 2090 | - if (IS_CLOSED(self)) { | ||
| 2099 | + r = IS_CLOSED(self); | ||
| 2100 | + if (r < 0) { | ||
| 2101 | + goto error; | ||
| 2102 | + } | ||
| 2103 | + if (r > 0) { | ||
| 2091 | 2104 | PyErr_SetString(PyExc_ValueError, "write to closed file"); | |
| 2092 | 2105 | goto error; | |
| 2093 | 2106 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments