Skip to content
Open
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
6 changes: 3 additions & 3 deletions Lib/test/test_io/test_textio.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ def test_detach(self):
r = self.BytesIO()
b = self.BufferedWriter(r)
t = self.TextIOWrapper(b, encoding="ascii")
self.assertIs(t.buffer, b)
self.assertIs(t.detach(), b)

t = self.TextIOWrapper(b, encoding="ascii")
Expand All @@ -259,6 +260,7 @@ def test_detach(self):
self.assertEqual(t.errors, "strict")
self.assertFalse(t.line_buffering)
self.assertFalse(t.write_through)
self.assertIsNone(t.buffer)

def test_repr(self):
raw = self.BytesIO("hello".encode("utf-8"))
Expand Down Expand Up @@ -1721,9 +1723,7 @@ 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')
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,2 @@
Fix :attr:`~io.TextIOBase.buffer` of a detached :class:`io.TextIOWrapper`
raising :exc:`ValueError` instead of returning ``None``.
6 changes: 5 additions & 1 deletion Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -3474,7 +3474,11 @@ static PyObject *
_io_TextIOWrapper_buffer_get_impl(textio *self)
/*[clinic end generated code: output=d265a34555aa5d4b input=5951cfa148f7350a]*/
{
return buffer_access_safe(self);
/* gh-160427: None once detached, matching the former member descriptor. */
if (self->buffer == NULL) {
Py_RETURN_NONE;
}
return Py_NewRef(self->buffer);
}

static PyMethodDef incrementalnewlinedecoder_methods[] = {
Expand Down
Loading