From 1d0eb616ed934116c5670c5fcd40ff14c4efb210 Mon Sep 17 00:00:00 2001 From: gouyu-hou Date: Mon, 23 Nov 2026 17:08:02 +0800 Subject: [PATCH] fix(docx): collect temporary images by their real temp path, not /tmp The multiprocess path saves each picture through tempfile.mkdtemp(prefix= "docx_img_"), then recognises its own files with a literal POSIX prefix ("/tmp/docx_img_") in _process_batch_results and again in _cleanup_temp_image_files. On Windows mkdtemp returns C://Users//...//Temp//docx_img_xxxx, so the predicate is False for every image the parser just wrote: nothing is collected and every multimodal docx parse leaves its PNGs and their directories behind. Decide from the directory name instead, which is identical on every platform, and keep one predicate for both call sites. --- docreader/parser/docx_parser.py | 32 ++++++++++++++++---- docreader/tests/test_docx_temp_images.py | 38 ++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 docreader/tests/test_docx_temp_images.py diff --git a/docreader/parser/docx_parser.py b/docreader/parser/docx_parser.py index 463fc4fc538..d6ff170f780 100644 --- a/docreader/parser/docx_parser.py +++ b/docreader/parser/docx_parser.py @@ -965,8 +965,8 @@ def _collect_process_results(self, future_to_idx, args_list, batch_start_time): # Collect temporary image paths for later cleanup for line in page_lines: for image_data in line.images: - if image_data.local_path and image_data.local_path.startswith( - "/tmp/docx_img_" + if image_data.local_path and _is_temp_image_file( + image_data.local_path ): temp_img_paths.add(image_data.local_path) @@ -1139,9 +1139,7 @@ def _cleanup_temp_image_files(self, temp_paths): # Delete temporary directory (if empty) try: temp_dir = os.path.dirname(path) - if temp_dir.startswith("/tmp/docx_img_") and os.path.exists( - temp_dir - ): + if _is_temp_image_dir(temp_dir) and os.path.exists(temp_dir): os.rmdir(temp_dir) except OSError: # If directory is not empty, ignore error @@ -1261,6 +1259,30 @@ def _safe_concat_images(self, images): return images[0] +# ``_save_image_to_temp`` gives every image its own ``tempfile.mkdtemp`` +# directory. Cleanup used to recognise them by the literal POSIX prefix +# ``/tmp/docx_img_``, which never matches on Windows: ``mkdtemp`` there returns +# ``C:\Users\...\Temp\docx_img_xxxx``, so no path was collected and every +# multimodal parse left its PNGs (and their directories) behind. +_DOCX_IMAGE_TEMP_PREFIX = "docx_img_" + + +def _is_temp_image_dir(path): + """Whether *path* is one of the directories ``_save_image_to_temp`` made.""" + if not path: + return False + return os.path.basename(os.path.normpath(path)).startswith( + _DOCX_IMAGE_TEMP_PREFIX + ) + + +def _is_temp_image_file(path): + """Whether *path* is an image written by ``_save_image_to_temp``.""" + if not path: + return False + return _is_temp_image_dir(os.path.dirname(path)) + + def _save_image_to_temp(logger, image, page_num, img_idx): """Save image to a temporary file to pass between processes diff --git a/docreader/tests/test_docx_temp_images.py b/docreader/tests/test_docx_temp_images.py new file mode 100644 index 00000000000..1af29d25392 --- /dev/null +++ b/docreader/tests/test_docx_temp_images.py @@ -0,0 +1,38 @@ +import logging +import os +import unittest + +from PIL import Image + +from docreader.parser.docx_parser import ( + Docx, + _is_temp_image_file, + _save_image_to_temp, +) + + +class TempImageCleanupTest(unittest.TestCase): + def test_an_image_saved_for_a_worker_is_collected_and_removed(self): + logger = logging.getLogger("docx-temp-image-test") + path = _save_image_to_temp(logger, Image.new("RGB", (4, 4)), 1, 0) + self.assertIsNotNone(path) + self.addCleanup(lambda: os.path.exists(path) and os.unlink(path)) + self.assertTrue(os.path.exists(path)) + + # The collector recognised temporary images by the literal POSIX + # prefix "/tmp/docx_img_", which never matches a Windows temp path. + self.assertTrue(_is_temp_image_file(path)) + + Docx()._cleanup_temp_image_files({path}) + + self.assertFalse(os.path.exists(path)) + self.assertFalse(os.path.exists(os.path.dirname(path))) + + def test_a_path_outside_the_image_temp_dirs_is_not_collected(self): + self.assertFalse(_is_temp_image_file(None)) + self.assertFalse(_is_temp_image_file("")) + self.assertFalse(_is_temp_image_file(os.path.join("some", "dir", "a.png"))) + + +if __name__ == "__main__": + unittest.main()