Skip to content

Commit 52200dc

Browse files
jeanseb6windrjarry
authored andcommitted
context: fix duplicate path error
search_dirs can contain duplicate path if YANGPATH envvar is used. With libyang1, we can add several time the same searchdir without problem but with libyang2, ly_ctx_set_searchdir returns an error if we add a path already existant. To fix it, we now pass the search dir directly when creating the context.
1 parent cc0c343 commit 52200dc

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

libyang/context.py

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,21 @@ def __init__(
4545
self.cdata = None
4646
ctx = ffi.new("struct ly_ctx **")
4747

48+
search_paths = []
49+
if "YANGPATH" in os.environ:
50+
search_paths.extend(os.environ["YANGPATH"].strip(": \t\r\n'\"").split(":"))
51+
elif "YANG_MODPATH" in os.environ:
52+
search_paths.extend(
53+
os.environ["YANG_MODPATH"].strip(": \t\r\n'\"").split(":")
54+
)
55+
if search_path:
56+
search_paths.extend(search_path.strip(": \t\r\n'\"").split(":"))
57+
58+
search_paths = [path for path in search_paths if os.path.isdir(path)]
59+
search_path = ":".join(search_paths) if search_paths else None
60+
4861
if yanglib_path is None:
49-
if lib.ly_ctx_new(ffi.NULL, options, ctx) != lib.LY_SUCCESS:
62+
if lib.ly_ctx_new(str2c(search_path), options, ctx) != lib.LY_SUCCESS:
5063
raise self.error("cannot create context")
5164
else:
5265
if yanglib_fmt == "json":
@@ -66,25 +79,6 @@ def __init__(
6679
if not self.cdata:
6780
raise self.error("cannot create context")
6881

69-
search_dirs = []
70-
if "YANGPATH" in os.environ:
71-
search_dirs.extend(os.environ["YANGPATH"].strip(": \t\r\n'\"").split(":"))
72-
elif "YANG_MODPATH" in os.environ:
73-
search_dirs.extend(
74-
os.environ["YANG_MODPATH"].strip(": \t\r\n'\"").split(":")
75-
)
76-
if search_path:
77-
search_dirs.extend(search_path.strip(": \t\r\n'\"").split(":"))
78-
79-
if yanglib_path:
80-
return
81-
82-
for path in search_dirs:
83-
if not os.path.isdir(path):
84-
continue
85-
if lib.ly_ctx_set_searchdir(self.cdata, str2c(path)) != 0:
86-
raise self.error("cannot set search dir")
87-
8882
def get_yanglib_data(self, content_id_format=""):
8983
dnode = ffi.new("struct lyd_node **")
9084
ret = lib.ly_ctx_get_yanglib_data(self.cdata, dnode, str2c(content_id_format))

tests/test_context.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ def test_ctx_dir(self):
2929
with Context(YANG_DIR) as ctx:
3030
self.assertIsNot(ctx, None)
3131

32+
def test_ctx_duplicate_searchpath(self):
33+
duplicate_search_path = ":".join([YANG_DIR, YANG_DIR])
34+
try:
35+
Context(duplicate_search_path)
36+
except LibyangError:
37+
self.fail("Context.__init__ should not raise LibyangError")
38+
3239
def test_ctx_invalid_dir(self):
3340
with Context("/does/not/exist") as ctx:
3441
self.assertIsNot(ctx, None)

0 commit comments

Comments
 (0)