summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFred Drake <fdrake@acm.org>1998-08-18 17:43:08 (GMT)
committerFred Drake <fdrake@acm.org>1998-08-18 17:43:08 (GMT)
commitce4ba897be5e4fc116f7cf805f38dec48cd5cfea (patch)
treead661749674372cd652bca79eefed2583b19cee4
parent71de6c6b747149b79b0d7f5c1192785f045da242 (diff)
downloadcpython-ce4ba897be5e4fc116f7cf805f38dec48cd5cfea.zip
cpython-ce4ba897be5e4fc116f7cf805f38dec48cd5cfea.tar.gz
cpython-ce4ba897be5e4fc116f7cf805f38dec48cd5cfea.tar.bz2
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.
-rw-r--r--Lib/StringIO.py15
1 files changed, 14 insertions, 1 deletions
diff --git a/Lib/StringIO.py b/Lib/StringIO.py
index dba38e4..fc195b9 100644
--- a/Lib/StringIO.py
+++ b/Lib/StringIO.py
@@ -41,8 +41,12 @@ class StringIO:
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 @@ class StringIO:
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 @@ class StringIO:
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 @@ class StringIO:
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 @@ class StringIO:
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, '')