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

Fix #2596 #2711

Merged
merged 3 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fitz/fitz.i
Original file line number Diff line number Diff line change
Expand Up @@ -4625,6 +4625,7 @@ if basestate:
old_annots[k] = v
page._erase() # remove the page
page = None
TOOLS.store_shrink(100)
page = self.load_page(pno) # reload the page

# copy annot refs over to the new dictionary
Expand Down
15 changes: 15 additions & 0 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5040,8 +5040,23 @@ def reload_page(self, page: "struct Page *") -> "struct Page *":
pno = page.number # save the page number
for k, v in page._annot_refs.items(): # save the annot dictionary
old_annots[k] = v

# We need to set `page.this` to `None` here in order to force the
# `mupdf::FzPage` to be removed from the MuPDF document's internal
# list, so that when `self.load_page()` is called below, its call of
# `fz_load_page()` will create a new `mupdf::FzPage`.
#
# This only works if the MuPDF `fz_page`'s reference count is 1. User
# code will have to work fairly hard to break this, by somehow making
# a copy of the mupdf::FzPage itself; for example making a copy in
# Python will usually create a new Python object that refers to the
# same mupdf::FzPage and this not increment the MuPDF reference count.
#
assert page.this.m_internal.refs == 1
page.this = None
page._erase() # remove the page
page = None
TOOLS.store_shrink(100)
page = self.load_page(pno) # reload the page

# copy annot refs over to the new dictionary
Expand Down
Binary file added tests/resources/test_2596.pdf
Binary file not shown.
14 changes: 14 additions & 0 deletions tests/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,3 +539,17 @@ def test_2692():
clip=fitz.Rect(0,0,10,10),
)


def test_2596():
"""Cconfirm correctly abandoning cache when reloading a page."""
doc = fitz.Document(f"{scriptdir}/resources/test_2596.pdf")
page = doc[0]
pix0 = page.get_pixmap() # render the page
_ = doc.tobytes(garbage=3) # save with garbage collection

# Note this will invalidate cache content for this page.
# Reloading the page now empties the cache, so rendering
# will deliver the same pixmap
page = doc.reload_page(page)
pix1 = page.get_pixmap()
assert pix1.samples == pix0.samples
Loading