Browse Source

pylibfdt: Proper handling of bytes/unicode strings and octal literals

Signed-off-by: Lumir Balhar <lbalhar@redhat.com>
Message-Id: <20190218164856.23861-1-frenzy@frenzy.cz>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
main
Lumir Balhar 6 years ago committed by David Gibson
parent
commit
4b68c6b360
  1. 14
      pylibfdt/libfdt.i
  2. 20
      tests/pylibfdt_tests.py

14
pylibfdt/libfdt.i

@ -669,7 +669,7 @@ class Fdt(FdtRo): @@ -669,7 +669,7 @@ class Fdt(FdtRo):
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)

@ -1074,12 +1074,20 @@ typedef uint32_t fdt32_t; @@ -1074,12 +1074,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() */

20
tests/pylibfdt_tests.py

@ -84,7 +84,7 @@ PHANDLE_2 = 0x2001 @@ -84,7 +84,7 @@ PHANDLE_2 = 0x2001

TEST_STRING_1 = 'hello world'
TEST_STRING_2 = 'hi world'
TEST_STRING_3 = u'unicode ' + unichr(467)
TEST_STRING_3 = u'unicode \u01d3'


def get_err(err_code):
@ -107,7 +107,7 @@ def _ReadFdt(fname): @@ -107,7 +107,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())

class PyLibfdtBasicTests(unittest.TestCase):
"""Test class for basic pylibfdt access functions
@ -164,7 +164,7 @@ class PyLibfdtBasicTests(unittest.TestCase): @@ -164,7 +164,7 @@ class PyLibfdtBasicTests(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):
@ -239,7 +239,7 @@ class PyLibfdtBasicTests(unittest.TestCase): @@ -239,7 +239,7 @@ class PyLibfdtBasicTests(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)
@ -252,7 +252,7 @@ class PyLibfdtBasicTests(unittest.TestCase): @@ -252,7 +252,7 @@ class PyLibfdtBasicTests(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))

@ -262,7 +262,7 @@ class PyLibfdtBasicTests(unittest.TestCase): @@ -262,7 +262,7 @@ class PyLibfdtBasicTests(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"""
@ -591,7 +591,7 @@ class PyLibfdtSwTests(unittest.TestCase): @@ -591,7 +591,7 @@ class PyLibfdtSwTests(unittest.TestCase):

# Make sure we can read from the tree too
node = sw.path_offset('/subnode@1')
self.assertEqual('subnode1' + chr(0), sw.getprop(node, 'compatible'))
self.assertEqual(b'subnode1\0', sw.getprop(node, 'compatible'))

# Make sure we did at least two resizes
self.assertTrue(len(fdt.as_bytearray()) > FdtSw.INC_SIZE * 2)
@ -609,15 +609,15 @@ class PyLibfdtRoTests(unittest.TestCase): @@ -609,15 +609,15 @@ class PyLibfdtRoTests(unittest.TestCase):

def setUp(self):
"""Read in the device tree we use for testing"""
self.fdt = libfdt.FdtRo(open('test_tree1.dtb').read())
self.fdt = libfdt.FdtRo(open('test_tree1.dtb', mode='rb').read())

def testAccess(self):
"""Basic sanity check for the FdtRo class"""
node = self.fdt.path_offset('/subnode@1')
self.assertEqual('subnode1' + chr(0),
self.assertEqual(b'subnode1\0',
self.fdt.getprop(node, 'compatible'))
node = self.fdt.first_subnode(node)
self.assertEqual('this is a placeholder string\0string2\0',
self.assertEqual(b'this is a placeholder string\0string2\0',
self.fdt.getprop(node, 'placeholder'))



Loading…
Cancel
Save