Skip to content

Commit

Permalink
netlink: fix recursive attrs dump
Browse files Browse the repository at this point in the history
  • Loading branch information
svinota committed Mar 20, 2024
1 parent 3ee38a9 commit 9de8ca2
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions pyroute2/netlink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 9de8ca2

Please sign in to comment.