[Python-bugs-list] [Bug #116636] Bug in StringIO.write()
noreply@sourceforge.net
noreply@sourceforge.net
Wed, 11 Oct 2000 12:44:30 -0700
Bug #116636, was updated on 2000-Oct-11 10:53
Here is a current snapshot of the bug.
Project: Python
Category: Library
Status: Open
Resolution: None
Bug Group: None
Priority: 5
Summary: Bug in StringIO.write()
Details: If the file position is less than the end of the "file", and a write is performed extending past then end of the file, the data string is corrupted. 100% reliable failure on all platforms!
This is apparently an old bug (since 1.5.2 at least), but still exists in 2.0c1.
To reproduce:
s = StringIO("abcdef")
s.seek(3)
s.write("uvwxyz") # write data that starts before the end
# of the buffer and extends beyond the end
# (self.len is not properly updated in this case)
s.write("m") # this incorrectly pads the buffer with '\0'
print `s.getvalue()`
The result is:
'abcuvwxyz\000\000\000m'
I fixed my copy by adding the following lines to the StingIO.write method:
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))
self.len = self.pos
newpos = self.pos + len(s)
if self.pos < self.len:
if self.buflist:
self.buf = self.buf + string.joinfields(self.buflist, '')
self.buflist = []
self.buflist = [self.buf[:self.pos], s, self.buf[newpos:]]
self.buf = ''
if self.len < newpos: # ADDED LINE
self.len = newpos # ADDED LINE
else:
self.buflist.append(s)
self.len = newpos
self.pos = newpos
Bob
bobalex@rsv.ricoh.com
For detailed info, follow this link:
http://sourceforge.net/bugs/?func=detailbug&bug_id=116636&group_id=5470