Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix segmentation fault with opaque node #76

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions libyang/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,11 +404,7 @@ def eval_xpath(self, xpath: str):
return False

def path(self) -> str:
path = lib.lyd_path(self.cdata, lib.LYD_PATH_STD, ffi.NULL, 0)
try:
return c2str(path)
finally:
lib.free(path)
return self._get_path(self.cdata)

def validate(
self,
Expand Down Expand Up @@ -904,11 +900,25 @@ def _decorator(nodeclass):
@classmethod
def new(cls, context: "libyang.Context", cdata) -> "DNode":
cdata = ffi.cast("struct lyd_node *", cdata)
nodecls = cls.NODETYPE_CLASS.get(cdata.schema.nodetype, None)
if not cdata.schema:
schemas = list(context.find_path(cls._get_path(cdata)))
if len(schemas) != 1:
raise LibyangError("Unable to determine schema")
nodecls = cls.NODETYPE_CLASS.get(schemas[0].nodetype(), None)
else:
nodecls = cls.NODETYPE_CLASS.get(cdata.schema.nodetype, None)
if nodecls is None:
raise TypeError("node type %s not implemented" % cdata.schema.nodetype)
return nodecls(context, cdata)

@staticmethod
def _get_path(cdata) -> str:
path = lib.lyd_path(cdata, lib.LYD_PATH_STD, ffi.NULL, 0)
try:
return c2str(path)
finally:
lib.free(path)


# -------------------------------------------------------------------------------------
@DNode.register(SNode.CONTAINER)
Expand Down
11 changes: 11 additions & 0 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -860,3 +860,14 @@ def test_add_defaults(self):
node = dnode.find_path("/yolo-system:conf/speed")
self.assertIsInstance(node, DLeaf)
self.assertEqual(node.value(), 4321)

def test_dnode_new_opaq_find_one(self):
dnode = self.ctx.parse_data_mem(self.JSON_CONFIG, "json", validate_present=True)

hostname = dnode.find_one("hostname")
hostname.free(with_siblings=False)

dnode.new_path("/yolo-system:conf/hostname", value=None, opt_opaq=True)
opaq_hostname = dnode.find_one("hostname")

self.assertIsInstance(opaq_hostname, DLeaf)
Loading