Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cycode/cli/files_collector/models/in_memory_zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ def close(self) -> None:
self.zip.close()

def cleanup(self) -> None:
"""Release the buffer, deleting the spilled temp file if there is one."""
"""Close the archive and release the buffer, deleting the spilled temp file if there is one."""
self.zip.close()
self._buffer.close()

def __enter__(self) -> 'InMemoryZip': # noqa: PYI034 # typing.Self needs Python 3.11
Expand Down
18 changes: 18 additions & 0 deletions tests/cli/files_collector/test_in_memory_zip.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Tests for InMemoryZip class, specifically for handling surrogate characters and encoding issues."""

import gc
import sys
import zipfile
from io import BytesIO
from pathlib import Path
Expand Down Expand Up @@ -158,6 +160,22 @@ def test_cleanup_is_idempotent() -> None:
zip_file.cleanup()


def test_cleanup_closes_an_archive_that_was_never_closed(monkeypatch: 'MonkeyPatch') -> None:
"""A commit range with nothing to scan releases its placeholder archive without closing it first.

ZipFile.__del__ must not then try to finalize the archive into the released buffer.
"""
unraisable_exceptions = []
monkeypatch.setattr(sys, 'unraisablehook', unraisable_exceptions.append)

zip_file = InMemoryZip()
zip_file.cleanup()
del zip_file
gc.collect()

assert unraisable_exceptions == []


def test_used_as_a_context_manager() -> None:
zip_file = InMemoryZip()
with zip_file as managed:
Expand Down
Loading