Skip to content

Commit edef470

Browse files
ishidawatarurjarry
authored andcommitted
data: fix error when key is prefixed
Fix the following error ``` ====================================================================== ERROR: test_data_from_dict_module_with_prefix (test_data.DataTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "/data/sm/libyang-python/tests/test_data.py", line 298, in test_data_from_dict_module_with_prefix dnode = module.parse_data_dict( File "/data/sm/libyang-python/.tox/py3-devel/lib/python3.8/site-packages/libyang/schema.py", line 177, in parse_data_dict return dict_to_dnode( File "/data/sm/libyang-python/.tox/py3-devel/lib/python3.8/site-packages/libyang/data.py", line 806, in dict_to_dnode _to_dnode( File "/data/sm/libyang-python/.tox/py3-devel/lib/python3.8/site-packages/libyang/data.py", line 743, in _to_dnode prefix, name = name.split(":") UnboundLocalError: local variable 'name' referenced before assignment ``` Signed-off-by: Wataru Ishida <wataru.ishid@gmail.com>
1 parent 8135b78 commit edef470

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

libyang/data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ def _dic_keys(_dic, _schema):
740740
def _to_dnode(_dic, _schema, _parent=ffi.NULL, in_rpc_output=False):
741741
for key in _dic_keys(_dic, _schema):
742742
if ":" in key:
743-
prefix, name = name.split(":")
743+
prefix, name = key.split(":")
744744
else:
745745
prefix, name = None, key
746746

tests/test_data.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,41 @@ def test_data_from_dict_module(self):
270270
dnode.free()
271271
self.assertEqual(json.loads(j), json.loads(self.JSON_CONFIG))
272272

273+
DICT_CONFIG_WITH_PREFIX = {
274+
"yolo-system:conf": {
275+
"hostname": "foo",
276+
"speed": 1234,
277+
"number": [1000, 2000, 3000],
278+
"url": [
279+
{
280+
"enabled": False,
281+
"path": "/CESNET/libyang-python",
282+
"host": "github.com",
283+
"proto": "https",
284+
},
285+
{
286+
"port": 8080,
287+
"proto": "http",
288+
"path": "/index.html",
289+
"enabled": True,
290+
"host": "foobar.com",
291+
},
292+
],
293+
}
294+
}
295+
296+
def test_data_from_dict_module_with_prefix(self):
297+
module = self.ctx.get_module("yolo-system")
298+
dnode = module.parse_data_dict(
299+
self.DICT_CONFIG_WITH_PREFIX, strict=True, config=True
300+
)
301+
self.assertIsInstance(dnode, DContainer)
302+
try:
303+
j = dnode.print_mem("json", pretty=True)
304+
finally:
305+
dnode.free()
306+
self.assertEqual(json.loads(j), json.loads(self.JSON_CONFIG))
307+
273308
DICT_EDIT = {"conf": {"hostname-ref": "notdefined"}}
274309

275310
def test_data_from_dict_edit(self):

0 commit comments

Comments
 (0)