diff --git a/asdf/_asdf.py b/asdf/_asdf.py index b09b4f8d1..1691f2783 100644 --- a/asdf/_asdf.py +++ b/asdf/_asdf.py @@ -1070,9 +1070,9 @@ def update( def rewrite(): self._fd.seek(0) self._serial_write(self._fd, pad_blocks, include_block_index) + self._fd.truncate() if self._fd.can_memmap(): self._fd.close_memmap() - self._fd.truncate() # if we have no read blocks, we can just call write_to as no internal blocks are reused if len(self._blocks.blocks) == 0: @@ -1107,9 +1107,9 @@ def rewrite(): # close memmap to trigger arrays to reload themselves self._fd.seek(end_of_file) + self._fd.truncate() if self._fd.can_memmap(): self._fd.close_memmap() - self._fd.truncate() finally: self._post_write(fd) diff --git a/asdf/generic_io.py b/asdf/generic_io.py index 76a3b9157..5edc6b44d 100644 --- a/asdf/generic_io.py +++ b/asdf/generic_io.py @@ -738,9 +738,6 @@ def fast_forward(self, size): self.seek(size, SEEK_CUR) def truncate(self, size=None): - # windows supports truncating as long as the file not opened - # more than once. So this must be called after closing all - # memmaps if size is None: self._fd.truncate() else: @@ -837,6 +834,15 @@ def close(self): if self._close: self._fix_permissions() + def truncate(self, size=None): + # windows supports truncating as long as the file not opened + # more than once. So this must be called after closing all + # memmaps + if sys.platform.startswith("win") and hasattr(self, "_mmap"): + self._mmap.close() + self.close_memmap() + super().truncate(size=size) + class MemoryIO(RandomAccessFile): """