Skip to content

Commit

Permalink
Refactor CheckParent
Browse files Browse the repository at this point in the history
Adresses issue #4004.
Calling CheckParent should raise ReferenceError, respetively ValueError if an objects parent is no longer available.
  • Loading branch information
JorjMcKie committed Oct 30, 2024
1 parent 214616d commit 730a550
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
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

0 comments on commit 730a550

Please sign in to comment.