From 44e12f8152549681f07fdacde69f99ee72fe3b70 Mon Sep 17 00:00:00 2001 From: Grzegorz Swiderski Date: Wed, 13 Dec 2023 13:47:25 +0100 Subject: [PATCH] dtlib: Allow deleting the root node Previously, dtlib would fail to parse the following: /delete-node/ &{/}; This is accepted by dtc, so dtlib should be aligned. The expected behavior is that the contents of the "deleted" root node are emptied, but the node itself remains in the tree. This means that it's possible to put that statement at the end of a DTS file and still get a valid output. A small test case for this scenario is included. Signed-off-by: Grzegorz Swiderski --- .../python-devicetree/src/devicetree/dtlib.py | 8 ++++- .../dts/python-devicetree/tests/test_dtlib.py | 32 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/scripts/dts/python-devicetree/src/devicetree/dtlib.py b/scripts/dts/python-devicetree/src/devicetree/dtlib.py index d74fbc67d37d496..cc510ced403568b 100644 --- a/scripts/dts/python-devicetree/src/devicetree/dtlib.py +++ b/scripts/dts/python-devicetree/src/devicetree/dtlib.py @@ -166,7 +166,13 @@ def _get_prop(self, name: str) -> 'Property': return prop def _del(self) -> None: - # Removes the node from the tree + # Removes the node from the tree. When called on the root node, + # this method will leave it empty but still part of the tree. + + if self.parent is None: + self.nodes.clear() + self.props.clear() + return self.parent.nodes.pop(self.name) # type: ignore def __str__(self): diff --git a/scripts/dts/python-devicetree/tests/test_dtlib.py b/scripts/dts/python-devicetree/tests/test_dtlib.py index b1b6ce33fd5a28a..32d1e34a5b4ba00 100644 --- a/scripts/dts/python-devicetree/tests/test_dtlib.py +++ b/scripts/dts/python-devicetree/tests/test_dtlib.py @@ -911,6 +911,15 @@ def test_deletion(): verify_parse(""" /dts-v1/; +/ { + x = "foo"; + sub0 { + x = "bar"; + }; +}; + +/delete-node/ &{/}; + / { sub1 { x = < 1 >; @@ -940,6 +949,29 @@ def test_deletion(): x = < 0x1 >; }; }; +""") + + verify_parse(""" +/dts-v1/; + +/ { + x: x = < &sub >, ⊂ + + sub1 { + x = < &sub >, ⊂ + }; + sub2: sub2 { + x = < &sub >, ⊂ + }; +}; + +/delete-node/ &{/}; +""", +""" +/dts-v1/; + +/ { +}; """) verify_error_endswith("""