From 9de8ca23d7c505aed6d769484af214b7b6c394cd Mon Sep 17 00:00:00 2001 From: Peter Saveliev Date: Wed, 20 Mar 2024 22:14:39 +0100 Subject: [PATCH] netlink: fix recursive attrs dump --- pyroute2/netlink/__init__.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/pyroute2/netlink/__init__.py b/pyroute2/netlink/__init__.py index 6e60a9ee5..6e43f2471 100644 --- a/pyroute2/netlink/__init__.py +++ b/pyroute2/netlink/__init__.py @@ -1388,6 +1388,23 @@ def load(self, dump): self.setvalue(dump) return self + def dump_attrs(self, attrs): + ret = [] + for i in attrs: + if isinstance(i, nlmsg_base): + ret.append(i.dump()) + elif isinstance(i[1], nlmsg_base): + # catch nlmsg + ret.append([i[0], i[1].dump()]) + elif isinstance(i[1], (set, list, tuple)): + # catch iterables + ret.append([i[0], self.dump_attrs(i[1])]) + elif isinstance(i[1], bytes): + ret.append([i[0], hexdump(i[1])]) + else: + ret.append([i[0], i[1]]) + return ret + def dump(self): ''' Dump packet as a dict @@ -1399,16 +1416,11 @@ def dump(self): if k == 'header': ret['header'] = dict(a['header']) elif k == 'attrs': - ret['attrs'] = attrs = [] - for i in a['attrs']: - if isinstance(i[1], nlmsg_base): - attrs.append([i[0], i[1].dump()]) - elif isinstance(i[1], set): - attrs.append([i[0], tuple(i[1])]) - else: - attrs.append([i[0], i[1]]) + ret['attrs'] = self.dump_attrs(v) else: ret[k] = v + elif isinstance(a, nlmsg_base): + ret = a.dump() else: ret = a return ret