|
1 | 1 | from io import BytesIO |
2 | | -from pathlib import Path |
| 2 | +from unittest.mock import MagicMock |
3 | 3 |
|
4 | 4 | import pytest |
5 | 5 | from docling_core.types.doc import BoundingBox, CoordOrigin |
6 | 6 | from PIL import Image |
7 | 7 |
|
8 | 8 | from docling.backend.image_backend import ImageDocumentBackend, _ImagePageBackend |
9 | 9 | from docling.datamodel.base_models import DocumentStream, InputFormat |
10 | | -from docling.datamodel.document import InputDocument, _DocumentConversionInput |
| 10 | +from docling.datamodel.document import ( |
| 11 | + InputDocument, |
| 12 | + _DocumentConversionInput, |
| 13 | + _DummyBackend, |
| 14 | +) |
11 | 15 | from docling.document_converter import DocumentConverter, ImageFormatOption |
12 | 16 | from docling.document_extractor import DocumentExtractor |
13 | 17 |
|
@@ -216,3 +220,69 @@ def test_multipage_access(): |
216 | 220 | size = page_backend.get_size() |
217 | 221 | assert size.width == 64 |
218 | 222 | assert size.height == 64 |
| 223 | + |
| 224 | + |
| 225 | +def test_source_image_is_closed_after_backend_init(tmp_path, monkeypatch): |
| 226 | + image_path = tmp_path / "test.png" |
| 227 | + Image.new("RGB", (32, 32), (10, 20, 30)).save(image_path) |
| 228 | + |
| 229 | + opened_images = [] |
| 230 | + original_open = Image.open |
| 231 | + |
| 232 | + class TrackingImage: |
| 233 | + def __init__(self, image): |
| 234 | + self._image = image |
| 235 | + self.closed = False |
| 236 | + |
| 237 | + def __getattr__(self, attr): |
| 238 | + return getattr(self._image, attr) |
| 239 | + |
| 240 | + def close(self): |
| 241 | + self.closed = True |
| 242 | + return self._image.close() |
| 243 | + |
| 244 | + def __enter__(self): |
| 245 | + return self |
| 246 | + |
| 247 | + def __exit__(self, exc_type, exc, tb): |
| 248 | + self.close() |
| 249 | + return False |
| 250 | + |
| 251 | + def tracking_open(*args, **kwargs): |
| 252 | + tracked_image = TrackingImage(original_open(*args, **kwargs)) |
| 253 | + opened_images.append(tracked_image) |
| 254 | + return tracked_image |
| 255 | + |
| 256 | + input_doc = InputDocument( |
| 257 | + path_or_stream=image_path, |
| 258 | + format=InputFormat.IMAGE, |
| 259 | + backend=_DummyBackend, |
| 260 | + filename=image_path.name, |
| 261 | + ) |
| 262 | + |
| 263 | + monkeypatch.setattr("docling.backend.image_backend.Image.open", tracking_open) |
| 264 | + backend = ImageDocumentBackend( |
| 265 | + in_doc=input_doc, |
| 266 | + path_or_stream=image_path, |
| 267 | + ) |
| 268 | + |
| 269 | + assert len(opened_images) == 1 |
| 270 | + assert opened_images[0].closed is True |
| 271 | + backend.unload() |
| 272 | + |
| 273 | + |
| 274 | +def test_unload_closes_cached_frames(): |
| 275 | + stream = _make_multipage_tiff_stream(num_pages=3, size=(32, 32)) |
| 276 | + doc_backend = _get_backend_from_stream(stream) |
| 277 | + |
| 278 | + tracked_closers = [] |
| 279 | + for frame in doc_backend._frames: |
| 280 | + closer = MagicMock(wraps=frame.close) |
| 281 | + frame.close = closer |
| 282 | + tracked_closers.append(closer) |
| 283 | + |
| 284 | + doc_backend.unload() |
| 285 | + |
| 286 | + assert doc_backend._frames == [] |
| 287 | + for closer in tracked_closers: |
| 288 | + closer.assert_called_once() |
0 commit comments