diff options
author | Simon Glass <sjg@chromium.org> | 2022-03-06 06:18:56 +0300 |
---|---|---|
committer | Simon Glass <sjg@chromium.org> | 2022-03-19 04:24:25 +0300 |
commit | 0ded4d434dc38031a256eab4428d5e339967dd6b (patch) | |
tree | e12d94745e1f34c272cf88f6227efe20680c5781 | |
parent | b13114cd216703769ef56a4d1129934380902996 (diff) | |
download | u-boot-0ded4d434dc38031a256eab4428d5e339967dd6b.tar.xz |
dtoc: Tidy up implementation of AddStringList()
Refactor this to avoid a loop. Also add a test for an empty string.
Signed-off-by: Simon Glass <sjg@chromium.org>
Suggested-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
Reviewed-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
-rw-r--r-- | tools/dtoc/fdt.py | 4 | ||||
-rw-r--r-- | tools/dtoc/fdt_util.py | 4 | ||||
-rwxr-xr-x | tools/dtoc/test_fdt.py | 8 |
3 files changed, 13 insertions, 3 deletions
diff --git a/tools/dtoc/fdt.py b/tools/dtoc/fdt.py index c16909a876..d933972918 100644 --- a/tools/dtoc/fdt.py +++ b/tools/dtoc/fdt.py @@ -516,9 +516,7 @@ class Node: Returns: Prop added """ - out = b'' - for string in val: - out += bytes(string, 'utf-8') + b'\0' + out = b'\0'.join(bytes(s, 'utf-8') for s in val) + b'\0' if val else b'' return self.AddData(prop_name, out) def AddInt(self, prop_name, val): diff --git a/tools/dtoc/fdt_util.py b/tools/dtoc/fdt_util.py index 57550624bb..d7c38ad1e0 100644 --- a/tools/dtoc/fdt_util.py +++ b/tools/dtoc/fdt_util.py @@ -158,6 +158,8 @@ def GetString(node, propname, default=None): if not prop: return default value = prop.value + if not prop.bytes: + return '' if isinstance(value, list): raise ValueError("Node '%s' property '%s' has list value: expecting " "a single string" % (node.name, propname)) @@ -179,6 +181,8 @@ def GetStringList(node, propname, default=None): if not prop: return default value = prop.value + if not prop.bytes: + return [] if not isinstance(value, list): strval = GetString(node, propname) return [strval] diff --git a/tools/dtoc/test_fdt.py b/tools/dtoc/test_fdt.py index ea707f2f87..914ed6aed5 100755 --- a/tools/dtoc/test_fdt.py +++ b/tools/dtoc/test_fdt.py @@ -550,6 +550,12 @@ class TestProp(unittest.TestCase): data = self.fdt.getprop(self.node.Offset(), 'stringlist') self.assertEqual(b'123\x00456\0', data) + val = [] + self.node.AddStringList('stringlist', val) + self.dtb.Sync(auto_resize=True) + data = self.fdt.getprop(self.node.Offset(), 'stringlist') + self.assertEqual(b'', data) + def test_delete_node(self): """Test deleting a node""" old_offset = self.fdt.path_offset('/spl-test') @@ -637,6 +643,7 @@ class TestFdtUtil(unittest.TestCase): self.assertEqual('message', fdt_util.GetString(self.node, 'stringval')) self.assertEqual('test', fdt_util.GetString(self.node, 'missing', 'test')) + self.assertEqual('', fdt_util.GetString(self.node, 'boolval')) with self.assertRaises(ValueError) as e: self.assertEqual(3, fdt_util.GetString(self.node, 'stringarray')) @@ -651,6 +658,7 @@ class TestFdtUtil(unittest.TestCase): fdt_util.GetStringList(self.node, 'stringarray')) self.assertEqual(['test'], fdt_util.GetStringList(self.node, 'missing', ['test'])) + self.assertEqual([], fdt_util.GetStringList(self.node, 'boolval')) def testGetArgs(self): node = self.dtb.GetNode('/orig-node') |