From 6423babd1a5b26af3ce297477cdad6c90362e15f Mon Sep 17 00:00:00 2001 From: Stefan Gula Date: Sat, 11 Nov 2023 23:10:04 +0100 Subject: [PATCH] data: add unlink function This patch adds unlink function from libyang to allow proper node cleanup procedure. Fixes: #84 Signed-off-by: Stefan Gula Signed-off-by: Samuel Gauthier --- cffi/cdefs.h | 2 ++ libyang/data.py | 6 ++++++ tests/test_data.py | 16 ++++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/cffi/cdefs.h b/cffi/cdefs.h index c1eaee39..23edb262 100644 --- a/cffi/cdefs.h +++ b/cffi/cdefs.h @@ -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); diff --git a/libyang/data.py b/libyang/data.py index c253c57e..0ed18750 100644 --- a/libyang/data.py +++ b/libyang/data.py @@ -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) diff --git a/tests/test_data.py b/tests/test_data.py index bf1a44fb..84e68f82 100644 --- a/tests/test_data.py +++ b/tests/test_data.py @@ -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()