Skip to content

Commit 74b7d1d

Browse files
stewegrjarry
authored andcommitted
schema: adds ability to get when context nodes
This patch adds ability to get context schema node from which when contition is evaluated. It also fixes memory leak of original when_conditions Signed-off-by: Stefan Gula <[email protected]>
1 parent b35336b commit 74b7d1d

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

libyang/schema.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1440,13 +1440,23 @@ def parent(self) -> Optional["SNode"]:
14401440
return None
14411441

14421442
def when_conditions(self):
1443-
wh = ffi.new("struct lysc_when **")
14441443
wh = lib.lysc_node_when(self.cdata)
14451444
if wh == ffi.NULL:
14461445
return
14471446
for cond in ly_array_iter(wh):
14481447
yield c2str(lib.lyxp_get_expr(cond.cond))
14491448

1449+
def when_conditions_nodes(self) -> Iterator[Optional["SNode"]]:
1450+
wh = lib.lysc_node_when(self.cdata)
1451+
if wh == ffi.NULL:
1452+
return
1453+
for cond in ly_array_iter(wh):
1454+
yield (
1455+
None
1456+
if cond.context == ffi.NULL
1457+
else SNode.new(self.context, cond.context)
1458+
)
1459+
14501460
def parsed(self) -> Optional["PNode"]:
14511461
if self.cdata_parsed is None or self.cdata_parsed == ffi.NULL:
14521462
return None

tests/test_schema.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,14 @@ def tearDown(self):
918918
self.ctx.destroy()
919919
self.ctx = None
920920

921+
def test_anydata(self):
922+
snode = next(self.ctx.find_path("/yolo-nodetypes:any1"))
923+
self.assertIsInstance(snode, SAnydata)
924+
assert next(snode.when_conditions()) is not None
925+
snode2 = next(snode.when_conditions_nodes())
926+
assert isinstance(snode2, SAnydata)
927+
assert snode2.cdata == snode.cdata
928+
921929
def test_anydata_parsed(self):
922930
snode = next(self.ctx.find_path("/yolo-nodetypes:any1"))
923931
self.assertIsInstance(snode, SAnydata)

0 commit comments

Comments
 (0)