Skip to contentSkip to conversation

pylibfdt: add FdtRo.get_path()

PATCH Applied Luca Weiss 3 participants 7 messages Apr 19, 2022 – Apr 26, 2022 3 messages not in the archive

lore
Share & tools
lore

Luca Weiss

author
#0 lore raw

PATCH v4 1/1 pylibfdt: add FdtRo.get_path()

Add a new Python method wrapping fdt_get_path() from the C API.

Also add a test for the new method.

---
Changes since v3:
* add quiet parameter
* return integer error when error is quieted

Changes since v2:
* Remove arbitrary size limit
* Change size calculation to increase exponentially
* Add test to verify we still get exceptions with bad parameters

 pylibfdt/libfdt.i       | 28 ++++++++++++++++++++++++++++
 tests/pylibfdt_tests.py | 13 +++++++++++++
 2 files changed, 41 insertions(+)
pylibfdt/libfdt.i +28 -0
index ac70762..f9f7e7e 100644
@@ -443,6 +443,29 @@ class FdtRo(object):
         """
         return fdt_get_alias(self._fdt, name)
 
+    def get_path(self, nodeoffset, quiet=()):
+        """Get the full path of a node
+
+        Args:
+            nodeoffset: Node offset to check
+
+        Returns:
+            Full path to the node
+
+        Raises:
+            FdtException if an error occurs
+        """
+        size = 1024
+        while True:
+            ret, path = fdt_get_path(self._fdt, nodeoffset, size)
+            if ret == -NOSPACE:
+                size = size * 2
+                continue
+            err = check_err(ret, quiet)
+            if err:
+                return err
+            return path
+
     def parent_offset(self, nodeoffset, quiet=()):
         """Get the offset of a node's parent
 
@@ -1115,6 +1138,11 @@ typedef uint32_t fdt32_t;
         }
 }
 
+%include "cstring.i"
+
+%cstring_output_maxsize(char *buf, int buflen);
+int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen);
+
 /* We have both struct fdt_property and a function fdt_property() */
 %warnfilter(302) fdt_property;
 
tests/pylibfdt_tests.py +13 -0
index 5479363..68d6aaa 100644
@@ -348,6 +348,19 @@ class PyLibfdtBasicTests(unittest.TestCase):
         self.assertEqual("/subnode@1/subsubnode", self.fdt3.get_alias('ss1'))
         self.assertEqual("/subnode@1/subsubnode/subsubsubnode", self.fdt3.get_alias('sss1'))
 
+    def testGetPath(self):
+        """Test for the get_path() method"""
+        node = self.fdt.path_offset('/subnode@1')
+        node2 = self.fdt.path_offset('/subnode@1/subsubnode')
+        self.assertEqual("/subnode@1", self.fdt.get_path(node))
+        self.assertEqual("/subnode@1/subsubnode", self.fdt.get_path(node2))
+
+        with self.assertRaises(FdtException) as e:
+            self.fdt.get_path(-1)
+        self.assertEqual(e.exception.err, -libfdt.BADOFFSET)
+
+        self.assertEqual(-libfdt.BADOFFSET, self.fdt.get_path(-1, quiet=(libfdt.BADOFFSET,)))
+
     def testParentOffset(self):
         """Test for the parent_offset() method"""
         self.assertEqual(-libfdt.NOTFOUND,
Signature · 1 line
2.35.3

A message that is not in the archive (sent off-list or not yet received): 20220419194537.63170-1-luca-IfPCFPJWly+lVyrhU4qvOw@public.gmane.org

Reviewed-by
#2 lore raw

Hi Luca,

Quoted 11 lines · Luca Weiss

Add a new Python method wrapping fdt_get_path() from the C API.

Also add a test for the new method.

---
Changes since v3:
* add quiet parameter
* return integer error when error is quieted

Quoted 8 lines

Changes since v2:
* Remove arbitrary size limit
* Change size calculation to increase exponentially
* Add test to verify we still get exceptions with bad parameters

 pylibfdt/libfdt.i       | 28 ++++++++++++++++++++++++++++
 tests/pylibfdt_tests.py | 13 +++++++++++++
 2 files changed, 41 insertions(+)

You don't check for when it is output of space, but I think that is OK.

Quoted 72 lines
pylibfdt/libfdt.i +28 -0
index ac70762..f9f7e7e 100644
@@ -443,6 +443,29 @@ class FdtRo(object):
         """
         return fdt_get_alias(self._fdt, name)
 
+    def get_path(self, nodeoffset, quiet=()):
+        """Get the full path of a node
+
+        Args:
+            nodeoffset: Node offset to check
+
+        Returns:
+            Full path to the node
+
+        Raises:
+            FdtException if an error occurs
+        """
+        size = 1024
+        while True:
+            ret, path = fdt_get_path(self._fdt, nodeoffset, size)
+            if ret == -NOSPACE:
+                size = size * 2
+                continue
+            err = check_err(ret, quiet)
+            if err:
+                return err
+            return path
+
     def parent_offset(self, nodeoffset, quiet=()):
         """Get the offset of a node's parent
 
@@ -1115,6 +1138,11 @@ typedef uint32_t fdt32_t;
         }
 }
 
+%include "cstring.i"
+
+%cstring_output_maxsize(char *buf, int buflen);
+int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen);
+
 /* We have both struct fdt_property and a function fdt_property() */
 %warnfilter(302) fdt_property;
 
tests/pylibfdt_tests.py +13 -1
index 5479363..68d6aaa 100644
@@ -348,6 +348,19 @@ class PyLibfdtBasicTests(unittest.TestCase):
         self.assertEqual("/subnode@1/subsubnode", self.fdt3.get_alias('ss1'))
         self.assertEqual("/subnode@1/subsubnode/subsubsubnode", self.fdt3.get_alias('sss1'))
 
+    def testGetPath(self):
+        """Test for the get_path() method"""
+        node = self.fdt.path_offset('/subnode@1')
+        node2 = self.fdt.path_offset('/subnode@1/subsubnode')
+        self.assertEqual("/subnode@1", self.fdt.get_path(node))
+        self.assertEqual("/subnode@1/subsubnode", self.fdt.get_path(node2))
+
+        with self.assertRaises(FdtException) as e:
+            self.fdt.get_path(-1)
+        self.assertEqual(e.exception.err, -libfdt.BADOFFSET)
+
+        self.assertEqual(-libfdt.BADOFFSET, self.fdt.get_path(-1, quiet=(libfdt.BADOFFSET,)))
+
     def testParentOffset(self):
         """Test for the parent_offset() method"""
         self.assertEqual(-libfdt.NOTFOUND,
--
Quoted 2 lines

2.35.3

Regards,
Simon

A message that is not in the archive (sent off-list or not yet received): CAPnjgZ2izMUDXJ43jSOzXW7DKbuoN5j26MyKZaLn5Wt2gd28wQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

Applied
#4 lore raw
Quoted 19 lines · Simon Glass

Hi Luca,

Quoted 16 lines · Luca Weiss

Add a new Python method wrapping fdt_get_path() from the C API.

Also add a test for the new method.

---
Changes since v3:
* add quiet parameter
* return integer error when error is quieted

Changes since v2:
* Remove arbitrary size limit
* Change size calculation to increase exponentially
* Add test to verify we still get exceptions with bad parameters

Quoted 8 lines
Quoted 3 lines
 pylibfdt/libfdt.i       | 28 ++++++++++++++++++++++++++++
 tests/pylibfdt_tests.py | 13 +++++++++++++
 2 files changed, 41 insertions(+)

You don't check for when it is output of space, but I think that is OK.

Right, a testcase for that would be good, but it can be a follow up.

Applied.

Quoted 77 lines
Quoted 63 lines
pylibfdt/libfdt.i +28 -0
index ac70762..f9f7e7e 100644
@@ -443,6 +443,29 @@ class FdtRo(object):
         """
         return fdt_get_alias(self._fdt, name)
 
+    def get_path(self, nodeoffset, quiet=()):
+        """Get the full path of a node
+
+        Args:
+            nodeoffset: Node offset to check
+
+        Returns:
+            Full path to the node
+
+        Raises:
+            FdtException if an error occurs
+        """
+        size = 1024
+        while True:
+            ret, path = fdt_get_path(self._fdt, nodeoffset, size)
+            if ret == -NOSPACE:
+                size = size * 2
+                continue
+            err = check_err(ret, quiet)
+            if err:
+                return err
+            return path
+
     def parent_offset(self, nodeoffset, quiet=()):
         """Get the offset of a node's parent
 
@@ -1115,6 +1138,11 @@ typedef uint32_t fdt32_t;
         }
 }
 
+%include "cstring.i"
+
+%cstring_output_maxsize(char *buf, int buflen);
+int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen);
+
 /* We have both struct fdt_property and a function fdt_property() */
 %warnfilter(302) fdt_property;
 
tests/pylibfdt_tests.py +6 -0
index 5479363..68d6aaa 100644
@@ -348,6 +348,19 @@ class PyLibfdtBasicTests(unittest.TestCase):
         self.assertEqual("/subnode@1/subsubnode", self.fdt3.get_alias('ss1'))
         self.assertEqual("/subnode@1/subsubnode/subsubsubnode", self.fdt3.get_alias('sss1'))
 
+    def testGetPath(self):
+        """Test for the get_path() method"""
+        node = self.fdt.path_offset('/subnode@1')
+        node2 = self.fdt.path_offset('/subnode@1/subsubnode')
+        self.assertEqual("/subnode@1", self.fdt.get_path(node))
+        self.assertEqual("/subnode@1/subsubnode", self.fdt.get_path(node2))

tests/pylibfdt_tests.py class PyLibfdtBasicTests(unittest.TestCase): · line 366

Quoted 10 lines
tests/pylibfdt_tests.py +7 -0
+
+        with self.assertRaises(FdtException) as e:
+            self.fdt.get_path(-1)
+        self.assertEqual(e.exception.err, -libfdt.BADOFFSET)
+
+        self.assertEqual(-libfdt.BADOFFSET, self.fdt.get_path(-1, quiet=(libfdt.BADOFFSET,)))
+
     def testParentOffset(self):
         """Test for the parent_offset() method"""
         self.assertEqual(-libfdt.NOTFOUND,

Regards,
Simon

Signature · 4 lines
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
author
#5 lore raw

Hi Simon and David,

Quoted 23 lines · On Mittwoch, 20. April 2022 04:56:15 CEST David Gibson wrote:
Quoted 22 lines · Simon Glass

Hi Luca,

Quoted 18 lines · Luca Weiss

Add a new Python method wrapping fdt_get_path() from the C API.

Also add a test for the new method.

---
Changes since v3:
* add quiet parameter
* return integer error when error is quieted

Changes since v2:
* Remove arbitrary size limit
* Change size calculation to increase exponentially
* Add test to verify we still get exceptions with bad parameters

 pylibfdt/libfdt.i       | 28 ++++++++++++++++++++++++++++
 tests/pylibfdt_tests.py | 13 +++++++++++++
 2 files changed, 41 insertions(+)
Quoted 6 lines
Quoted 4 lines

You don't check for when it is output of space, but I think that is OK.

Right, a testcase for that would be good, but it can be a follow up.

I don't quite understand what you mean "is output of space".

Maybe you mean a test for when the string is longer than 1024 characters ("size = 1024") so the size increase gets triggered?

If yes, then I did test that manually before by setting the value absurdly low. Not sure how to properly check that in the tests though. Add a node with a super long name into the test dtb?

Quoted 2 lines

Applied.

Thanks!

Regards
Luca

Quoted 83 lines
Quoted 83 lines
Quoted 77 lines
pylibfdt/libfdt.i +28 -0
index ac70762..f9f7e7e 100644
@@ -443,6 +443,29 @@ class FdtRo(object):
         """
         return fdt_get_alias(self._fdt, name)
 
+    def get_path(self, nodeoffset, quiet=()):
+        """Get the full path of a node
+
+        Args:
+            nodeoffset: Node offset to check
+
+        Returns:
+            Full path to the node
+
+        Raises:
+            FdtException if an error occurs
+        """
+        size = 1024
+        while True:
+            ret, path = fdt_get_path(self._fdt, nodeoffset, size)
+            if ret == -NOSPACE:
+                size = size * 2
+                continue
+            err = check_err(ret, quiet)
+            if err:
+                return err
+            return path
+
 
     def parent_offset(self, nodeoffset, quiet=()):
         """Get the offset of a node's parent
@@ -1115,6 +1138,11 @@ typedef uint32_t fdt32_t;
 
         }
 
 }
 
+%include "cstring.i"
+
+%cstring_output_maxsize(char *buf, int buflen);
+int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int
 buflen);
+

/* We have both struct fdt_property and a function fdt_property() */ %warnfilter(302) fdt_property;

tests/pylibfdt_tests.py +6 -0
index 5479363..68d6aaa 100644
@@ -348,6 +348,19 @@ class PyLibfdtBasicTests(unittest.TestCase):
         self.assertEqual("/subnode@1/subsubnode",
         self.fdt3.get_alias('ss1'))
         self.assertEqual("/subnode@1/subsubnode/subsubsubnode",
         self.fdt3.get_alias('sss1'))> > 
+    def testGetPath(self):
+        """Test for the get_path() method"""
+        node = self.fdt.path_offset('/subnode@1')
+        node2 = self.fdt.path_offset('/subnode@1/subsubnode')
+        self.assertEqual("/subnode@1", self.fdt.get_path(node))
+        self.assertEqual("/subnode@1/subsubnode",

self.fdt.get_path(node2)) +

tests/pylibfdt_tests.py +5 -0
+        with self.assertRaises(FdtException) as e:
+            self.fdt.get_path(-1)
+        self.assertEqual(e.exception.err, -libfdt.BADOFFSET)
+
+        self.assertEqual(-libfdt.BADOFFSET, self.fdt.get_path(-1,

quiet=(libfdt.BADOFFSET,))) +

Quoted 3 lines

def testParentOffset(self):
"""Test for the parent_offset() method""" self.assertEqual(-libfdt.NOTFOUND,

Regards,
Simon

#6 lore raw

Hi Luca,

Quoted 27 lines · Luca Weiss

Hi Simon and David,

Quoted 23 lines · On Mittwoch, 20. April 2022 04:56:15 CEST David Gibson wrote:
Quoted 22 lines · Simon Glass

Hi Luca,

Quoted 18 lines · Luca Weiss

Add a new Python method wrapping fdt_get_path() from the C API.

Also add a test for the new method.

---
Changes since v3:
* add quiet parameter
* return integer error when error is quieted

Changes since v2:
* Remove arbitrary size limit
* Change size calculation to increase exponentially
* Add test to verify we still get exceptions with bad parameters

 pylibfdt/libfdt.i       | 28 ++++++++++++++++++++++++++++
 tests/pylibfdt_tests.py | 13 +++++++++++++
 2 files changed, 41 insertions(+)
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

You don't check for when it is output of space, but I think that is OK.

Right, a testcase for that would be good, but it can be a follow up.

I don't quite understand what you mean "is output of space".

is out of space (sorry)

Quoted 7 lines

Maybe you mean a test for when the string is longer than 1024 characters ("size = 1024") so the size increase gets triggered?

If yes, then I did test that manually before by setting the value absurdly low. Not sure how to properly check that in the tests though. Add a node with a super long name into the test dtb?

Another option is to add a parameter for the max size, just for testing, but I doubt David would like that!

You could have a SIZE_INCREMENT = 1024 at the top of the file, then change it in a try/finally clause in your test.

Quoted 8 lines
Quoted 3 lines

Applied.

Thanks!

Regards
Luca

Regards,
Simon

A message that is not in the archive (sent off-list or not yet received): CAPnjgZ2CjKLrCXEcBH0vTN77d7zk29dxnmRta4tTR_Th2arBzQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#8 lore raw
Quoted 45 lines · Simon Glass

Hi Luca,

Quoted 27 lines · Luca Weiss

Hi Simon and David,

Quoted 23 lines · On Mittwoch, 20. April 2022 04:56:15 CEST David Gibson wrote:
Quoted 22 lines · Simon Glass

Hi Luca,

Quoted 18 lines · Luca Weiss

Add a new Python method wrapping fdt_get_path() from the C API.

Also add a test for the new method.

---
Changes since v3:
* add quiet parameter
* return integer error when error is quieted

Changes since v2:
* Remove arbitrary size limit
* Change size calculation to increase exponentially
* Add test to verify we still get exceptions with bad parameters

 pylibfdt/libfdt.i       | 28 ++++++++++++++++++++++++++++
 tests/pylibfdt_tests.py | 13 +++++++++++++
 2 files changed, 41 insertions(+)
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

You don't check for when it is output of space, but I think that is OK.

Right, a testcase for that would be good, but it can be a follow up.

I don't quite understand what you mean "is output of space".

is out of space (sorry)

Quoted 4 lines

Maybe you mean a test for when the string is longer than 1024 characters ("size = 1024") so the size increase gets triggered?

Quoted 6 lines
Quoted 3 lines

If yes, then I did test that manually before by setting the value absurdly low. Not sure how to properly check that in the tests though. Add a node with a super long name into the test dtb?

Another option is to add a parameter for the max size, just for testing, but I doubt David would like that!

Actually I'm fine with adding a "hint" size as a parameter - as long as it's optional with a a sensible default.

Quoted 15 lines

You could have a SIZE_INCREMENT = 1024 at the top of the file, then change it in a try/finally clause in your test.

Quoted 8 lines
Quoted 3 lines

Applied.

Thanks!

Regards
Luca

Regards,
Simon

Signature · 4 lines
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
#9 lore raw
In reply to Luca Weiss
Quoted 31 lines · Luca Weiss

Hi Simon and David,

Quoted 28 lines · On Mittwoch, 20. April 2022 04:56:15 CEST David Gibson wrote:
Quoted 26 lines · Simon Glass

Hi Luca,

Quoted 10 lines · Luca Weiss

Add a new Python method wrapping fdt_get_path() from the C API.

Also add a test for the new method.

---
Changes since v3:
* add quiet parameter
* return integer error when error is quieted

Quoted 8 lines

Changes since v2:
* Remove arbitrary size limit
* Change size calculation to increase exponentially
* Add test to verify we still get exceptions with bad parameters

 pylibfdt/libfdt.i       | 28 ++++++++++++++++++++++++++++
 tests/pylibfdt_tests.py | 13 +++++++++++++
 2 files changed, 41 insertions(+)

You don't check for when it is output of space, but I think that is OK.

Quoted 6 lines
Quoted 1 line

Right, a testcase for that would be good, but it can be a follow up.

I don't quite understand what you mean "is output of space".

Maybe you mean a test for when the string is longer than 1024 characters ("size = 1024") so the size increase gets triggered?

That's what I was thinking, yes.

Quoted 3 lines

If yes, then I did test that manually before by setting the value absurdly low. Not sure how to properly check that in the tests though. Add a node with a super long name into the test dtb?

Not to the main "test_tree1" dtb, but you could use a different dtb with a large property. You could make one, or use an existing one with a long property - look for references to 'lorem.txt` in run_tests.sh for one example already creating and using a long property.

Alternatively you could add an extra parameter to the Python wrapper giving a "hint" size for the initial buffer.

Signature · 4 lines
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