Skip to content

Commit c688f56

Browse files
jktjktsamuel-gauthier
authored andcommitted
schema: fix if-feature str() failure with prefixed features
An if-feature can use a feature from an imported module, with the prefix of that module. The parsed lookup only looked in the current module and used the name with its prefix, so str() on such an expression failed: LibyangError: No feature dep:remote-feature in module Remove the prefix from the name, and look in the imported module when the prefix is not the one of the current module. Fixes: 806be4c ("Port to libyang 2") Closes: #174 Signed-off-by: Jan Kundrát <jan.kundrat@cesnet.cz> Signed-off-by: Samuel Gauthier <samuel.gauthier@6wind.com>
1 parent d21117d commit c688f56

4 files changed

Lines changed: 66 additions & 2 deletions

File tree

‎libyang/schema.py‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,8 +1097,25 @@ def _get_operands_parsed(self):
10971097
}
10981098

10991099
def get_feature(name):
1100-
for feature in self.module_features:
1101-
if feature.name() == name:
1100+
prefix, _, local_name = name.rpartition(":")
1101+
candidates = self.module_features
1102+
pmod = self.cdata.mod
1103+
if prefix and pmod and pmod.mod and prefix != c2str(pmod.mod.prefix):
1104+
imp = next(
1105+
(
1106+
i
1107+
for i in ly_array_iter(pmod.imports)
1108+
if c2str(i.prefix) == prefix
1109+
),
1110+
None,
1111+
)
1112+
if imp is None:
1113+
raise LibyangError(
1114+
"Unknown prefix %r in if-feature %r" % (prefix, name)
1115+
)
1116+
candidates = Module(self.context, imp.module).features()
1117+
for feature in candidates:
1118+
if feature.name() == local_name:
11021119
return feature.cdata
11031120
raise LibyangError("No feature %s in module" % name)
11041121

‎tests/test_schema.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,13 @@ def test_iffeature_str(self):
263263
iff = next(self.leaf.if_features())
264264
self.assertEqual(str(iff), "turbo-boost OR networking")
265265

266+
def test_iffeature_str_cross_module(self):
267+
self.ctx.load_module("yolo-featdep", None, ["*"])
268+
self.ctx.load_module("yolo-featuser")
269+
leaf = next(self.ctx.find_path("/yolo-featuser:gated"))
270+
iff = next(leaf.if_features())
271+
self.assertEqual(str(iff), "remote-feature")
272+
266273
def test_iffeature_dump(self):
267274
iff = next(self.leaf.if_features())
268275
self.assertEqual(

‎tests/yang/yolo/yolo-featdep.yang‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module yolo-featdep {
2+
yang-version 1.1;
3+
namespace "urn:yang:yolo:featdep";
4+
prefix featdep;
5+
6+
revision 2026-09-23 {
7+
description
8+
"Initial version.";
9+
}
10+
11+
feature remote-feature {
12+
description
13+
"Defined here so yolo-featuser can reference it by prefix.";
14+
}
15+
}

‎tests/yang/yolo/yolo-featuser.yang‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module yolo-featuser {
2+
yang-version 1.1;
3+
namespace "urn:yang:yolo:featuser";
4+
prefix featuser;
5+
6+
import yolo-featdep { prefix dep; }
7+
8+
revision 2026-09-23 {
9+
description
10+
"Initial version.";
11+
}
12+
13+
feature local-feature {
14+
description
15+
"Unused by `gated` - only here so this module has a non-empty
16+
feature list of its own.";
17+
}
18+
19+
leaf gated {
20+
type string;
21+
if-feature "dep:remote-feature";
22+
description
23+
"Gated by another module's feature, referenced by prefix.";
24+
}
25+
}

0 commit comments

Comments
 (0)