From 4e9e220985ec9541cc3fd598c57f0834d4836cc1 Mon Sep 17 00:00:00 2001 From: Douglas Anderson Date: Sun, 1 Nov 2026 14:01:13 -0800 Subject: [PATCH] pylibfdt: Support copy.copy() and copy.deepcopy() on Property `libfdt.Property` subclasses `bytearray` and has a two-argument `__init__(self, name, value)` constructor. Because `bytearray`'s default copy/reduce protocol passes only the raw byte buffer to the constructor (and does not copy instance attributes), calling `copy.copy()` or `copy.deepcopy()` on a `Property` fails with `TypeError: Property.__init__() missing 1 required positional argument: 'value'`. Implement `__copy__()` and `__deepcopy__()` on `Property` so shallow and deep copies preserve both `name` and the byte contents, and add a unit test in `tests/pylibfdt_tests.py`. Assisted-by: Gemini:gemini-pro-prerelease Signed-off-by: Douglas Anderson Message-ID: <20261101220224.1436242-7-dianders@chromium.org> Signed-off-by: David Gibson --- pylibfdt/libfdt.i | 6 ++++++ tests/pylibfdt_tests.py | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/pylibfdt/libfdt.i b/pylibfdt/libfdt.i index 51db953..fa5f499 100644 --- a/pylibfdt/libfdt.i +++ b/pylibfdt/libfdt.i @@ -908,6 +908,12 @@ class Property(bytearray): bytearray.__init__(self, value) self.name = name + def __copy__(self): + return type(self)(self.name, self) + + def __deepcopy__(self, memo): + return type(self)(self.name, self) + def as_cell(self, fmt): return struct.unpack('>' + fmt, self)[0] diff --git a/tests/pylibfdt_tests.py b/tests/pylibfdt_tests.py index 1873096..ae46b29 100644 --- a/tests/pylibfdt_tests.py +++ b/tests/pylibfdt_tests.py @@ -4,6 +4,7 @@ # Written by Simon Glass # +import copy import struct import sys import types @@ -192,6 +193,11 @@ class PyLibfdtBasicTests(unittest.TestCase): prop = self.fdt.get_property_by_offset(poffset) self.assertEqual(prop.name, 'compatible') self.assertEqual(prop, b'test_tree1\0') + for copied in (copy.copy(prop), copy.deepcopy(prop)): + self.assertIsInstance(copied, libfdt.Property) + self.assertIsNot(copied, prop) + self.assertEqual(copied.name, 'compatible') + self.assertEqual(copied, b'test_tree1\0') with self.assertRaises(FdtException) as e: self.fdt.get_property_by_offset(-2)