Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Lib/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -4194,6 +4194,22 @@ def write(self, data):
self.assertEqual([b"abcdef", b"middle", b"g"*chunk_size],
buf._write_stack)

def test_issue142594(self):
wrapper = None
detached = False
class ReentrantRawIO(self.RawIOBase):
@property
def closed(self):
nonlocal detached
if wrapper is not None and not detached:
detached = True
wrapper.detach()
return False

raw = ReentrantRawIO()
wrapper = self.TextIOWrapper(raw)
wrapper.close() # should not crash


class PyTextIOWrapperTest(TextIOWrapperTest):
io = pyio
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix crash in ``TextIOWrapper.close()`` when the underlying buffer's
``closed`` property calls :meth:`~io.TextIOBase.detach`.
3 changes: 3 additions & 0 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -3149,6 +3149,9 @@ _io_TextIOWrapper_close_impl(textio *self)
if (r > 0) {
Py_RETURN_NONE; /* stream already closed */
}
if (self->detached) {
Py_RETURN_NONE; /* gh-142594 null pointer issue */
}
else {
PyObject *exc = NULL;
if (self->finalizing) {
Expand Down
Loading