Skip to content

Commit

Permalink
dtlib: Allow deleting the root node
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
57300 committed Dec 13, 2023
1 parent c758f1c commit 44e12f8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
8 changes: 7 additions & 1 deletion scripts/dts/python-devicetree/src/devicetree/dtlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
32 changes: 32 additions & 0 deletions scripts/dts/python-devicetree/tests/test_dtlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,15 @@ def test_deletion():
verify_parse("""
/dts-v1/;
/ {
x = "foo";
sub0 {
x = "bar";
};
};
/delete-node/ &{/};
/ {
sub1 {
x = < 1 >;
Expand Down Expand Up @@ -940,6 +949,29 @@ def test_deletion():
x = < 0x1 >;
};
};
""")

verify_parse("""
/dts-v1/;
/ {
x: x = < &sub >, &sub;
sub1 {
x = < &sub >, &sub;
};
sub2: sub2 {
x = < &sub >, &sub;
};
};
/delete-node/ &{/};
""",
"""
/dts-v1/;
/ {
};
""")

verify_error_endswith("""
Expand Down

0 comments on commit 44e12f8

Please sign in to comment.