Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc.close() not closing the document #3081

Closed
JojoMueller opened this issue Jan 23, 2024 · 3 comments
Closed

doc.close() not closing the document #3081

JojoMueller opened this issue Jan 23, 2024 · 3 comments
Labels
bug fix developed release schedule to be determined

Comments

@JojoMueller
Copy link

Description of the bug

Hi,

doc.close() does not really close a pdf file. My python script does some checks using PyMuPDF, closes the file, compresses it by calling an external program and finally saves the original and copies the compressed file onto the original one. This yields an os error, indicating that the original file is in use by the current python process. Importing fitz_old as fitz instead of fitz works fine.

Thank you in advance for your effort fixing the problem and enhancing this excellent program.

How to reproduce the bug

  • Open a PDF file using PyMuPDF: doc = fitz.open(pdf_path)
  • Try to close the document: doc.close()
  • Try to copy a file onto the one pointed to by pdf_path: os.rename(any_file, pdf_path)

PyMuPDF version

1.23.17

Operating system

Windows

Python version

3.10

@JorjMcKie JorjMcKie added the bug label Jan 23, 2024
@julian-smith-artifex-com
Copy link
Collaborator

With rebased, the file is kept open after .close() if a fitz.Page instance is still alive.

So for example:

document = fitz.open(path)
page = document[0]
document.close()
# We still have a file handle open for `path`.
page = None
# No file handle open for `path`.

Arguably this new behaviour is ok, but we're looking at whether we could replicate the behaviour of fitz_old where page would become some sort of zombie object after document.close().

julian-smith-artifex-com added a commit that referenced this issue Jan 23, 2024
In `Document.close()`, we reinstate call of `self._reset_page_refs()`.

Also when converting between Fz and Pdf pages/documents, generate better error
if document is None; this can happen if a document has been closed.
julian-smith-artifex-com added a commit that referenced this issue Jan 23, 2024
In `Document.close()`, we reinstate call of `self._reset_page_refs()`.

Also when converting between Fz and Pdf pages/documents, generate better error
if document is None; this can happen if a document has been closed.
@JojoMueller
Copy link
Author

Thanks for your help. After setting page to None the file is effectively closed.

@julian-smith-artifex-com
Copy link
Collaborator

Fixed in 1.23.18. (There is now no need to set page to None.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug fix developed release schedule to be determined
Projects
None yet
Development

No branches or pull requests

3 participants