[Python-checkins] CVS: python/dist/src/Lib StringIO.py,1.10,1.11
Fred L. Drake
python-dev@python.org
Wed, 27 Sep 2000 21:21:08 -0700
Update of /cvsroot/python/python/dist/src/Lib
In directory slayer.i.sourceforge.net:/tmp/cvs-serv22772/Lib
Modified Files:
StringIO.py
Log Message:
Add truncate() method to StringIO objects.
This closes SourceForge bug #115527.
Index: StringIO.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/StringIO.py,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -r1.10 -r1.11
*** StringIO.py 2000/09/19 11:07:44 1.10
--- StringIO.py 2000/09/28 04:21:06 1.11
***************
*** 14,17 ****
--- 14,18 ----
buf = f.readline() # read until end of line ('\n') or EOF
list = f.readlines()# list of f.readline() results until EOF
+ f.truncate([size]) # truncate file at to at most size (default: current pos)
f.write(buf) # write at current position
f.writelines(list) # for line in list: f.write(line)
***************
*** 29,32 ****
--- 30,34 ----
"""
+ import errno
import string
***************
*** 103,106 ****
--- 105,119 ----
line = self.readline()
return lines
+ def truncate(self, size=None):
+ if self.closed:
+ raise ValueError, "I/O operation on closed file"
+ if size is None:
+ size = self.pos
+ elif size < 0:
+ raise IOError(errno.EINVAL,
+ "Negative size not allowed")
+ elif size < self.pos:
+ self.pos = size
+ self.buf = self.getvalue()[:size]
def write(self, s):
if self.closed: