summaryrefslogtreecommitdiffstats
path: root/Lib/_pyio.py
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2009-05-01 20:40:59 (GMT)
committerBenjamin Peterson <benjamin@python.org>2009-05-01 20:40:59 (GMT)
commitd2e0c7955f972c8477260e8d9799dd45acd4607b (patch)
tree1b6ac51c7efa6425c0da52b22ad88163029935b3 /Lib/_pyio.py
parent155374d95d8ecd235d3a3edd92dd6f6a23d59f11 (diff)
downloadcpython-d2e0c7955f972c8477260e8d9799dd45acd4607b.zip
cpython-d2e0c7955f972c8477260e8d9799dd45acd4607b.tar.gz
cpython-d2e0c7955f972c8477260e8d9799dd45acd4607b.tar.bz2
implement a detach() method for BufferedIOBase and TextIOBase #5883
Diffstat (limited to 'Lib/_pyio.py')
-rw-r--r--Lib/_pyio.py51
1 files changed, 45 insertions, 6 deletions
diff --git a/Lib/_pyio.py b/Lib/_pyio.py
index e580366..e3e7c3d 100644
--- a/Lib/_pyio.py
+++ b/Lib/_pyio.py
@@ -642,6 +642,15 @@ class BufferedIOBase(IOBase):
"""
self._unsupported("write")
+ def detach(self) -> None:
+ """
+ Separate the underlying raw stream from the buffer and return it.
+
+ After the raw stream has been detached, the buffer is in an unusable
+ state.
+ """
+ self._unsupported("detach")
+
io.BufferedIOBase.register(BufferedIOBase)
@@ -689,13 +698,21 @@ class _BufferedIOMixin(BufferedIOBase):
self.raw.flush()
def close(self):
- if not self.closed:
+ if not self.closed and self.raw is not None:
try:
self.flush()
except IOError:
pass # If flush() fails, just give up
self.raw.close()
+ def detach(self):
+ if self.raw is None:
+ raise ValueError("raw stream already detached")
+ self.flush()
+ raw = self.raw
+ self.raw = None
+ return raw
+
### Inquiries ###
def seekable(self):
@@ -1236,6 +1253,15 @@ class TextIOBase(IOBase):
"""
self._unsupported("readline")
+ def detach(self) -> None:
+ """
+ Separate the underlying buffer from the TextIOBase and return it.
+
+ After the underlying buffer has been detached, the TextIO is in an
+ unusable state.
+ """
+ self._unsupported("detach")
+
@property
def encoding(self):
"""Subclasses should override."""
@@ -1448,11 +1474,12 @@ class TextIOWrapper(TextIOBase):
self._telling = self._seekable
def close(self):
- try:
- self.flush()
- except IOError:
- pass # If flush() fails, just give up
- self.buffer.close()
+ if self.buffer is not None:
+ try:
+ self.flush()
+ except IOError:
+ pass # If flush() fails, just give up
+ self.buffer.close()
@property
def closed(self):
@@ -1647,6 +1674,14 @@ class TextIOWrapper(TextIOBase):
self.seek(pos)
return self.buffer.truncate()
+ def detach(self):
+ if self.buffer is None:
+ raise ValueError("buffer is already detached")
+ self.flush()
+ buffer = self.buffer
+ self.buffer = None
+ return buffer
+
def seek(self, cookie, whence=0):
if self.closed:
raise ValueError("tell on closed file")
@@ -1865,3 +1900,7 @@ class StringIO(TextIOWrapper):
@property
def encoding(self):
return None
+
+ def detach(self):
+ # This doesn't make sense on StringIO.
+ self._unsupported("detach")