On Sat, Dec 25, 2021 at 02:25:58PM +0100, Luca Weiss wrote: > Add a new method that doesn't throw an exception when a property isn't > found but returns None instead. > > Also add a test for the new method. > > Signed-off-by: Luca Weiss <luca@xxxxxxxxx> I'd need a stronger rationale to apply this. It's not really doing anything that the existing getprop method can't, just presenting it in a slightly different form. > --- > pylibfdt/libfdt.i | 10 ++++++++++ > tests/pylibfdt_tests.py | 2 ++ > 2 files changed, 12 insertions(+) > > diff --git a/pylibfdt/libfdt.i b/pylibfdt/libfdt.i > index 5434d48..2c2fa5c 100644 > --- a/pylibfdt/libfdt.i > +++ b/pylibfdt/libfdt.i > @@ -419,6 +419,16 @@ class FdtRo(object): > return pdata > return Property(prop_name, bytearray(pdata[0])) > > + def getprop_or_none(self, nodeoffset, prop_name): > + """Get a property from a node, or None if it is not found > + > + See getprop() for the arguments. > + """ > + prop = self.getprop(nodeoffset, prop_name, [FDT_ERR_NOTFOUND]) > + if prop == -FDT_ERR_NOTFOUND: > + return None > + return prop > + > def get_phandle(self, nodeoffset): > """Get the phandle of a node > > diff --git a/tests/pylibfdt_tests.py b/tests/pylibfdt_tests.py > index bcd3daa..152e925 100644 > --- a/tests/pylibfdt_tests.py > +++ b/tests/pylibfdt_tests.py > @@ -219,6 +219,8 @@ class PyLibfdtBasicTests(unittest.TestCase): > self.fdt.getprop(root, 'missing') > self.assertEqual(e.exception.err, -libfdt.NOTFOUND) > > + self.assertIsNone(self.fdt.getprop_or_none(root, 'missing')) > + > node = self.fdt.path_offset('/subnode@1/subsubnode') > value = self.fdt.getprop(node, "compatible") > self.assertEqual(value, b'subsubnode1\0subsubnode\0') -- David Gibson | 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