summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Lib/_pyio.py20
-rw-r--r--Lib/test/test_io.py13
-rw-r--r--Misc/NEWS2
-rw-r--r--Modules/_io/bufferedio.c6
4 files changed, 32 insertions, 9 deletions
diff --git a/Lib/_pyio.py b/Lib/_pyio.py
index 87c833c..2a6e7a8 100644
--- a/Lib/_pyio.py
+++ b/Lib/_pyio.py
@@ -676,7 +676,7 @@ class _BufferedIOMixin(BufferedIOBase):
"""
def __init__(self, raw):
- self.raw = raw
+ self._raw = raw
### Positioning ###
@@ -720,8 +720,8 @@ class _BufferedIOMixin(BufferedIOBase):
if self.raw is None:
raise ValueError("raw stream already detached")
self.flush()
- raw = self.raw
- self.raw = None
+ raw = self._raw
+ self._raw = None
return raw
### Inquiries ###
@@ -736,6 +736,10 @@ class _BufferedIOMixin(BufferedIOBase):
return self.raw.writable()
@property
+ def raw(self):
+ return self._raw
+
+ @property
def closed(self):
return self.raw.closed
@@ -1465,7 +1469,7 @@ class TextIOWrapper(TextIOBase):
if not isinstance(errors, str):
raise ValueError("invalid errors: %r" % errors)
- self.buffer = buffer
+ self._buffer = buffer
self._line_buffering = line_buffering
self._encoding = encoding
self._errors = errors
@@ -1520,6 +1524,10 @@ class TextIOWrapper(TextIOBase):
def line_buffering(self):
return self._line_buffering
+ @property
+ def buffer(self):
+ return self._buffer
+
def seekable(self):
return self._seekable
@@ -1734,8 +1742,8 @@ class TextIOWrapper(TextIOBase):
if self.buffer is None:
raise ValueError("buffer is already detached")
self.flush()
- buffer = self.buffer
- self.buffer = None
+ buffer = self._buffer
+ self._buffer = None
return buffer
def seek(self, cookie, whence=0):
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index a17fcee..ee4e42f 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -730,6 +730,13 @@ class CommonBufferedTests:
self.assertRaises(self.UnsupportedOperation, bufio.tell)
self.assertRaises(self.UnsupportedOperation, bufio.seek, 0)
+ def test_readonly_attributes(self):
+ raw = self.MockRawIO()
+ buf = self.tp(raw)
+ x = self.MockRawIO()
+ with self.assertRaises(AttributeError):
+ buf.raw = x
+
class BufferedReaderTest(unittest.TestCase, CommonBufferedTests):
read_mode = "rb"
@@ -2245,6 +2252,12 @@ class TextIOWrapperTest(unittest.TestCase):
self.assertRaises(self.UnsupportedOperation, txt.tell)
self.assertRaises(self.UnsupportedOperation, txt.seek, 0)
+ def test_readonly_attributes(self):
+ txt = self.TextIOWrapper(self.BytesIO(self.testdata), encoding="ascii")
+ buf = self.BytesIO(self.testdata)
+ with self.assertRaises(AttributeError):
+ txt.buffer = buf
+
class CTextIOWrapperTest(TextIOWrapperTest):
def test_initialization(self):
diff --git a/Misc/NEWS b/Misc/NEWS
index ea4e52d..12a5a0a 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -11,6 +11,8 @@ Core and Builtins
Library
-------
+- Issue #10750: The ``raw`` attribute of buffered IO objects is now read-only.
+
- Deprecated assertDictContainsSubclass() in the unittest module.
diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c
index f7e34a8..55c0a23 100644
--- a/Modules/_io/bufferedio.c
+++ b/Modules/_io/bufferedio.c
@@ -1541,7 +1541,7 @@ static PyMethodDef bufferedreader_methods[] = {
};
static PyMemberDef bufferedreader_members[] = {
- {"raw", T_OBJECT, offsetof(buffered, raw), 0},
+ {"raw", T_OBJECT, offsetof(buffered, raw), READONLY},
{NULL}
};
@@ -1926,7 +1926,7 @@ static PyMethodDef bufferedwriter_methods[] = {
};
static PyMemberDef bufferedwriter_members[] = {
- {"raw", T_OBJECT, offsetof(buffered, raw), 0},
+ {"raw", T_OBJECT, offsetof(buffered, raw), READONLY},
{NULL}
};
@@ -2320,7 +2320,7 @@ static PyMethodDef bufferedrandom_methods[] = {
};
static PyMemberDef bufferedrandom_members[] = {
- {"raw", T_OBJECT, offsetof(buffered, raw), 0},
+ {"raw", T_OBJECT, offsetof(buffered, raw), READONLY},
{NULL}
};