ArtifexSoftware / PyMuPDF-performance

2 stars 2 forks source link

pypdfium2: remove problematic close calls #4

Closed mara004 closed 1 year ago

mara004 commented 1 year ago

With pypdfium2, the API contract is that parent objects must not be closed before child objects are closed, i.e. order matters.

If you close the document explicitly but let pages be closed automatically on garbage collection, the document is likely to be closed before all pages are closed, which is illegal and will result in warnings.

See also https://pypdfium2.readthedocs.io/en/latest/python_api.html#memory-management. I hope to add additional clarification to the docs soon. Seeing as this is not obvious and an easy mistake to make, I guess we should update the support model to have parents keep track of children and ensure correct order automatically.

This change removes the document close calls. Alternatively, page close calls could be added.

mara004 commented 1 year ago

I've made a patch that should fix this in pypdfium2 sooner or later (https://github.com/pypdfium2-team/pypdfium2/pull/224).

julian-smith-artifex-com commented 1 year ago

Thanks for this, and apologies for not responding earlier.

I'm glad that you've fixed the problem, sounds like a useful improvement too.

mara004 commented 1 year ago

No problem, take your time. Sorry for the embarrassing commit repetition above, I shouldn't force push so much. 🙈