Skip to content

Commit affd2f2

Browse files
cmaloneyyihong0618
andauthored
[3.13] gh-142594: fix by property calls io.TextIOWrapper.detach (GH-142706) (GH-142757)
(cherry picked from commit 1d3854a) Signed-off-by: yihong0618 <[email protected]> Co-authored-by: yihong <[email protected]>
1 parent 3e45440 commit affd2f2

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

Lib/test/test_io.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4160,6 +4160,22 @@ def write(self, data):
41604160
self.assertEqual([b"abcdef", b"middle", b"g"*chunk_size],
41614161
buf._write_stack)
41624162

4163+
def test_issue142594(self):
4164+
wrapper = None
4165+
detached = False
4166+
class ReentrantRawIO(self.RawIOBase):
4167+
@property
4168+
def closed(self):
4169+
nonlocal detached
4170+
if wrapper is not None and not detached:
4171+
detached = True
4172+
wrapper.detach()
4173+
return False
4174+
4175+
raw = ReentrantRawIO()
4176+
wrapper = self.TextIOWrapper(raw)
4177+
wrapper.close() # should not crash
4178+
41634179

41644180
class PyTextIOWrapperTest(TextIOWrapperTest):
41654181
io = pyio
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix crash in ``TextIOWrapper.close()`` when the underlying buffer's
2+
``closed`` property calls :meth:`~io.TextIOBase.detach`.

Modules/_io/textio.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3133,6 +3133,9 @@ _io_TextIOWrapper_close_impl(textio *self)
31333133
if (r > 0) {
31343134
Py_RETURN_NONE; /* stream already closed */
31353135
}
3136+
if (self->detached) {
3137+
Py_RETURN_NONE; /* gh-142594 null pointer issue */
3138+
}
31363139
else {
31373140
PyObject *exc = NULL;
31383141
if (self->finalizing) {

0 commit comments

Comments
 (0)