Re: [PATCH v2 09/10] pylibfdt: Support setting the name of a node

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



On Wed, Jun 06, 2018 at 03:37:08PM -0600, Simon Glass wrote:
> Add a method to call fdt_set_name().
> 
> Signed-off-by: Simon Glass <sjg@xxxxxxxxxxxx>
> ---
> 
> Changes in v2:
> - Check for an invalid C string (embedded nul)
> 
>  pylibfdt/libfdt.i       | 17 +++++++++++++++++
>  tests/pylibfdt_tests.py | 11 +++++++++++
>  2 files changed, 28 insertions(+)
> 
> diff --git a/pylibfdt/libfdt.i b/pylibfdt/libfdt.i
> index ae63263..566a401 100644
> --- a/pylibfdt/libfdt.i
> +++ b/pylibfdt/libfdt.i
> @@ -555,6 +555,23 @@ class Fdt:
>          """
>          return check_err(fdt_parent_offset(self._fdt, nodeoffset), quiet)
>  
> +    def set_name(self, nodeoffset, name, quiet=()):
> +        """Set the name of a node
> +
> +        Args:
> +            nodeoffset: Node offset of node to update
> +            name: New node name
> +
> +        Returns:
> +            Error code, or 0 if OK
> +
> +        Raises:
> +            FdtException if no parent found or other error occurs
> +        """
> +        if chr(0) in name[:-1]:
> +            raise ValueError('Property contains embedded nul characters')
> +        return check_err(fdt_set_name(self._fdt, nodeoffset, name), quiet)

Hmm.. so, this could also get a bit messier when you consider Python
3.  In that case you'd want this to take a str() not a bytes(), which
means you'd need to encode it before sending it to the C side.

I don't see a really easy way to fix that up which isn't likely to
just introduce untestable stuff, so I guess I'll apply this for now.
Just be aware that it'll need attention for Python 3.

>      def setprop(self, nodeoffset, prop_name, val, quiet=()):
>          """Set the value of a property
>  
> diff --git a/tests/pylibfdt_tests.py b/tests/pylibfdt_tests.py
> index c7b82be..18a583d 100644
> --- a/tests/pylibfdt_tests.py
> +++ b/tests/pylibfdt_tests.py
> @@ -467,6 +467,17 @@ class PyLibfdtTests(unittest.TestCase):
>          self.assertEquals(TEST_STRING_3,
>                            libfdt.as_str(self.fdt.getprop(node, prop)))
>  
> +    def testSetName(self):
> +        """Test that we can update a node name"""
> +        node = self.fdt.path_offset('/subnode@1')
> +        old_val = self.fdt.get_name(node)
> +        self.fdt.set_name(node, 'test')
> +        self.assertEquals('test', self.fdt.get_name(node))
> +
> +        with self.assertRaises(ValueError) as e:
> +            self.fdt.set_name(node, 'some\0name')
> +        self.assertIn('embedded nul', str(e.exception))
> +
>  
>  if __name__ == "__main__":
>      unittest.main()

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


[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