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

TST: TreeObject.empty_tree() #1236

Merged
merged 1 commit into from Aug 14, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 10 additions & 10 deletions PyPDF2/generic/_data_structures.py
Expand Up @@ -359,16 +359,20 @@ def has_children(self) -> bool:
def __iter__(self) -> Any:
return self.children()

def children(self) -> Optional[Any]:
def children(self) -> Iterable[Any]:
if not self.has_children():
return

child = self["/First"]
child_ref = self[NameObject("/First")]
child = child_ref.get_object()
while True:
yield child
if child == self["/Last"]:
if child == self[NameObject("/Last")]:
return
child = child["/Next"] # type: ignore
child_ref = child.get(NameObject("/Next")) # type: ignore
if child_ref is None:
return
child = child_ref.get_object()

def addChild(self, child: Any, pdf: Any) -> None: # pragma: no cover
deprecate_with_replacement("addChild", "add_child")
Expand Down Expand Up @@ -484,11 +488,7 @@ def emptyTree(self) -> None: # pragma: no cover
def empty_tree(self) -> None:
for child in self:
child_obj = child.get_object()
del child_obj[NameObject("/Parent")]
if NameObject("/Next") in child_obj:
del child_obj[NameObject("/Next")]
if NameObject("/Prev") in child_obj:
del child_obj[NameObject("/Prev")]
_reset_node_tree_relationship(child_obj)

if NameObject("/Count") in self:
del self[NameObject("/Count")]
Expand Down Expand Up @@ -650,7 +650,7 @@ def getData(self) -> Union[None, str, bytes]: # pragma: no cover
deprecate_with_replacement("getData", "get_data")
return self.get_data()

def set_data(self, data: Any) -> None:
def set_data(self, data: Any) -> None: # pragma: no cover
raise PdfReadError("Creating EncodedStreamObject is not currently supported")

def setData(self, data: Any) -> None: # pragma: no cover
Expand Down
16 changes: 15 additions & 1 deletion tests/test_generic.py
Expand Up @@ -196,12 +196,16 @@ def test_destination_fit_r():
def test_destination_fit_v():
Destination(NameObject("title"), NullObject(), NameObject(TF.FIT_V), FloatObject(0))

# Trigger Exception
Destination(NameObject("title"), NullObject(), NameObject(TF.FIT_V), None)


def test_destination_exception():
with pytest.raises(PdfReadError):
with pytest.raises(PdfReadError) as exc:
Destination(
NameObject("title"), NullObject(), NameObject("foo"), FloatObject(0)
)
assert exc.value.args[0] == "Unknown Destination Type: 'foo'"


def test_outline_item_write_to_stream():
Expand Down Expand Up @@ -450,45 +454,55 @@ def test_remove_child_found_in_tree():
child1_ref = writer._add_object(child1)
tree.add_child(child1_ref, writer)
assert tree[NameObject("/Count")] == 1
assert len([el for el in tree.children()]) == 1

# Add second child
child2 = TreeObject()
child2[NameObject("/Foo")] = TextStringObject("baz")
child2_ref = writer._add_object(child2)
tree.add_child(child2_ref, writer)
assert tree[NameObject("/Count")] == 2
assert len([el for el in tree.children()]) == 2

# Remove last child
tree.remove_child(child2)
assert tree[NameObject("/Count")] == 1
assert len([el for el in tree.children()]) == 1

# Add new child
child3 = TreeObject()
child3[NameObject("/Foo")] = TextStringObject("3")
child3_ref = writer._add_object(child3)
tree.add_child(child3_ref, writer)
assert tree[NameObject("/Count")] == 2
assert len([el for el in tree.children()]) == 2

# Remove first child
child1 = tree[NameObject("/First")]
tree.remove_child(child1)
assert tree[NameObject("/Count")] == 1
assert len([el for el in tree.children()]) == 1

child4 = TreeObject()
child4[NameObject("/Foo")] = TextStringObject("4")
child4_ref = writer._add_object(child4)
tree.add_child(child4_ref, writer)
assert tree[NameObject("/Count")] == 2
assert len([el for el in tree.children()]) == 2

child5 = TreeObject()
child5[NameObject("/Foo")] = TextStringObject("5")
child5_ref = writer._add_object(child5)
tree.add_child(child5_ref, writer)
assert tree[NameObject("/Count")] == 3
assert len([el for el in tree.children()]) == 3

# Remove middle child
tree.remove_child(child4)
assert tree[NameObject("/Count")] == 2
assert len([el for el in tree.children()]) == 2

tree.empty_tree()


def test_remove_child_in_tree():
Expand Down