Skip to content

Commit 4880f64

Browse files
committed
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 3072af0 commit 4880f64

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
@@ -1389,13 +1389,23 @@ def parent(self) -> Optional["SNode"]:
13891389
return None
13901390

13911391
def when_conditions(self):
1392-
wh = ffi.new("struct lysc_when **")
13931392
wh = lib.lysc_node_when(self.cdata)
13941393
if wh == ffi.NULL:
13951394
return
13961395
for cond in ly_array_iter(wh):
13971396
yield c2str(lib.lyxp_get_expr(cond.cond))
13981397

1398+
def when_conditions_nodes(self) -> Iterator[Optional["SNode"]]:
1399+
wh = lib.lysc_node_when(self.cdata)
1400+
if wh == ffi.NULL:
1401+
return
1402+
for cond in ly_array_iter(wh):
1403+
yield (
1404+
None
1405+
if cond.context == ffi.NULL
1406+
else SNode.new(self.context, cond.context)
1407+
)
1408+
13991409
def parsed(self) -> Optional["PNode"]:
14001410
if self.cdata_parsed is None or self.cdata_parsed == ffi.NULL:
14011411
return None

tests/test_schema.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,14 @@ def tearDown(self):
843843
self.ctx.destroy()
844844
self.ctx = None
845845

846+
def test_anydata(self):
847+
snode = next(self.ctx.find_path("/yolo-nodetypes:any1"))
848+
self.assertIsInstance(snode, SAnydata)
849+
assert next(snode.when_conditions()) is not None
850+
snode2 = next(snode.when_conditions_nodes())
851+
assert isinstance(snode2, SAnydata)
852+
assert snode2.cdata == snode.cdata
853+
846854
def test_anydata_parsed(self):
847855
snode = next(self.ctx.find_path("/yolo-nodetypes:any1"))
848856
self.assertIsInstance(snode, SAnydata)

0 commit comments

Comments
 (0)