diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index 2324c447f..497c4e6e2 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -4211,7 +4211,6 @@ class CTextIOWrapperTest(TextIOWrapperTest): self.assertEqual([b"abcdef", b"middle", b"g"*chunk_size], buf._write_stack) - @unittest.expectedFailure # TODO: RUSTPYTHON; AttributeError: 'NoneType' object has no attribute 'closed' def test_issue142594(self): wrapper = None detached = False diff --git a/crates/vm/src/stdlib/io.rs b/crates/vm/src/stdlib/io.rs index 26d4a67c8..a22642dbf 100644 --- a/crates/vm/src/stdlib/io.rs +++ b/crates/vm/src/stdlib/io.rs @@ -3793,6 +3793,13 @@ mod _io { if file_closed(&buffer, vm)? { return Ok(()); } + // https://github.com/python/cpython/issues/142594 + // The file_closed() check above may have triggered a reentrant + // call to detach() via a custom `closed` property. + // If so, the buffer is now detached and we should return early. + if vm.is_none(&zelf.lock(vm)?.buffer) { + return Ok(()); + } if zelf.finalizing.load(Ordering::Relaxed) { // _dealloc_warn: delegate to buffer._dealloc_warn(source) let _ = vm.call_method(&buffer, "_dealloc_warn", (zelf.as_object().to_owned(),));