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

Refactor CheckParent #4007

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 8 additions & 2 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7186,6 +7186,7 @@ def _parse_da(self):
def _validate(self):
"""Validate the class entries.
"""
CheckParent(self) # ensure page still exists
if (self.rect.is_infinite
or self.rect.is_empty
):
Expand Down Expand Up @@ -7305,6 +7306,7 @@ def button_states(self):

@property
def next(self):
CheckParent(self) # ensure page still exists
return self._annot.next

def on_state(self):
Expand Down Expand Up @@ -17996,9 +17998,13 @@ def CheckMorph(o: typing.Any) -> bool:


def CheckParent(o: typing.Any):
try:
check = str(o.parent) # will raise if no parent or weakref is dead
if check == "None":
raise ValueError(f"orphaned object: parent is None")
except (ReferenceError, ValueError):
raise ValueError(f"orphaned object: parent is dead")
return
if not hasattr(o, "parent") or o.parent is None:
raise ValueError(f"orphaned object {type(o)=}: parent is None")


def CheckQuad(q: typing.Any) -> bool:
Expand Down
16 changes: 16 additions & 0 deletions tests/test_parent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import pymupdf


def test_parent():
"""Test invalidating parent on page re-assignment."""
doc = pymupdf.open()
page = doc.new_page()
a = page.add_highlight_annot(page.rect) # insert annotation on page 0
page = doc.new_page() # make a new page, should orphanate annotation
try:
print(a) # should raise
error = False
except ValueError as e:
assert str(e) == "orphaned object: parent is dead"
error = True
assert error
Loading