Args:
bytes: New property value to set
"""
- self.bytes = str(bytes)
+ self.bytes = bytes
self.type, self.value = BytesToValue(bytes)
self.dirty = True
prop_name: Name of property to set
val: String value to set (will be \0-terminated in DT)
"""
- self.props[prop_name].SetData(val + chr(0))
+ if sys.version_info[0] >= 3: # pragma: no cover
+ val = bytes(val, 'utf-8')
+ self.props[prop_name].SetData(val + b'\0')
def AddString(self, prop_name, val):
"""Add a new string property to a node
prop_name: Name of property to add
val: String value of property
"""
- self.props[prop_name] = Prop(self, None, prop_name, val + chr(0))
+ if sys.version_info[0] >= 3: # pragma: no cover
+ val = bytes(val, 'utf-8')
+ self.props[prop_name] = Prop(self, None, prop_name, val + b'\0')
def AddSubnode(self, name):
"""Add a new subnode to the node
Fdt object containing the data
"""
fdt = Fdt(None)
- fdt._fdt_obj = libfdt.Fdt(bytearray(data))
+ fdt._fdt_obj = libfdt.Fdt(bytes(data))
return fdt
def LookupPhandle(self, phandle):
Returns:
The FDT contents as a string of bytes
"""
- return self._fdt_obj.as_bytearray()
+ return bytes(self._fdt_obj.as_bytearray())
def GetFdtObj(self):
"""Get the contents of the FDT
# Add 12, which is sizeof(struct fdt_property), to get to start of data
offset = prop.GetOffset() + 12
data = dtb.GetContents()[offset:offset + len(prop.value)]
- return prop, [chr(x) for x in data]
+ return prop, [tools.ToChar(x) for x in data]
class TestFdt(unittest.TestCase):
self.node.AddString('string', val)
self.dtb.Sync(auto_resize=True)
data = self.fdt.getprop(self.node.Offset(), 'string')
- self.assertEqual(val + '\0', data)
+ self.assertEqual(tools.ToBytes(val) + b'\0', data)
self.fdt.pack()
self.node.SetString('string', val + 'x')
self.node.SetString('string', val[:-1])
prop = self.node.props['string']
- prop.SetData(val)
+ prop.SetData(tools.ToBytes(val))
self.dtb.Sync(auto_resize=False)
data = self.fdt.getprop(self.node.Offset(), 'string')
- self.assertEqual(val, data)
+ self.assertEqual(tools.ToBytes(val), data)
self.node.AddEmptyProp('empty', 5)
self.dtb.Sync(auto_resize=True)
prop = self.node.props['empty']
- prop.SetData(val)
+ prop.SetData(tools.ToBytes(val))
self.dtb.Sync(auto_resize=False)
data = self.fdt.getprop(self.node.Offset(), 'empty')
- self.assertEqual(val, data)
+ self.assertEqual(tools.ToBytes(val), data)
- self.node.SetData('empty', '123')
- self.assertEqual('123', prop.bytes)
+ self.node.SetData('empty', b'123')
+ self.assertEqual(b'123', prop.bytes)
def testFromData(self):
dtb2 = fdt.Fdt.FromData(self.dtb.GetContents())
self.assertEqual(dtb, fdt_util.EnsureCompiled(dtb))
def testGetPlainBytes(self):
- self.assertEqual('fred', fdt_util.get_plain_bytes('fred'))
+ self.assertEqual(b'fred', fdt_util.get_plain_bytes('fred'))
def RunTestCoverage():
byte: A byte or str value
"""
return chr(byte) if type(byte) != str else byte
+
+def ToChars(byte_list):
+ """Convert a list of bytes to a str/bytes type
+
+ Args:
+ byte_list: List of ASCII values representing the string
+
+ Returns:
+ string made by concatenating all the ASCII values
+ """
+ return ''.join([chr(byte) for byte in byte_list])
+
+def ToBytes(string):
+ """Convert a str type into a bytes type
+
+ Args:
+ string: string to convert value
+
+ Returns:
+ Python 3: A bytes type
+ Python 2: A string type
+ """
+ if sys.version_info[0] >= 3:
+ return string.encode('utf-8')
+ return string