Re: [PATCH v3 06/15] pylibfdt: Support copy.copy() and copy.deepcopy() on Property
From: David Gibson
Date: Sun Nov 01 2026 - 23:36:10 EST
On Sun, Nov 01, 2026 at 02:01:13PM -0800, Douglas Anderson wrote:
> `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`.
Oh, good catch.
Reviewed-by: David Gibson <david@xxxxxxxxxxxxxxxxxxxxx>
>
> Assisted-by: Gemini:gemini-pro-prerelease
> Signed-off-by: Douglas Anderson <dianders@xxxxxxxxxxxx>
> ---
>
> Changes in v3:
> - New patch in v3 to support copying Property instances in DeviceTree
> (Simon Glass)
>
> 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 51db9535bd39..fa5f499ed96d 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 18730965bd02..ae46b29a8f5c 100644
> --- a/tests/pylibfdt_tests.py
> +++ b/tests/pylibfdt_tests.py
> @@ -4,6 +4,7 @@
> # Written by Simon Glass <sjg@xxxxxxxxxxxx>
> #
>
> +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)
> --
> 2.56.0.317.g454f8302e1-goog
>
>
--
David Gibson (he or they) | I'll have my music baroque, and my code
david AT gibson.dropbear.id.au | minimalist, thank you, not the other way
| around.
http://www.ozlabs.org/~dgibson
Attachment:
signature.asc
Description: PGP signature