'Truncate the file\'s size.
If the optional size argument is present, the file is truncated to
(at most) that size. The size defaults to the current position.
The current file position is not changed unless the position
is beyond the new file size.
If the specified size exceeds the file\'s current size, the
file remains unchanged.'

def truncate(self, size=None):

    _complain_ifclosed(self.closed)
    if (size is None):
        size = self.pos
    elif (size < 0):
        raise IOError(EINVAL, 'Negative    size    not    allowed')
    elif (size < self.pos):
        self.pos = size
    self.buf = self.getvalue()[:size]
    self.len = size
    return
