devicetree-compiler.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3] pylibfdt: add FdtRo.get_path()
@ 2022-01-26 15:59 Luca Weiss
       [not found] ` <20220126155933.865297-1-luca-IfPCFPJWly+lVyrhU4qvOw@public.gmane.org>
  0 siblings, 1 reply; 4+ messages in thread
From: Luca Weiss @ 2022-01-26 15:59 UTC (permalink / raw)
  To: devicetree-compiler-u79uwXL29TY76Z2rM5mHXA; +Cc: Luca Weiss

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

Also add a test for the new method.

Signed-off-by: Luca Weiss <luca-IfPCFPJWly+lVyrhU4qvOw@public.gmane.org>
---
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       | 26 ++++++++++++++++++++++++++
 tests/pylibfdt_tests.py | 11 +++++++++++
 2 files changed, 37 insertions(+)

diff --git a/pylibfdt/libfdt.i b/pylibfdt/libfdt.i
index ac70762..2f085c8 100644
--- a/pylibfdt/libfdt.i
+++ b/pylibfdt/libfdt.i
@@ -443,6 +443,27 @@ class FdtRo(object):
         """
         return fdt_get_alias(self._fdt, name)
 
+    def get_path(self, nodeoffset):
+        """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
+            check_err(ret)
+            return path
+
     def parent_offset(self, nodeoffset, quiet=()):
         """Get the offset of a node's parent
 
@@ -1115,6 +1136,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;
 
diff --git a/tests/pylibfdt_tests.py b/tests/pylibfdt_tests.py
index 5479363..5435881 100644
--- a/tests/pylibfdt_tests.py
+++ b/tests/pylibfdt_tests.py
@@ -348,6 +348,17 @@ 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)
+
     def testParentOffset(self):
         """Test for the parent_offset() method"""
         self.assertEqual(-libfdt.NOTFOUND,
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v3] pylibfdt: add FdtRo.get_path()
       [not found] ` <20220126155933.865297-1-luca-IfPCFPJWly+lVyrhU4qvOw@public.gmane.org>
@ 2022-01-26 19:29   ` Simon Glass
       [not found]     ` <CAPnjgZ2joJOwVN8D46ehZAmO0EH26fiYgz2zPdq19nW4iFzgow-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
  0 siblings, 1 reply; 4+ messages in thread
From: Simon Glass @ 2022-01-26 19:29 UTC (permalink / raw)
  To: Luca Weiss; +Cc: Devicetree Compiler

Hi Luca,

On Wed, 26 Jan 2022 at 09:01, Luca Weiss <luca-IfPCFPJWly+lVyrhU4qvOw@public.gmane.org> wrote:
>
> Add a new Python method wrapping fdt_get_path() from the C API.
>
> Also add a test for the new method.
>
> Signed-off-by: Luca Weiss <luca-IfPCFPJWly+lVyrhU4qvOw@public.gmane.org>
> ---
> 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       | 26 ++++++++++++++++++++++++++
>  tests/pylibfdt_tests.py | 11 +++++++++++
>  2 files changed, 37 insertions(+)
>
> diff --git a/pylibfdt/libfdt.i b/pylibfdt/libfdt.i
> index ac70762..2f085c8 100644
> --- a/pylibfdt/libfdt.i
> +++ b/pylibfdt/libfdt.i
> @@ -443,6 +443,27 @@ class FdtRo(object):
>          """
>          return fdt_get_alias(self._fdt, name)
>
> +    def get_path(self, nodeoffset):

I think this should have:

quiet=()

as another parameter, to pass to check_err() below. See other examples.

> +        """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

nit:

size *= 2

> +                continue
> +            check_err(ret)

Should pass 'quiet' return the error here (see other examples)

> +            return path
> +
>      def parent_offset(self, nodeoffset, quiet=()):
>          """Get the offset of a node's parent
>
> @@ -1115,6 +1136,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;
>
> diff --git a/tests/pylibfdt_tests.py b/tests/pylibfdt_tests.py
> index 5479363..5435881 100644
> --- a/tests/pylibfdt_tests.py
> +++ b/tests/pylibfdt_tests.py
> @@ -348,6 +348,17 @@ 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)

Perhaps add a test for non-exception case too

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

REgards,
Simon

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v3] pylibfdt: add FdtRo.get_path()
       [not found]     ` <CAPnjgZ2joJOwVN8D46ehZAmO0EH26fiYgz2zPdq19nW4iFzgow-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
@ 2022-01-26 19:50       ` Luca Weiss
  2022-01-26 21:16         ` Simon Glass
  0 siblings, 1 reply; 4+ messages in thread
From: Luca Weiss @ 2022-01-26 19:50 UTC (permalink / raw)
  To: Simon Glass; +Cc: Devicetree Compiler

On Mittwoch, 26. Jänner 2022 20:29:10 CET Simon Glass wrote:
> Hi Luca,
> 
> On Wed, 26 Jan 2022 at 09:01, Luca Weiss <luca-IfPCFPJWly+lVyrhU4qvOw@public.gmane.org> wrote:
> > Add a new Python method wrapping fdt_get_path() from the C API.
> > 
> > Also add a test for the new method.
> > 
> > Signed-off-by: Luca Weiss <luca-IfPCFPJWly+lVyrhU4qvOw@public.gmane.org>
> > ---
> > 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       | 26 ++++++++++++++++++++++++++
> >  tests/pylibfdt_tests.py | 11 +++++++++++
> >  2 files changed, 37 insertions(+)
> > 
> > diff --git a/pylibfdt/libfdt.i b/pylibfdt/libfdt.i
> > index ac70762..2f085c8 100644
> > --- a/pylibfdt/libfdt.i
> > +++ b/pylibfdt/libfdt.i
> > 
> > @@ -443,6 +443,27 @@ class FdtRo(object):
> >          """
> >          return fdt_get_alias(self._fdt, name)
> > 
> > +    def get_path(self, nodeoffset):
> I think this should have:
> 
> quiet=()
> 
> as another parameter, to pass to check_err() below. See other examples.

see below

> 
> > +        """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
> 
> nit:
> 
> size *= 2
> 
> > +                continue
> > +            check_err(ret)
> 
> Should pass 'quiet' return the error here (see other examples)

This will return an empty string then in an error case, is that ok?

> 
> > +            return path
> > +
> > 
> >      def parent_offset(self, nodeoffset, quiet=()):
> >          """Get the offset of a node's parent
> > 
> > @@ -1115,6 +1136,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;
> > 
> > diff --git a/tests/pylibfdt_tests.py b/tests/pylibfdt_tests.py
> > index 5479363..5435881 100644
> > --- a/tests/pylibfdt_tests.py
> > +++ b/tests/pylibfdt_tests.py
> > 
> > @@ -348,6 +348,17 @@ 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)
> 
> Perhaps add a test for non-exception case too

You mean with the quiet= parameter? Or something else?

Below is proposed diff on top of v3 (maybe badly formatted here though):

diff --git a/pylibfdt/libfdt.i b/pylibfdt/libfdt.i
index 2f085c8..25c7cec 100644
--- a/pylibfdt/libfdt.i
+++ b/pylibfdt/libfdt.i
@@ -443,7 +443,7 @@ class FdtRo(object):
         """
         return fdt_get_alias(self._fdt, name)
 
-    def get_path(self, nodeoffset):
+    def get_path(self, nodeoffset, quiet=()):
         """Get the full path of a node
 
         Args:
@@ -461,7 +461,7 @@ class FdtRo(object):
             if ret == -NOSPACE:
                 size = size * 2
                 continue
-            check_err(ret)
+            check_err(ret, quiet)
             return path
 
     def parent_offset(self, nodeoffset, quiet=()):
diff --git a/tests/pylibfdt_tests.py b/tests/pylibfdt_tests.py
index 5435881..676f221 100644
--- a/tests/pylibfdt_tests.py
+++ b/tests/pylibfdt_tests.py
@@ -359,6 +359,8 @@ class PyLibfdtBasicTests(unittest.TestCase):
             self.fdt.get_path(-1)
         self.assertEqual(e.exception.err, -libfdt.BADOFFSET)
 
+        self.assertEqual("", self.fdt.get_path(-1, 
quiet=(libfdt.BADOFFSET,)))
+
     def testParentOffset(self):
         """Test for the parent_offset() method"""
         self.assertEqual(-libfdt.NOTFOUND,

Looks okay?

Regards
Luca

p.s. funnily enough I don't even use this function in my own script anymore, I 
only used it a bit during development for debugging :D But happy to get this 
included into pylibfdt

> 
> > +
> > 
> >      def testParentOffset(self):
> >          """Test for the parent_offset() method"""
> >          self.assertEqual(-libfdt.NOTFOUND,
> > 
> > --
> > 2.34.1
> 
> REgards,
> Simon





^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v3] pylibfdt: add FdtRo.get_path()
  2022-01-26 19:50       ` Luca Weiss
@ 2022-01-26 21:16         ` Simon Glass
  0 siblings, 0 replies; 4+ messages in thread
From: Simon Glass @ 2022-01-26 21:16 UTC (permalink / raw)
  To: Luca Weiss; +Cc: Devicetree Compiler

Hi Luca,

On Wed, 26 Jan 2022 at 12:50, Luca Weiss <luca-IfPCFPJWly+lVyrhU4qvOw@public.gmane.org> wrote:
>
> On Mittwoch, 26. Jänner 2022 20:29:10 CET Simon Glass wrote:
> > Hi Luca,
> >
> > On Wed, 26 Jan 2022 at 09:01, Luca Weiss <luca-IfPCFPJWly+lVyrhU4qvOw@public.gmane.org> wrote:
> > > Add a new Python method wrapping fdt_get_path() from the C API.
> > >
> > > Also add a test for the new method.
> > >
> > > Signed-off-by: Luca Weiss <luca-IfPCFPJWly+lVyrhU4qvOw@public.gmane.org>
> > > ---
> > > 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       | 26 ++++++++++++++++++++++++++
> > >  tests/pylibfdt_tests.py | 11 +++++++++++
> > >  2 files changed, 37 insertions(+)
> > >
> > > diff --git a/pylibfdt/libfdt.i b/pylibfdt/libfdt.i
> > > index ac70762..2f085c8 100644
> > > --- a/pylibfdt/libfdt.i
> > > +++ b/pylibfdt/libfdt.i
> > >
> > > @@ -443,6 +443,27 @@ class FdtRo(object):
> > >          """
> > >          return fdt_get_alias(self._fdt, name)
> > >
> > > +    def get_path(self, nodeoffset):
> > I think this should have:
> >
> > quiet=()
> >
> > as another parameter, to pass to check_err() below. See other examples.
>
> see below
>
> >
> > > +        """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
> >
> > nit:
> >
> > size *= 2
> >
> > > +                continue
> > > +            check_err(ret)
> >
> > Should pass 'quiet' return the error here (see other examples)
>
> This will return an empty string then in an error case, is that ok?

I think it should return the value of check_err(), i.e. an int.
Otherwise the real error cannot be obtained from the API.

>
> >
> > > +            return path
> > > +
> > >
> > >      def parent_offset(self, nodeoffset, quiet=()):
> > >          """Get the offset of a node's parent
> > >
> > > @@ -1115,6 +1136,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;
> > >
> > > diff --git a/tests/pylibfdt_tests.py b/tests/pylibfdt_tests.py
> > > index 5479363..5435881 100644
> > > --- a/tests/pylibfdt_tests.py
> > > +++ b/tests/pylibfdt_tests.py
> > >
> > > @@ -348,6 +348,17 @@ 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)
> >
> > Perhaps add a test for non-exception case too
>
> You mean with the quiet= parameter? Or something else?

Yes

>
> Below is proposed diff on top of v3 (maybe badly formatted here though):
>
> diff --git a/pylibfdt/libfdt.i b/pylibfdt/libfdt.i
> index 2f085c8..25c7cec 100644
> --- a/pylibfdt/libfdt.i
> +++ b/pylibfdt/libfdt.i
> @@ -443,7 +443,7 @@ class FdtRo(object):
>          """
>          return fdt_get_alias(self._fdt, name)
>
> -    def get_path(self, nodeoffset):
> +    def get_path(self, nodeoffset, quiet=()):
>          """Get the full path of a node
>
>          Args:
> @@ -461,7 +461,7 @@ class FdtRo(object):
>              if ret == -NOSPACE:
>                  size = size * 2
>                  continue
> -            check_err(ret)
> +            check_err(ret, quiet)

        err = check_err(ret, quiet)
        if err:
            return err

>              return path
>
>      def parent_offset(self, nodeoffset, quiet=()):
> diff --git a/tests/pylibfdt_tests.py b/tests/pylibfdt_tests.py
> index 5435881..676f221 100644
> --- a/tests/pylibfdt_tests.py
> +++ b/tests/pylibfdt_tests.py
> @@ -359,6 +359,8 @@ class PyLibfdtBasicTests(unittest.TestCase):
>              self.fdt.get_path(-1)
>          self.assertEqual(e.exception.err, -libfdt.BADOFFSET)
>
> +        self.assertEqual("", self.fdt.get_path(-1,

Yes, but it should return -libfdt.BADOFFSET

> quiet=(libfdt.BADOFFSET,)))
> +
>      def testParentOffset(self):
>          """Test for the parent_offset() method"""
>          self.assertEqual(-libfdt.NOTFOUND,
>
> Looks okay?
>
> Regards
> Luca
>
> p.s. funnily enough I don't even use this function in my own script anymore, I
> only used it a bit during development for debugging :D But happy to get this
> included into pylibfdt

Yes I think it's a useful function!


- Simon

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-01-26 21:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-01-26 15:59 [PATCH v3] pylibfdt: add FdtRo.get_path() Luca Weiss
     [not found] ` <20220126155933.865297-1-luca-IfPCFPJWly+lVyrhU4qvOw@public.gmane.org>
2022-01-26 19:29   ` Simon Glass
     [not found]     ` <CAPnjgZ2joJOwVN8D46ehZAmO0EH26fiYgz2zPdq19nW4iFzgow-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2022-01-26 19:50       ` Luca Weiss
2022-01-26 21:16         ` Simon Glass

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).