Re: [PATCH 07/10] pylibfdt: Add functions to set and get properties as strings

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



On Wed, May 23, 2018 at 10:03:43PM -0600, Simon Glass wrote:
> It is common to want to set a property to a nul-terminated string in a
> device tree. Add python methods to handle this.
> 
> Signed-off-by: Simon Glass <sjg@xxxxxxxxxxxx>

It's probably worth thinking about future Python3 compatibility here:
Python3 makes a firm distinction between "bytes" objects and "string"
(Unicode) objects.  So these functions would want to do the necessary
conversion here (with encoding forced to UTF-8, of course).

> ---
> 
>  pylibfdt/libfdt.i       | 24 ++++++++++++++++++++++++
>  tests/pylibfdt_tests.py | 11 +++++++++++
>  2 files changed, 35 insertions(+)
> 
> diff --git a/pylibfdt/libfdt.i b/pylibfdt/libfdt.i
> index 75868b8..5ecddba 100644
> --- a/pylibfdt/libfdt.i
> +++ b/pylibfdt/libfdt.i
> @@ -599,6 +599,27 @@ class Fdt:
>          return check_err(fdt_setprop_u64(self._fdt, nodeoffset, prop_name, val),
>                           quiet)
>  
> +    def setprop_str(self, nodeoffset, prop_name, val, quiet=()):
> +        """Set the string value of a property
> +
> +        The property is set to the string, with a nul terminator added
> +
> +        Args:
> +            nodeoffset: Node offset containing the property to create/update
> +            prop_name: Name of property
> +            val: Value to write (string without nul terminator)
> +            quiet: Errors to ignore (empty to raise on all errors)
> +
> +        Returns:
> +            Error code, or 0 if OK
> +
> +        Raises:
> +            FdtException if no parent found or other error occurs
> +        """
> +        val += '\0'
> +        return check_err(fdt_setprop(self._fdt, nodeoffset, prop_name,
> +                                     val, len(val)), quiet)
> +
>      def delprop(self, nodeoffset, prop_name):
>          """Delete a property from a node
>  
> @@ -656,6 +677,9 @@ class Property(bytearray):
>  
>      def as_int64(self):
>          return self.as_cell('q')
> +
> +    def as_str(self):
> +        return self[:-1]

This should probably throw an exception if the last byte isn't '\0'.
And maybe if it has any embedded \0s as well.

>  %}
>  
>  %rename(fdt_property) fdt_property_func;
> diff --git a/tests/pylibfdt_tests.py b/tests/pylibfdt_tests.py
> index f75579f..497124b 100644
> --- a/tests/pylibfdt_tests.py
> +++ b/tests/pylibfdt_tests.py
> @@ -68,6 +68,7 @@ TEST_VALUE64_1L = 0x01abcdef
>  TEST_VALUE64_1 = (TEST_VALUE64_1H << 32) | TEST_VALUE64_1L
>  
>  TEST_STRING_1 = 'hello world'
> +TEST_STRING_2 = 'hi world'
>  
>  
>  def get_err(err_code):
> @@ -440,6 +441,16 @@ class PyLibfdtTests(unittest.TestCase):
>          self.assertEquals(struct.pack('>Q', TEST_VALUE64_1),
>                            self.fdt.getprop(node, prop))
>  
> +    def testSetPropStr(self):
> +        """Test that we can set a property to a particular string"""
> +        node = 0
> +        prop = 'prop-str'
> +        self.assertEquals(TEST_STRING_1,
> +                          self.fdt.getprop_obj(node, prop).as_str())
> +        self.fdt.setprop_str(node, prop, TEST_STRING_2)
> +        self.assertEquals(TEST_STRING_2,
> +                          self.fdt.getprop_obj(node, prop).as_str())
> +
>  
>  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