diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2010-12-21 21:20:59 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2010-12-21 21:20:59 (GMT) |
commit | 7f8f41808b82b5e4812bc2e2d484a6fc8f02295c (patch) | |
tree | d04b34c9a8897cfdc8fce780f002b15b97502e12 /Lib | |
parent | cfee0e83ebd8826dbdc89035d15e1ea90a577fa2 (diff) | |
download | cpython-7f8f41808b82b5e4812bc2e2d484a6fc8f02295c.zip cpython-7f8f41808b82b5e4812bc2e2d484a6fc8f02295c.tar.gz cpython-7f8f41808b82b5e4812bc2e2d484a6fc8f02295c.tar.bz2 |
Issue #10750: The `raw` attribute of buffered IO objects is now read-only.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/_pyio.py | 20 | ||||
-rw-r--r-- | Lib/test/test_io.py | 13 |
2 files changed, 27 insertions, 6 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): |