From 15db98a40022b11fc30f60f60d1ca09576a7898c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=AD=20Bol=C3=ADvar?= Date: Thu, 6 May 2021 16:25:33 -0700 Subject: [PATCH] dtlib: allow dangling aliases with DT(..., force=True) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As a first step towards being more forgiving on invalid inputs, allow string-valued aliases properties that do not point to valid nodes when the user requests permissiveness. Signed-off-by: Martí Bolívar --- .../python-devicetree/src/devicetree/dtlib.py | 11 +++++++++-- .../dts/python-devicetree/tests/test_dtlib.py | 18 +++++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/scripts/dts/python-devicetree/src/devicetree/dtlib.py b/scripts/dts/python-devicetree/src/devicetree/dtlib.py index c9084f08c1e..9d5f32cf711 100644 --- a/scripts/dts/python-devicetree/src/devicetree/dtlib.py +++ b/scripts/dts/python-devicetree/src/devicetree/dtlib.py @@ -1684,8 +1684,15 @@ class DT: _err("/aliases: alias property name '{}' should include " "only characters from [0-9a-z-]".format(prop.name)) - # Property.to_path() already checks that the node exists - alias2node[prop.name] = prop.to_path() + # Property.to_path() checks that the node exists, has + # the right type, etc. Swallow errors for invalid + # aliases with self._force. + try: + alias2node[prop.name] = prop.to_path() + except DTError: + if self._force: + continue + raise self.alias2node = alias2node diff --git a/scripts/dts/python-devicetree/tests/test_dtlib.py b/scripts/dts/python-devicetree/tests/test_dtlib.py index c6a07765608..13e5544da6a 100644 --- a/scripts/dts/python-devicetree/tests/test_dtlib.py +++ b/scripts/dts/python-devicetree/tests/test_dtlib.py @@ -24,13 +24,15 @@ from devicetree import dtlib # - to run a particular test function or functions, use # '-k test_function_pattern_goes_here' -def parse(dts, include_path=()): - '''Parse a DTS string 'dts', using the given include path.''' +def parse(dts, include_path=(), **kwargs): + '''Parse a DTS string 'dts', using the given include path. + + Any kwargs are passed on to DT().''' fd, path = tempfile.mkstemp(prefix='pytest-', suffix='.dts') try: os.write(fd, dts.encode('utf-8')) - return dtlib.DT(path, include_path) + return dtlib.DT(path, include_path, **kwargs) finally: os.close(fd) os.unlink(path) @@ -2250,3 +2252,13 @@ l1: l2: &foo { / { }; """) + +def test_dangling_alias(): + dt = parse(''' +/dts-v1/; + +/ { + aliases { foo = "/missing"; }; +}; +''', force=True) + assert dt.get_node('/aliases').props['foo'].to_string() == '/missing'