Skip to content
This repository has been archived by the owner on Feb 25, 2018. It is now read-only.

Commit

Permalink
Buffer wrapper: work around StringIO not truncating past EOF
Browse files Browse the repository at this point in the history
  • Loading branch information
rfk committed Nov 10, 2010
1 parent dea35a8 commit 17463a5
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion filelike/wrappers/buffer.py
Expand Up @@ -174,8 +174,17 @@ def _truncate(self,size):
self._buffer._file.truncate(size)
except Exception:
raise et,ev,tb
# StringIO objects don't truncate to larger size correctly.
if hasattr(self._buffer,"_file"):
_file = self._buffer._file
if hasattr(_file,"getvalue"):
if len(_file.getvalue()) != size:
curpos = _file.tell()
_file.seek(0,2)
_file.write("\x00" * (size - len(_file.getvalue())))
_file.seek(curpos)
self._was_truncated = True

def _read_rest(self):
"""Read the rest of the input stream."""
if self._in_eof:
Expand Down

0 comments on commit 17463a5

Please sign in to comment.