Skip to content

Commit 58da2a9

Browse files
cmaloneyyihong0618
andauthored
[3.14] gh-142594: fix by property calls io.TextIOWrapper.detach (GH-142706) (GH-142755)
(cherry picked from commit 1d3854a) Signed-off-by: yihong0618 <[email protected]> Co-authored-by: yihong <[email protected]>
1 parent b9cbdde commit 58da2a9

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
@@ -4194,6 +4194,22 @@ def write(self, data):
41944194
self.assertEqual([b"abcdef", b"middle", b"g"*chunk_size],
41954195
buf._write_stack)
41964196

4197+
def test_issue142594(self):
4198+
wrapper = None
4199+
detached = False
4200+
class ReentrantRawIO(self.RawIOBase):
4201+
@property
4202+
def closed(self):
4203+
nonlocal detached
4204+
if wrapper is not None and not detached:
4205+
detached = True
4206+
wrapper.detach()
4207+
return False
4208+
4209+
raw = ReentrantRawIO()
4210+
wrapper = self.TextIOWrapper(raw)
4211+
wrapper.close() # should not crash
4212+
41974213

41984214
class PyTextIOWrapperTest(TextIOWrapperTest):
41994215
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
@@ -3149,6 +3149,9 @@ _io_TextIOWrapper_close_impl(textio *self)
31493149
if (r > 0) {
31503150
Py_RETURN_NONE; /* stream already closed */
31513151
}
3152+
if (self->detached) {
3153+
Py_RETURN_NONE; /* gh-142594 null pointer issue */
3154+
}
31523155
else {
31533156
PyObject *exc = NULL;
31543157
if (self->finalizing) {

0 commit comments

Comments
 (0)