]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-142594: fix by property calls io.TextIOWrapper.detach (GH-142706)
authoryihong <zouzou0208@gmail.com>
Mon, 15 Dec 2025 14:13:58 +0000 (22:13 +0800)
committerGitHub <noreply@github.com>
Mon, 15 Dec 2025 14:13:58 +0000 (15:13 +0100)
Signed-off-by: yihong0618 <zouzou0208@gmail.com>
Lib/test/test_io/test_textio.py
Misc/NEWS.d/next/Library/2025-12-14-18-30-48.gh-issue-142594.belDmD.rst [new file with mode: 0644]
Modules/_io/textio.c

index 6331ed2b958552432397cde5e131fd2a4ac94675..d725f9212ceaaef718045d5e2bc78df58229131b 100644 (file)
@@ -1544,6 +1544,22 @@ class CTextIOWrapperTest(TextIOWrapperTest, CTestCase):
         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"
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 (file)
index 0000000..ee6a958
--- /dev/null
@@ -0,0 +1,2 @@
+Fix crash in ``TextIOWrapper.close()`` when the underlying buffer's
+``closed`` property calls :meth:`~io.TextIOBase.detach`.
index 65da300abcf3bc13906e1275a4835c366b2be8a2..f9881952561292e2d829a3752485c8703c19d69d 100644 (file)
@@ -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) {