[PATCH v4 04/14] pylibfdt: Add Property.from_*() creation methods
From: Douglas Anderson
Date: Mon Nov 16 2026 - 15:53:53 EST
`libfdt.Property` provides `as_*()` methods (`as_cell`, `as_uint32`,
`as_int32`, `as_uint64`, `as_int64`, `as_list`, `as_uint32_list`,
`as_int32_list`, `as_uint64_list`, `as_int64_list`, `as_str`, and
`as_stringlist`) to decode typed values from a `Property`, but lacks the
corresponding creation methods to construct a `Property` from typed
values.
Add `from_*()` classmethods mirroring each `as_*()` method so callers
(such as `DeviceTree` users) can construct typed `Property` instances
directly without duplicating encoding helpers on `DeviceTree`.
Assisted-by: Gemini:gemini-pro-prerelease
Signed-off-by: Douglas Anderson <dianders@xxxxxxxxxxxx>
---
Changes in v4:
- New patch adding Property.from_*() creation classmethods so
DeviceTree does not need its own get_*/set_* wrapper methods (David
Gibson)
pylibfdt/libfdt.i | 56 +++++++++++++++++++++++++++++++++++++++++
tests/pylibfdt_tests.py | 49 ++++++++++++++++++++++++++++++++++++
2 files changed, 105 insertions(+)
diff --git a/pylibfdt/libfdt.i b/pylibfdt/libfdt.i
index 68f22e316ece..ff9210d123cb 100644
--- a/pylibfdt/libfdt.i
+++ b/pylibfdt/libfdt.i
@@ -962,6 +962,62 @@ class Property(bytes):
parts = self[:-1].split(b'\x00')
return list(map(lambda x: x.decode('utf-8'), parts))
+ @classmethod
+ def from_cell(cls, name, fmt, val):
+ return cls(name, struct.pack('>' + fmt, val))
+
+ @classmethod
+ def from_uint32(cls, name, val):
+ return cls.from_cell(name, 'L', val)
+
+ @classmethod
+ def from_int32(cls, name, val):
+ return cls.from_cell(name, 'l', val)
+
+ @classmethod
+ def from_uint64(cls, name, val):
+ return cls.from_cell(name, 'Q', val)
+
+ @classmethod
+ def from_int64(cls, name, val):
+ return cls.from_cell(name, 'q', val)
+
+ @classmethod
+ def from_list(cls, name, fmt, vals):
+ vals = list(vals)
+ return cls(name, struct.pack(f'>{len(vals)}{fmt}', *vals))
+
+ @classmethod
+ def from_uint32_list(cls, name, vals):
+ return cls.from_list(name, 'L', vals)
+
+ @classmethod
+ def from_int32_list(cls, name, vals):
+ return cls.from_list(name, 'l', vals)
+
+ @classmethod
+ def from_uint64_list(cls, name, vals):
+ return cls.from_list(name, 'Q', vals)
+
+ @classmethod
+ def from_int64_list(cls, name, vals):
+ return cls.from_list(name, 'q', vals)
+
+ @classmethod
+ def from_str(cls, name, val):
+ """Unicode is supported by encoding to UTF-8"""
+ if '\x00' in val:
+ raise ValueError('String value contains embedded nul characters')
+ return cls(name, val.encode('utf-8') + b'\x00')
+
+ @classmethod
+ def from_stringlist(cls, name, vals):
+ """Unicode is supported by encoding to UTF-8"""
+ vals = list(vals)
+ if any('\x00' in v for v in vals):
+ raise ValueError('String value contains embedded nul characters')
+ return cls(name, b''.join(v.encode('utf-8') + b'\x00' for v in vals))
+
class FdtSw(FdtRo):
"""Software interface to create a device tree from scratch
diff --git a/tests/pylibfdt_tests.py b/tests/pylibfdt_tests.py
index 26915a015ccb..e2f9c6d5afe4 100644
--- a/tests/pylibfdt_tests.py
+++ b/tests/pylibfdt_tests.py
@@ -414,6 +414,16 @@ class PyLibfdtBasicTests(unittest.TestCase):
self.assertEqual(9223372036854775807,
self.get_prop("prop-uint64").as_uint64())
self.assertEqual(-2, self.get_prop("prop-int64").as_int64())
+ self.assertEqual(self.get_prop("prop-hex32"),
+ libfdt.Property.from_uint32("prop-hex32", 0xdeadbeef))
+ self.assertEqual(self.get_prop("prop-int32"),
+ libfdt.Property.from_int32("prop-int32", -2))
+ self.assertEqual(
+ self.get_prop("prop-uint64"),
+ libfdt.Property.from_uint64("prop-uint64", 9223372036854775807),
+ )
+ self.assertEqual(self.get_prop("prop-int64"),
+ libfdt.Property.from_int64("prop-int64", -2))
def testGetIntListProperties(self):
"""Test that we can access properties as integer lists"""
@@ -425,6 +435,28 @@ class PyLibfdtBasicTests(unittest.TestCase):
self.get_prop("prop-int64-array").as_int64_list())
self.assertEqual([0x100000000, 0x1],
self.get_prop("prop-uint64-array").as_uint64_list())
+ self.assertEqual(
+ self.get_prop("prop-int32-array"),
+ libfdt.Property.from_int32_list("prop-int32-array", [128, -16, -2]),
+ )
+ self.assertEqual(
+ self.get_prop("prop-uint32-array"),
+ libfdt.Property.from_uint32_list(
+ "prop-uint32-array", [0x1, 0x98765432, 0xdeadbeef]
+ ),
+ )
+ self.assertEqual(
+ self.get_prop("prop-int64-array"),
+ libfdt.Property.from_int64_list(
+ "prop-int64-array", [0x100000000, -2]
+ ),
+ )
+ self.assertEqual(
+ self.get_prop("prop-uint64-array"),
+ libfdt.Property.from_uint64_list(
+ "prop-uint64-array", [0x100000000, 0x1]
+ ),
+ )
def testGetStringlistProperties(self):
"""Test that we can access properties as string list"""
@@ -434,6 +466,17 @@ class PyLibfdtBasicTests(unittest.TestCase):
self.assertEqual(["this is a placeholder string", "string2"],
self.fdt.getprop(node, "placeholder").as_stringlist())
self.assertEqual([], libfdt.Property('empty', b'').as_stringlist())
+ self.assertEqual(
+ self.fdt.getprop(node, "compatible"),
+ libfdt.Property.from_stringlist(
+ "compatible", ["subsubnode1", "subsubnode"]
+ ),
+ )
+ self.assertEqual(
+ b'', libfdt.Property.from_stringlist("empty", [])
+ )
+ with self.assertRaises(ValueError):
+ libfdt.Property.from_stringlist("bad", ["embedded\x00nul"])
with self.assertRaises(ValueError) as e:
libfdt.Property('bad', b'no-nul').as_stringlist()
self.assertIn('lacks nul termination', str(e.exception))
@@ -552,6 +595,12 @@ class PyLibfdtBasicTests(unittest.TestCase):
self.fdt.setprop_str(node, prop, TEST_STRING_3)
self.assertEqual(TEST_STRING_3,
self.fdt.getprop(node, prop).as_str())
+ self.assertEqual(
+ self.fdt.getprop(node, prop),
+ libfdt.Property.from_str(prop, TEST_STRING_3),
+ )
+ with self.assertRaises(ValueError):
+ libfdt.Property.from_str(prop, 'embedded\x00nul')
def testHasProp(self):
"""Test that we can check if a node has a property"""
--
2.56.0.679.gc527c65306-goog