diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2015-01-15 08:33:50 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2015-01-15 08:33:50 (GMT) |
commit | e7a35717d29cf0b070c814a7a43b44966bf89f73 (patch) | |
tree | b1a49b3c1f4913079298561545f7efc1e4765ef6 /Lib/asyncio/streams.py | |
parent | caa12dab690aace565a53d17bfed1eea9ed5d9cd (diff) | |
download | cpython-e7a35717d29cf0b070c814a7a43b44966bf89f73.zip cpython-e7a35717d29cf0b070c814a7a43b44966bf89f73.tar.gz cpython-e7a35717d29cf0b070c814a7a43b44966bf89f73.tar.bz2 |
StreamWriter: close() now clears the reference to the transport
StreamWriter now raises an exception if it is closed: write(), writelines(),
write_eof(), can_write_eof(), get_extra_info(), drain().
Diffstat (limited to 'Lib/asyncio/streams.py')
-rw-r--r-- | Lib/asyncio/streams.py | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/Lib/asyncio/streams.py b/Lib/asyncio/streams.py index 7ff16a4..12ab1c5 100644 --- a/Lib/asyncio/streams.py +++ b/Lib/asyncio/streams.py @@ -258,8 +258,22 @@ class StreamWriter: self._reader = reader self._loop = loop + def close(self): + if self._transport is None: + return + self._transport.close() + self._transport = None + + def _check_closed(self): + if self._transport is None: + raise RuntimeError('StreamWriter is closed') + def __repr__(self): - info = [self.__class__.__name__, 'transport=%r' % self._transport] + info = [self.__class__.__name__] + if self._transport is not None: + info.append('transport=%r' % self._transport) + else: + info.append('closed') if self._reader is not None: info.append('reader=%r' % self._reader) return '<%s>' % ' '.join(info) @@ -269,21 +283,23 @@ class StreamWriter: return self._transport def write(self, data): + self._check_closed() self._transport.write(data) def writelines(self, data): + self._check_closed() self._transport.writelines(data) def write_eof(self): + self._check_closed() return self._transport.write_eof() def can_write_eof(self): + self._check_closed() return self._transport.can_write_eof() - def close(self): - return self._transport.close() - def get_extra_info(self, name, default=None): + self._check_closed() return self._transport.get_extra_info(name, default) @coroutine @@ -295,6 +311,7 @@ class StreamWriter: w.write(data) yield from w.drain() """ + self._check_closed() if self._reader is not None: exc = self._reader.exception() if exc is not None: |