fix(high_level): close pymupdf Document handles to prevent file lock …#597
Open
BlackSharkJ wants to merge 2 commits into
Open
fix(high_level): close pymupdf Document handles to prevent file lock …#597BlackSharkJ wants to merge 2 commits into
BlackSharkJ wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
1 issue found across 1 file
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
fix(high_level): This bug was introduced by awwaawwa in commit 25205ab (2025-06-20). Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Title
[PR] fix: close pymupdf Document handles to prevent file lock on Windows
Related Issue(s)
N/A
Motivation and Context
When using BabelDOC's Python API to batch translate multiple PDFs in a loop (e.g. translating a folder of papers and moving source files to a
done/directory after completion), the original PDF remains locked on Windows after translation finishes.Attempting to
shutil.move()oros.unlink()the source PDF raises:Root cause: Several places in
high_level.pyopenpymupdf.Documentobjects but never callclose(). On Windows, PyMuPDF's file handles are not released by Python's GC promptly.This is not noticeable when using the CLI (
babeldoc --files) because the process exits after translation, releasing all handles. But it affects any programmatic usage that needs to manipulate the source file afterward. For example, a common workflow is to loop through a directory of PDFs, translate each one, and move completed files to adone/folder to avoid reprocessing — this pattern breaks on Windows without the fix.Summary of Changes
Wrap all
Document/pymupdf.open()calls inbabeldoc/format/pdf/high_level.pywith context managers (withstatements) to ensure deterministic file handle cleanup:check_metadata(Document(...))call indo_translate()old_docandnew_docinmigrate_toc()pymupdf.open()inadd_metadata()pymupdf.open()infix_cmap()PR Type
Breaking Changes
No, this PR does not introduce breaking changes.
Contributor Checklist
Testing Instructions
shutil.move()the source PDF to another directory.PermissionError: [WinError 32]because the first file's handle is still held.Example reproduction script:
Additional Notes
withstatement ensuresDocument.close()is called deterministically, which is the recommended pattern in PyMuPDF's documentation.Summary by cubic
Closes
pymupdfDocumenthandles inbabeldoc/format/pdf/high_level.pyto prevent locked PDFs on Windows after translation, fixingPermissionError: [WinError 32]when moving or deleting source files in API workflows. Also corrects TOC page selection inmigrate_toc()whenonly_include_translated_pageis enabled.pymupdf.open()/Document(...)calls inwithblocks indo_translate(),migrate_toc(),add_metadata(), andfix_cmap()for deterministic cleanup.migrate_toc()page filtering by usingrange(len(old_doc))when buildingpages_to_translate.Written for commit daba33f. Summary will update on new commits.