mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Simon Glass <sjg@chromium.org>
Cc: dianders@chromium.org, Devicetree-compiler@vger.kernel.org,
	"Rob Herring" <robh@kernel.org>,
	"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
	"Conor Dooley" <conor+dt@kernel.org>,
	"Konrad Dybcio" <konradybcio@kernel.org>,
	"Brian Norris" <briannorris@chromium.org>,
	"Chen-Yu Tsai" <wenst@chromium.org>,
	"André Draszik" <andre.draszik@linaro.org>,
	Devicetree@vger.kernel.org,
	"Tudor Ambarus" <tudor.ambarus@linaro.org>,
	linux-kernel@vger.kernel.org,
	"Julius Werner" <jwerner@chromium.org>,
	"Saravana Kannan" <saravanak@kernel.org>,
	"Peter Griffin" <peter.griffin@linaro.org>
Subject: Re: [PATCH v2 02/10] pylibfdt: Add subnode/property iterators and get_mem_rsv_list()
Date: Mon, 2 Nov 2026 12:48:47 +1100	[thread overview]
Message-ID: <aufr7SA1BO7zDQws@gractus.seuss> (raw)
In-Reply-To: <CAFLszThVtOwXJ4GKv5fFPGWGy7EL63JrEU=yhByUccW6Vkq+=A@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 3514 bytes --]

On Fri, Oct 30, 2026 at 07:50:17AM -0500, Simon Glass wrote:
> Hi Doug,
> 
> On 2026-10-28T21:46:49, Douglas Anderson <dianders@chromium.org> wrote:
> > pylibfdt: Add subnode/property iterators and get_mem_rsv_list()
> >
> > Add Pythonic iterator helpers on `FdtRo` to simplify walking nodes,
> > properties, and memory reservation entries without manual offset loops
> > or `-FDT_ERR_NOTFOUND` sentinel handling:
> >
> > - `subnodes(nodeoffset)`: yields the offset of each direct child subnode
> >   of `nodeoffset`.
> > - `properties(nodeoffset)`: yields a `Property` object for each property
> >   of `nodeoffset`.
> > - `get_mem_rsv_list()`: returns a list of `(address, size)` tuples for
> >   all entries in the memory reservation block.
> >
> > Assisted-by: Gemini:gemini-pro-prerelease
> > Signed-off-by: Douglas Anderson <dianders@chromium.org>
> >
> >  pylibfdt/libfdt.i       | 45 +++++++++++++++++++++++++++++++++++++++++++++
> >  tests/pylibfdt_tests.py | 18 ++++++++++++++++++
> >  2 files changed, 63 insertions(+)
> 
> > diff --git a/pylibfdt/libfdt.i b/pylibfdt/libfdt.i
> > @@ -201,6 +201,23 @@ class FdtRo(object):
> > +        soff = self.first_subnode(nodeoffset, quiet=QUIET_NOTFOUND)
> > +        while soff >= 0:
> > +            yield soff
> > +            soff = self.next_subnode(soff, quiet=QUIET_NOTFOUND)
> 
> Since Fdt inherits this generator, a caller can easily write 'for off
> in fdt.subnodes(0): fdt.del_node(off)' and get garbage, because the
> offsets move under it.

Right.  I noticed that, but merged anyway, since you can basically do
the same thing with the existing interfaces.  By abstracting it
further, this interface does arguably make the problem worse.  This is
kind of a perennial problem with the Python bindings.  The libfdt
interface is shaped by what is and isn't possible with the flattened
format - the reason we insistently name "offsets" everywhere, rather
than just "nodes" or "properties" is an attempt to inculcate the right
mental model.  By abstracting things more, the Python bindings make it
easier to miss that.

[Aside: one of the many projects I'll probably never find time for is
 to write a Rust-native libfdt, where the ownership model could be
 harnessed to enforce many of the required invariants]

> Since David has applied this, please can you
> send a follow-up noting in the docstring (here and in properties())
> that the tree must not be modified while iterating, as with
> fdt_for_each_subnode() in C?

Right, adding a comment is probably a good idea.

> > diff --git a/tests/pylibfdt_tests.py b/tests/pylibfdt_tests.py
> > @@ -243,6 +243,22 @@ class PyLibfdtBasicTests(unittest.TestCase):
> > +        leaf = self.fdt.path_offset('/subnode@1/ss1')
> > +        self.assertEqual(list(self.fdt.subnodes(leaf)), [])
> 
> The docstrings say both iterators raise FdtException for a bad offset,
> but nothing tests that. A test that list(self.fdt.subnodes(-1)) and
> list(self.fdt.properties(-1)) raise, perhaps alongside
> testFirstNextSubnodeOffsetExceptions, would be good, perhaps in the
> same follow-up. Since these are generators, the exception only appears
> on the first next(), not when the method is called.
> 
> Reviewed-by: Simon Glass <sjg@chromium.org>
> 

-- 
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 #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  parent reply	other threads:[~2026-11-02  1:49 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-28 21:46 [PATCH v2 00/10] fdtfactor: Introduce fdtfactor to auto-factor overlays Douglas Anderson
2026-10-28 21:46 ` [PATCH v2 01/10] pylibfdt: Fix FdtSw.as_fdt() when buffer is nearly full Douglas Anderson
2026-10-30 12:50   ` Simon Glass
2026-10-28 21:46 ` [PATCH v2 02/10] pylibfdt: Add subnode/property iterators and get_mem_rsv_list() Douglas Anderson
2026-10-29  8:38   ` David Gibson
2026-10-30 12:50   ` Simon Glass
2026-11-01 22:04     ` Doug Anderson
2026-11-02  1:48     ` David Gibson [this message]
2026-10-28 21:46 ` [PATCH v2 03/10] pylibfdt: Add NodeTree live tree representation Douglas Anderson
2026-10-30  4:57   ` David Gibson
2026-11-01 22:04     ` Doug Anderson
2026-10-30 12:50   ` Simon Glass
2026-11-01 22:04     ` Doug Anderson
2026-10-28 21:46 ` [PATCH v2 04/10] libfdt: overlay: Factor out fixup and fragment path parsing helpers Douglas Anderson
2026-10-30  4:04   ` David Gibson
2026-11-01 22:04     ` Doug Anderson
2026-10-30 12:50   ` Simon Glass
2026-10-28 21:46 ` [PATCH v2 05/10] libfdt: overlay: Prepare fixup and fragment path helpers for reuse Douglas Anderson
2026-10-30  4:24   ` David Gibson
2026-11-01 22:04     ` Doug Anderson
2026-10-30 12:50   ` Simon Glass
2026-11-01 22:05     ` Doug Anderson
2026-10-28 21:46 ` [PATCH v2 06/10] libfdt: overlay: Update base /__local_fixups__ on apply Douglas Anderson
2026-10-30 11:26   ` David Gibson
2026-11-01 22:05     ` Doug Anderson
2026-11-02  5:11       ` David Gibson
2026-10-30 12:50   ` Simon Glass
2026-11-01 22:05     ` Doug Anderson
2026-10-28 21:46 ` [PATCH v2 07/10] fdtfactor: Add tool to split DTBs into a base DTB and overlays Douglas Anderson
2026-10-30 12:50   ` Simon Glass
2026-10-28 21:46 ` [PATCH v2 08/10] fdtfactor: Add -S/--strip-local-fixups option Douglas Anderson
2026-10-30 12:50   ` Simon Glass
2026-11-01 22:05     ` Doug Anderson
2026-10-28 21:46 ` [PATCH v2 09/10] fdtfactor: Support compressed DTB inputs and outputs Douglas Anderson
2026-10-30 12:50   ` Simon Glass
2026-10-28 21:46 ` [PATCH v2 10/10] fdtfactor: Add compiled FIT image factoring support Douglas Anderson
2026-10-30 12:50   ` Simon Glass
2026-11-01 22:05     ` Doug Anderson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aufr7SA1BO7zDQws@gractus.seuss \
    --to=david@gibson.dropbear.id.au \
    --cc=Devicetree-compiler@vger.kernel.org \
    --cc=Devicetree@vger.kernel.org \
    --cc=andre.draszik@linaro.org \
    --cc=briannorris@chromium.org \
    --cc=conor+dt@kernel.org \
    --cc=dianders@chromium.org \
    --cc=jwerner@chromium.org \
    --cc=konradybcio@kernel.org \
    --cc=krzk+dt@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peter.griffin@linaro.org \
    --cc=robh@kernel.org \
    --cc=saravanak@kernel.org \
    --cc=sjg@chromium.org \
    --cc=tudor.ambarus@linaro.org \
    --cc=wenst@chromium.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®