Re: [PATCH 2/5] pylibfdt: Proper handling of bytes/unicode strings and octal literals

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



Hi,

On 12 July 2018 at 19:01, David Gibson <david@xxxxxxxxxxxxxxxxxxxxx> wrote:
> On Thu, Jul 12, 2018 at 04:10:42PM +0200, frenzy@xxxxxxxxx wrote:
>> From: Lumir Balhar <lbalhar@xxxxxxxxxx>
>>
>> Signed-off-by: Lumir Balhar <lbalhar@xxxxxxxxxx>
>> ---
>>  pylibfdt/libfdt.i       | 14 +++++++++++---
>>  tests/pylibfdt_tests.py | 17 ++++++++++-------
>>  2 files changed, 21 insertions(+), 10 deletions(-)
>>
>> diff --git a/pylibfdt/libfdt.i b/pylibfdt/libfdt.i
>> index aed5390..88d443d 100644
>> --- a/pylibfdt/libfdt.i
>> +++ b/pylibfdt/libfdt.i
>> @@ -624,7 +624,7 @@ class Fdt:
>>          Raises:
>>              FdtException if no parent found or other error occurs
>>          """
>> -        val = val.encode('utf-8') + '\0'
>> +        val = val.encode('utf-8') + b'\0'
>>          return check_err(fdt_setprop(self._fdt, nodeoffset, prop_name,
>>                                       val, len(val)), quiet)
>>
>> @@ -749,12 +749,20 @@ typedef uint32_t fdt32_t;
>>       if (!$1)
>>               $result = Py_None;
>>       else
>> -             $result = Py_BuildValue("s#", $1, *arg4);
>> +        %#if PY_VERSION_HEX >= 0x03000000
>> +            $result = Py_BuildValue("y#", $1, *arg4);
>> +        %#else
>> +            $result = Py_BuildValue("s#", $1, *arg4);
>> +        %#endif
>>  }
>>
>>  /* typemap used for fdt_setprop() */
>>  %typemap(in) (const void *val) {
>> -    $1 = PyString_AsString($input);   /* char *str */
>> +    %#if PY_VERSION_HEX >= 0x03000000
>> +        $1 = PyBytes_AsString($input);
>> +    %#else
>> +        $1 = PyString_AsString($input);   /* char *str */
>> +    %#endif
>>  }
>>
>>  /* typemaps used for fdt_next_node() */
>> diff --git a/tests/pylibfdt_tests.py b/tests/pylibfdt_tests.py
>> index 9f3e55a..e1204f8 100644
>> --- a/tests/pylibfdt_tests.py
>> +++ b/tests/pylibfdt_tests.py
>> @@ -69,7 +69,10 @@ TEST_VALUE64_1 = (TEST_VALUE64_1H << 32) | TEST_VALUE64_1L
>>
>>  TEST_STRING_1 = 'hello world'
>>  TEST_STRING_2 = 'hi world'
>> -TEST_STRING_3 = u'unicode ' + unichr(467)
>> +try:
>> +    TEST_STRING_3 = u'unicode ' + unichr(467)
>> +except NameError:
>> +    TEST_STRING_3 = u'unicode ' + chr(467)
>>
>
> Doing this with a try/except seems pretty yucky.  What about:
>
> TEST_STRING_# = u'unicode \u01d3'

I agree it's better to avoid exceptions when handling normal cases. I
worry that it makes things non-deterministic.

If the above doesn't work in some cases then perhaps we can check the
Python version.

It's such a shame that Python has then backwards-incompatible changes,
but I suppose that has been discussed to death elsewhere, and we have
no alternative.

>
>>  def get_err(err_code):
>> @@ -92,7 +95,7 @@ def _ReadFdt(fname):
>>      Returns:
>>          Fdt bytearray suitable for passing to libfdt functions
>>      """
>> -    return libfdt.Fdt(open(fname).read())
>> +    return libfdt.Fdt(open(fname, mode='rb').read())
>
> Actually a correct fix regardless of python 2/3 issues.

and perhaps could be a separate patch.

Regards,
Simon
--
To unsubscribe from this list: send the line "unsubscribe devicetree-compiler" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[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