Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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/test_textio.py
Original file line number Diff line number Diff line change
Expand Up @@ -1544,6 +1544,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, PyTestCase):
shutdown_error = "LookupError: unknown encoding: ascii"
Expand Down
Comment thread
yihong0618 marked this conversation as resolved.
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 ``TextIOWrapper.detach()``.
Comment thread
yihong0618 marked this conversation as resolved.
Outdated
3 changes: 3 additions & 0 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -3150,6 +3150,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