https://github.com/python/cpython/commit/affd2f2ff24f45e672adf3638ab5e8dafc76c0e7
commit: affd2f2ff24f45e672adf3638ab5e8dafc76c0e7
branch: 3.13
author: Cody Maloney <[email protected]>
committer: encukou <[email protected]>
date: 2025-12-16T10:55:35+01:00
summary:

[3.13] gh-142594: fix by property calls io.TextIOWrapper.detach (GH-142706) 
(GH-142757)

(cherry picked from commit 1d3854a19a376c1fc7f71e96c620f6bc2de8cd74)

Signed-off-by: yihong0618 <[email protected]>
Co-authored-by: yihong <[email protected]>

files:
A Misc/NEWS.d/next/Library/2025-12-14-18-30-48.gh-issue-142594.belDmD.rst
M Lib/test/test_io.py
M Modules/_io/textio.c

diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index fab1a0d4e51e51..77b9b70d00f5ad 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -4160,6 +4160,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
diff --git 
a/Misc/NEWS.d/next/Library/2025-12-14-18-30-48.gh-issue-142594.belDmD.rst 
b/Misc/NEWS.d/next/Library/2025-12-14-18-30-48.gh-issue-142594.belDmD.rst
new file mode 100644
index 00000000000000..ee6a958933f7c8
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-12-14-18-30-48.gh-issue-142594.belDmD.rst
@@ -0,0 +1,2 @@
+Fix crash in ``TextIOWrapper.close()`` when the underlying buffer's
+``closed`` property calls :meth:`~io.TextIOBase.detach`.
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c
index 047d0fdfc76770..db6c709c00cd22 100644
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -3133,6 +3133,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) {

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to