'Write any pending changes to disk.'

def flush(self):

    if (not self._pending):
        if self._pending_sync:
            _sync_flush(self._file)
            self._pending_sync = False
        return
    assert (self._toc is not None)
    self._file.seek(0, 2)
    cur_len = self._file.tell()
    if (cur_len != self._file_length):
        raise ExternalClashError(('Size    of    mailbox    file    changed    (expected    %i,    found    %i)' % (self._file_length, cur_len)))
    new_file = _create_temporary(self._path)
    try:
        new_toc = {}
        self._pre_mailbox_hook(new_file)
        for key in sorted(self._toc.keys()):
            (start, stop) = self._toc[key]
            self._file.seek(start)
            self._pre_message_hook(new_file)
            new_start = new_file.tell()
            while True:
                buffer = self._file.read(min(4096, (stop - self._file.tell())))
                if (buffer == ''):
                    break
                new_file.write(buffer)
            new_toc[key] = (new_start, new_file.tell())
            self._post_message_hook(new_file)
        self._file_length = new_file.tell()
    except:
        new_file.close()
        os.remove(new_file.name)
        raise
    _sync_close(new_file)
    self._file.close()
    mode = os.stat(self._path).st_mode
    os.chmod(new_file.name, mode)
    try:
        os.rename(new_file.name, self._path)
    except OSError as e:
        if ((e.errno == errno.EEXIST) or ((os.name == 'os2') and (e.errno == errno.EACCES))):
            os.remove(self._path)
            os.rename(new_file.name, self._path)
        else:
            raise
    self._file = open(self._path, 'rb+')
    self._toc = new_toc
    self._pending = False
    self._pending_sync = False
    if self._locked:
        _lock_file(self._file, dotlock=False)
