[PATCH 4/4] pylibfdt: add FdtRo.getprop_or_none()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]



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>
---
 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')
-- 
2.34.1




[Index of Archives]     [Device Tree]     [Device Tree Spec]     [Linux Driver Backports]     [Video for Linux]     [Linux USB Devel]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Yosemite Backpacking]

  Powered by Linux