Mercurial > cpython-lru_cache-weakref
diff Lib/StringIO.py @ 9016:b9a070736eac legacy-trunk
Raise the right exception (ValueError) for attempted I/O on closed StringIO
objects; this makes the emulation of file objects a bit better, and the
exceptions explain things a bit better.
| author | Fred Drake <fdrake@acm.org> |
|---|---|
| date | Tue, 18 Aug 1998 17:43:08 +0000 |
| parents | 253377a40644 |
| children | e34991022571 |
line wrap: on
line diff
--- a/Lib/StringIO.py Tue Aug 18 15:31:36 1998 +0000 +++ b/Lib/StringIO.py Tue Aug 18 17:43:08 1998 +0000 @@ -41,8 +41,12 @@ self.closed = 1 del self.buf, self.pos def isatty(self): + if self.closed: + raise ValueError, "I/O operation on closed file" return 0 def seek(self, pos, mode = 0): + if self.closed: + raise ValueError, "I/O operation on closed file" if self.buflist: self.buf = self.buf + string.joinfields(self.buflist, '') self.buflist = [] @@ -52,8 +56,12 @@ pos = pos + self.len self.pos = max(0, pos) def tell(self): + if self.closed: + raise ValueError, "I/O operation on closed file" return self.pos def read(self, n = -1): + if self.closed: + raise ValueError, "I/O operation on closed file" if self.buflist: self.buf = self.buf + string.joinfields(self.buflist, '') self.buflist = [] @@ -65,6 +73,8 @@ self.pos = newpos return r def readline(self, length=None): + if self.closed: + raise ValueError, "I/O operation on closed file" if self.buflist: self.buf = self.buf + string.joinfields(self.buflist, '') self.buflist = [] @@ -87,6 +97,8 @@ line = self.readline() return lines def write(self, s): + if self.closed: + raise ValueError, "I/O operation on closed file" if not s: return if self.pos > self.len: self.buflist.append('\0'*(self.pos - self.len)) @@ -105,7 +117,8 @@ def writelines(self, list): self.write(string.joinfields(list, '')) def flush(self): - pass + if self.closed: + raise ValueError, "I/O operation on closed file" def getvalue(self): if self.buflist: self.buf = self.buf + string.joinfields(self.buflist, '')
