Re: [PATCH 1/4] pylibfdt: add Property.as_stringlist()

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



On Sat, Dec 25, 2021 at 7:26 AM Luca Weiss <luca@xxxxxxxxx> wrote:
>
> Add a new method for decoding a string list property, useful for e.g.
> the "reg-names" property.
>
> Also add a test for the new method.
>
> Signed-off-by: Luca Weiss <luca@xxxxxxxxx>
> ---
>  pylibfdt/libfdt.i       | 7 +++++++
>  tests/pylibfdt_tests.py | 8 ++++++++
>  2 files changed, 15 insertions(+)
>
> diff --git a/pylibfdt/libfdt.i b/pylibfdt/libfdt.i
> index 9ccc57b..c81b504 100644
> --- a/pylibfdt/libfdt.i
> +++ b/pylibfdt/libfdt.i
> @@ -724,6 +724,13 @@ class Property(bytearray):
>              raise ValueError('Property contains embedded nul characters')
>          return self[:-1].decode('utf-8')
>
> +    def as_stringlist(self):
> +        """Unicode is supported by decoding from UTF-8"""
> +        if self[-1] != 0:
> +            raise ValueError('Property lacks nul termination')
> +        parts = self[:-1].split(b'\x00')
> +        return list(map(lambda x: x.decode('utf-8'), parts))

Doesn't this result in multiple decode() calls when a single one would work:

return data[:-1].decode(encoding='ascii').split('\0')

Rob



[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