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]



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'

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

>  class PyLibfdtTests(unittest.TestCase):
>      """Test class for pylibfdt
> @@ -131,7 +134,7 @@ class PyLibfdtTests(unittest.TestCase):
>      def testBadFdt(self):
>          """Check that a filename provided accidentally is not accepted"""
>          with self.assertRaises(FdtException) as e:
> -            fdt = libfdt.Fdt('a string')
> +            fdt = libfdt.Fdt(b'a string')
>          self.assertEquals(e.exception.err, -libfdt.BADMAGIC)
>  
>      def testSubnodeOffset(self):
> @@ -206,7 +209,7 @@ class PyLibfdtTests(unittest.TestCase):
>          poffset = self.fdt.first_property_offset(root)
>          prop = self.fdt.get_property_by_offset(poffset)
>          self.assertEquals(prop.name, 'compatible')
> -        self.assertEquals(prop, 'test_tree1\0')
> +        self.assertEquals(prop, b'test_tree1\0')
>  
>          with self.assertRaises(FdtException) as e:
>              self.fdt.get_property_by_offset(-2)
> @@ -219,7 +222,7 @@ class PyLibfdtTests(unittest.TestCase):
>          """Check that we can read the contents of a property by name"""
>          root = self.fdt.path_offset('/')
>          value = self.fdt.getprop(root, "compatible")
> -        self.assertEquals(value, 'test_tree1\0')
> +        self.assertEquals(value, b'test_tree1\0')
>          self.assertEquals(-libfdt.NOTFOUND, self.fdt.getprop(root, 'missing',
>                                                               QUIET_NOTFOUND))
>  
> @@ -229,7 +232,7 @@ class PyLibfdtTests(unittest.TestCase):
>  
>          node = self.fdt.path_offset('/subnode@1/subsubnode')
>          value = self.fdt.getprop(node, "compatible")
> -        self.assertEquals(value, 'subsubnode1\0subsubnode\0')
> +        self.assertEquals(value, b'subsubnode1\0subsubnode\0')
>  
>      def testStrError(self):
>          """Check that we can get an error string"""
> @@ -389,7 +392,7 @@ class PyLibfdtTests(unittest.TestCase):
>          self.assertEquals(2, self.fdt.num_mem_rsv())
>          self.assertEquals([ 0xdeadbeef00000000, 0x100000],
>                            self.fdt.get_mem_rsv(0))
> -        self.assertEquals([123456789, 010000], self.fdt.get_mem_rsv(1))
> +        self.assertEquals([123456789, 0o10000], self.fdt.get_mem_rsv(1))
>  
>      def testEmpty(self):
>          """Test that we can create an empty tree"""

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