Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Lib/test/test_io/test_textio.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ def test_detach(self):
self.assertFalse(t.line_buffering)
self.assertFalse(t.write_through)

# The attribute is None once detached, not an error (gh-160427).
self.assertIsNone(t.buffer)
self.assertTrue(hasattr(t, "buffer"))

def test_repr(self):
raw = self.BytesIO("hello".encode("utf-8"))
b = self.BufferedReader(raw)
Expand Down Expand Up @@ -1710,9 +1714,9 @@ def readinto(self, b):
self.BufferedReader(raw), encoding="utf-8")
method = getattr(wrapper, method_name)
self.assertEqual(method(), "ab\n")
with self.assertRaisesRegex(ValueError,
"underlying buffer has been detached"):
getattr(wrapper, 'buffer')
# gh-160427: once detached the attribute is None, not an
# error; the read above still had to complete safely.
self.assertIsNone(wrapper.buffer)

def test_reentrant_seek_during_tell(self):
# gh-153539: reading short of _CHUNK_SIZE leaves residual bytes in the
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix ``io.TextIOWrapper.buffer`` to return ``None`` after the wrapper has been
detached, instead of raising :exc:`ValueError`. This restores the behaviour of
Python 3.14.8 and earlier, and stops :func:`hasattr` from raising when called on
a detached wrapper.
14 changes: 13 additions & 1 deletion Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -3467,7 +3467,19 @@ static PyObject *
_io_TextIOWrapper_buffer_get_impl(textio *self)
/*[clinic end generated code: output=d265a34555aa5d4b input=5951cfa148f7350a]*/
{
return buffer_access_safe(self);
if (self->buffer == NULL) {
if (self->ok <= 0) {
PyErr_SetString(PyExc_ValueError,
"I/O operation on uninitialized object");
return NULL;
}
/* Detached: the attribute is None, as it was while it was a member
descriptor (gh-160427). Raising here broke hasattr() in 3.14.9. */
Py_RETURN_NONE;
}

_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);
return Py_NewRef(self->buffer);
}

static PyMethodDef incrementalnewlinedecoder_methods[] = {
Expand Down
Loading