Skip to content

Commit 6423bab

Browse files
stewegsamuel-gauthier
authored andcommitted
data: add unlink function
This patch adds unlink function from libyang to allow proper node cleanup procedure. Fixes: #84 Signed-off-by: Stefan Gula <steweg@gmail.com> Signed-off-by: Samuel Gauthier <samuel.gauthier@6wind.com>
1 parent 6e42fb3 commit 6423bab

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

cffi/cdefs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,8 @@ LY_ERR lys_set_implemented(struct lys_module *, const char **);
267267
#define LYD_NEW_PATH_CANON_VALUE ...
268268
LY_ERR lyd_new_path(struct lyd_node *, const struct ly_ctx *, const char *, const char *, uint32_t, struct lyd_node **);
269269
LY_ERR lyd_find_xpath(const struct lyd_node *, const char *, struct ly_set **);
270+
void lyd_unlink_siblings(struct lyd_node *node);
271+
void lyd_unlink_tree(struct lyd_node *node);
270272
void lyd_free_all(struct lyd_node *node);
271273
void lyd_free_tree(struct lyd_node *node);
272274

libyang/data.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,12 @@ def merge_data_dict(
868868
rpcreply=rpcreply,
869869
)
870870

871+
def unlink(self, with_siblings: bool = False) -> None:
872+
if with_siblings:
873+
lib.lyd_unlink_siblings(self.cdata)
874+
else:
875+
lib.lyd_unlink_tree(self.cdata)
876+
871877
def free_internal(self, with_siblings: bool = True) -> None:
872878
if with_siblings:
873879
lib.lyd_free_all(self.cdata)

tests/test_data.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,3 +881,19 @@ def test_dnode_double_free(self):
881881
dnode = self.ctx.parse_data_mem(self.JSON_CONFIG, "json", validate_present=True)
882882
dnode.free()
883883
dnode.free()
884+
885+
def test_dnode_unlink(self):
886+
dnode = self.ctx.parse_data_mem(self.JSON_CONFIG, "json", validate_present=True)
887+
self.assertIsInstance(dnode, DContainer)
888+
try:
889+
child = dnode.find_one("hostname")
890+
self.assertIsInstance(child, DNode)
891+
child.unlink(with_siblings=False)
892+
self.assertIsNone(dnode.find_one("hostname"))
893+
child = next(dnode.children(), None)
894+
self.assertIsNot(child, None)
895+
child.unlink(with_siblings=True)
896+
child = next(dnode.children(), None)
897+
self.assertIsNone(child, None)
898+
finally:
899+
dnode.free()

0 commit comments

Comments
 (0)