Issue #10750: The raw attribute of buffered IO objects is now read-only.

This commit is contained in:
Antoine Pitrou 2010-12-21 21:20:59 +00:00
parent cfee0e83eb
commit 7f8f41808b
4 changed files with 32 additions and 9 deletions

View File

@ -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 ###
@ -735,6 +735,10 @@ class _BufferedIOMixin(BufferedIOBase):
def writable(self):
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):

View File

@ -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):

View File

@ -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.

View File

@ -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}
};