Skip to content

Commit

Permalink
pylibfdt: add Property.as_stringlist()
Browse files Browse the repository at this point in the history
Add a new method for decoding a string list property, useful for e.g.
the "reg-names" property.

Also add a test for the new method.

Signed-off-by: Luca Weiss <[email protected]>
Message-Id: <[email protected]>
Signed-off-by: David Gibson <[email protected]>
  • Loading branch information
z3ntu authored and dgibson committed Dec 28, 2021
1 parent d152126 commit 8310271
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pylibfdt/libfdt.i
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,13 @@ class Property(bytearray):
raise ValueError('Property contains embedded nul characters')
return self[:-1].decode('utf-8')

def as_stringlist(self):
"""Unicode is supported by decoding from UTF-8"""
if self[-1] != 0:
raise ValueError('Property lacks nul termination')
parts = self[:-1].split(b'\x00')
return list(map(lambda x: x.decode('utf-8'), parts))


class FdtSw(FdtRo):
"""Software interface to create a device tree from scratch
Expand Down
8 changes: 8 additions & 0 deletions tests/pylibfdt_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,14 @@ def testGetIntProperties(self):
self.get_prop("prop-uint64").as_uint64())
self.assertEqual(-2, self.get_prop("prop-int64").as_int64())

def testGetStringlistProperties(self):
"""Test that we can access properties as string list"""
node = self.fdt.path_offset('/subnode@1/subsubnode')
self.assertEqual(["subsubnode1", "subsubnode"],
self.fdt.getprop(node, "compatible").as_stringlist())
self.assertEqual(["this is a placeholder string", "string2"],
self.fdt.getprop(node, "placeholder").as_stringlist())

def testReserveMap(self):
"""Test that we can access the memory reserve map"""
self.assertEqual(2, self.fdt.num_mem_rsv())
Expand Down

0 comments on commit 8310271

Please sign in to comment.