diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2010-09-05 23:01:12 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2010-09-05 23:01:12 (GMT) |
commit | 0d739d70471cafdea04d9624cbfb7895b7d1b566 (patch) | |
tree | c8a04ae9f5b648613d724b4497c1fac3986f48fd /Lib/_pyio.py | |
parent | bad092556e12e8b2cf5976718cda902bef66b3f4 (diff) | |
download | cpython-0d739d70471cafdea04d9624cbfb7895b7d1b566.zip cpython-0d739d70471cafdea04d9624cbfb7895b7d1b566.tar.gz cpython-0d739d70471cafdea04d9624cbfb7895b7d1b566.tar.bz2 |
Issue #9293: I/O streams now raise `io.UnsupportedOperation` when an
unsupported operation is attempted (for example, writing to a file open
only for reading).
Diffstat (limited to 'Lib/_pyio.py')
-rw-r--r-- | Lib/_pyio.py | 30 |
1 files changed, 17 insertions, 13 deletions
diff --git a/Lib/_pyio.py b/Lib/_pyio.py index 66f990d..12ae4b6 100644 --- a/Lib/_pyio.py +++ b/Lib/_pyio.py @@ -243,8 +243,13 @@ class OpenWrapper: return open(*args, **kwargs) -class UnsupportedOperation(ValueError, IOError): - pass +# In normal operation, both `UnsupportedOperation`s should be bound to the +# same object. +try: + UnsupportedOperation = io.UnsupportedOperation +except AttributeError: + class UnsupportedOperation(ValueError, IOError): + pass class IOBase(metaclass=abc.ABCMeta): @@ -362,9 +367,8 @@ class IOBase(metaclass=abc.ABCMeta): """Internal: raise an IOError if file is not seekable """ if not self.seekable(): - raise IOError("File or stream is not seekable." - if msg is None else msg) - + raise UnsupportedOperation("File or stream is not seekable." + if msg is None else msg) def readable(self) -> bool: """Return whether object was opened for reading. @@ -377,8 +381,8 @@ class IOBase(metaclass=abc.ABCMeta): """Internal: raise an IOError if file is not readable """ if not self.readable(): - raise IOError("File or stream is not readable." - if msg is None else msg) + raise UnsupportedOperation("File or stream is not readable." + if msg is None else msg) def writable(self) -> bool: """Return whether object was opened for writing. @@ -391,8 +395,8 @@ class IOBase(metaclass=abc.ABCMeta): """Internal: raise an IOError if file is not writable """ if not self.writable(): - raise IOError("File or stream is not writable." - if msg is None else msg) + raise UnsupportedOperation("File or stream is not writable." + if msg is None else msg) @property def closed(self): @@ -1647,7 +1651,7 @@ class TextIOWrapper(TextIOBase): def tell(self): if not self._seekable: - raise IOError("underlying stream is not seekable") + raise UnsupportedOperation("underlying stream is not seekable") if not self._telling: raise IOError("telling position disabled by next() call") self.flush() @@ -1726,17 +1730,17 @@ class TextIOWrapper(TextIOBase): if self.closed: raise ValueError("tell on closed file") if not self._seekable: - raise IOError("underlying stream is not seekable") + raise UnsupportedOperation("underlying stream is not seekable") if whence == 1: # seek relative to current position if cookie != 0: - raise IOError("can't do nonzero cur-relative seeks") + raise UnsupportedOperation("can't do nonzero cur-relative seeks") # Seeking to the current position should attempt to # sync the underlying buffer with the current position. whence = 0 cookie = self.tell() if whence == 2: # seek relative to end of file if cookie != 0: - raise IOError("can't do nonzero end-relative seeks") + raise UnsupportedOperation("can't do nonzero end-relative seeks") self.flush() position = self.buffer.seek(0, 2) self._set_decoded_chars('') |