Skip to content

Commit

Permalink
data: add unlink function
Browse files Browse the repository at this point in the history
This patch adds unlink function from libyang to allow
proper node cleanup procedure.

Fixes: #84
Signed-off-by: Stefan Gula <[email protected]>
Signed-off-by: Samuel Gauthier <[email protected]>
  • Loading branch information
steweg authored and samuel-gauthier committed Jan 26, 2024
1 parent 6e42fb3 commit 6423bab
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cffi/cdefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ LY_ERR lys_set_implemented(struct lys_module *, const char **);
#define LYD_NEW_PATH_CANON_VALUE ...
LY_ERR lyd_new_path(struct lyd_node *, const struct ly_ctx *, const char *, const char *, uint32_t, struct lyd_node **);
LY_ERR lyd_find_xpath(const struct lyd_node *, const char *, struct ly_set **);
void lyd_unlink_siblings(struct lyd_node *node);
void lyd_unlink_tree(struct lyd_node *node);
void lyd_free_all(struct lyd_node *node);
void lyd_free_tree(struct lyd_node *node);

Expand Down
6 changes: 6 additions & 0 deletions libyang/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,12 @@ def merge_data_dict(
rpcreply=rpcreply,
)

def unlink(self, with_siblings: bool = False) -> None:
if with_siblings:
lib.lyd_unlink_siblings(self.cdata)
else:
lib.lyd_unlink_tree(self.cdata)

def free_internal(self, with_siblings: bool = True) -> None:
if with_siblings:
lib.lyd_free_all(self.cdata)
Expand Down
16 changes: 16 additions & 0 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -881,3 +881,19 @@ def test_dnode_double_free(self):
dnode = self.ctx.parse_data_mem(self.JSON_CONFIG, "json", validate_present=True)
dnode.free()
dnode.free()

def test_dnode_unlink(self):
dnode = self.ctx.parse_data_mem(self.JSON_CONFIG, "json", validate_present=True)
self.assertIsInstance(dnode, DContainer)
try:
child = dnode.find_one("hostname")
self.assertIsInstance(child, DNode)
child.unlink(with_siblings=False)
self.assertIsNone(dnode.find_one("hostname"))
child = next(dnode.children(), None)
self.assertIsNot(child, None)
child.unlink(with_siblings=True)
child = next(dnode.children(), None)
self.assertIsNone(child, None)
finally:
dnode.free()

0 comments on commit 6423bab

Please sign in to comment.