[PATCH v4 02/14] pylibfdt: Make Property a subclass of bytes instead of bytearray

From: Douglas Anderson

Date: Mon Nov 16 2026 - 15:53:59 EST


`Property` represents a snapshot of a property's name and value read
from a flattened device tree. Making `Property` a subclass of the
immutable `bytes` type instead of mutable `bytearray` prevents callers
from accidentally mutating a `Property` instance in place when sharing
it across data structures (such as `DeviceTree`), makes `Property`
hashable, and allows `Property` instances to be passed directly to
`Fdt.setprop()` without converting to `bytes` first.

Also return `bytes` instead of `bytearray` from the
`fdt_get_property_by_offset()` SWIG typemap and drop the redundant
`bytearray()` conversion in `FdtRo.getprop()`.

Assisted-by: Gemini:gemini-pro-prerelease
Signed-off-by: Douglas Anderson <dianders@xxxxxxxxxxxx>
---

Changes in v4:
- New patch making Property a subclass of bytes instead of bytearray
(David Gibson)

pylibfdt/libfdt.i | 17 +++++++++--------
tests/pylibfdt_tests.py | 1 +
2 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/pylibfdt/libfdt.i b/pylibfdt/libfdt.i
index fa5f499ed96d..8a7dfed85f15 100644
--- a/pylibfdt/libfdt.i
+++ b/pylibfdt/libfdt.i
@@ -492,7 +492,7 @@ class FdtRo(object):

Returns:
Value of property as a Property object (which can be used as a
- bytearray/string), or -ve error number. On failure, returns an
+ bytes/string), or -ve error number. On failure, returns an
integer error

Raises:
@@ -502,7 +502,7 @@ class FdtRo(object):
quiet)
if pdata[0] is None:
return pdata[1]
- return Property(prop_name, bytearray(pdata[0]))
+ return Property(prop_name, pdata[0])

def hasprop(self, nodeoffset, prop_name, quiet=()):
"""Check if a node has a property
@@ -893,7 +893,7 @@ class Fdt(FdtRo):
return check_err(fdt_del_node(self._fdt, nodeoffset), quiet)


-class Property(bytearray):
+class Property(bytes):
"""Holds a device tree property name and value.

This holds a copy of a property taken from the device tree. It does not
@@ -902,11 +902,12 @@ class Property(bytearray):

Properties:
name: Property name
- value: Property value as a bytearray
+ value: Property value as bytes
"""
- def __init__(self, name, value):
- bytearray.__init__(self, value)
- self.name = name
+ def __new__(cls, name, value):
+ obj = super().__new__(cls, value)
+ obj.name = name
+ return obj

def __copy__(self):
return type(self)(self.name, self)
@@ -1275,7 +1276,7 @@ typedef uint32_t fdt32_t;
if ($1) {
resultobj = PyUnicode_FromString(
fdt_string(fdt1, fdt32_to_cpu($1->nameoff)));
- buff = PyByteArray_FromStringAndSize(
+ buff = PyBytes_FromStringAndSize(
(const char *)($1 + 1), fdt32_to_cpu($1->len));
resultobj = SWIG_AppendOutput(resultobj, buff);
}
diff --git a/tests/pylibfdt_tests.py b/tests/pylibfdt_tests.py
index ae46b29a8f5c..78d8976c0cf3 100644
--- a/tests/pylibfdt_tests.py
+++ b/tests/pylibfdt_tests.py
@@ -191,6 +191,7 @@ class PyLibfdtBasicTests(unittest.TestCase):
root = 0
poffset = self.fdt.first_property_offset(root)
prop = self.fdt.get_property_by_offset(poffset)
+ self.assertIsInstance(prop, bytes)
self.assertEqual(prop.name, 'compatible')
self.assertEqual(prop, b'test_tree1\0')
for copied in (copy.copy(prop), copy.deepcopy(prop)):
--
2.56.0.679.gc527c65306-goog