mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
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.
This commit is contained in:
parent
71de6c6b74
commit
ce4ba897be
1 changed files with 14 additions and 1 deletions
|
@ -41,8 +41,12 @@ class StringIO:
|
||||||
self.closed = 1
|
self.closed = 1
|
||||||
del self.buf, self.pos
|
del self.buf, self.pos
|
||||||
def isatty(self):
|
def isatty(self):
|
||||||
|
if self.closed:
|
||||||
|
raise ValueError, "I/O operation on closed file"
|
||||||
return 0
|
return 0
|
||||||
def seek(self, pos, mode = 0):
|
def seek(self, pos, mode = 0):
|
||||||
|
if self.closed:
|
||||||
|
raise ValueError, "I/O operation on closed file"
|
||||||
if self.buflist:
|
if self.buflist:
|
||||||
self.buf = self.buf + string.joinfields(self.buflist, '')
|
self.buf = self.buf + string.joinfields(self.buflist, '')
|
||||||
self.buflist = []
|
self.buflist = []
|
||||||
|
@ -52,8 +56,12 @@ class StringIO:
|
||||||
pos = pos + self.len
|
pos = pos + self.len
|
||||||
self.pos = max(0, pos)
|
self.pos = max(0, pos)
|
||||||
def tell(self):
|
def tell(self):
|
||||||
|
if self.closed:
|
||||||
|
raise ValueError, "I/O operation on closed file"
|
||||||
return self.pos
|
return self.pos
|
||||||
def read(self, n = -1):
|
def read(self, n = -1):
|
||||||
|
if self.closed:
|
||||||
|
raise ValueError, "I/O operation on closed file"
|
||||||
if self.buflist:
|
if self.buflist:
|
||||||
self.buf = self.buf + string.joinfields(self.buflist, '')
|
self.buf = self.buf + string.joinfields(self.buflist, '')
|
||||||
self.buflist = []
|
self.buflist = []
|
||||||
|
@ -65,6 +73,8 @@ class StringIO:
|
||||||
self.pos = newpos
|
self.pos = newpos
|
||||||
return r
|
return r
|
||||||
def readline(self, length=None):
|
def readline(self, length=None):
|
||||||
|
if self.closed:
|
||||||
|
raise ValueError, "I/O operation on closed file"
|
||||||
if self.buflist:
|
if self.buflist:
|
||||||
self.buf = self.buf + string.joinfields(self.buflist, '')
|
self.buf = self.buf + string.joinfields(self.buflist, '')
|
||||||
self.buflist = []
|
self.buflist = []
|
||||||
|
@ -87,6 +97,8 @@ class StringIO:
|
||||||
line = self.readline()
|
line = self.readline()
|
||||||
return lines
|
return lines
|
||||||
def write(self, s):
|
def write(self, s):
|
||||||
|
if self.closed:
|
||||||
|
raise ValueError, "I/O operation on closed file"
|
||||||
if not s: return
|
if not s: return
|
||||||
if self.pos > self.len:
|
if self.pos > self.len:
|
||||||
self.buflist.append('\0'*(self.pos - self.len))
|
self.buflist.append('\0'*(self.pos - self.len))
|
||||||
|
@ -105,7 +117,8 @@ class StringIO:
|
||||||
def writelines(self, list):
|
def writelines(self, list):
|
||||||
self.write(string.joinfields(list, ''))
|
self.write(string.joinfields(list, ''))
|
||||||
def flush(self):
|
def flush(self):
|
||||||
pass
|
if self.closed:
|
||||||
|
raise ValueError, "I/O operation on closed file"
|
||||||
def getvalue(self):
|
def getvalue(self):
|
||||||
if self.buflist:
|
if self.buflist:
|
||||||
self.buf = self.buf + string.joinfields(self.buflist, '')
|
self.buf = self.buf + string.joinfields(self.buflist, '')
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue