[U-Boot] [PATCH 06/24] dtoc: Use byte type instead of str in fdt

Simon Glass sjg at chromium.org
Sat May 18 04:00:36 UTC 2019


In Python 3 bytes and str are separate types. Use bytes to ensure that
the code functions correctly with Python 3.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

 tools/dtoc/fdt.py      | 14 +++++++++-----
 tools/dtoc/test_fdt.py | 18 +++++++++---------
 tools/patman/tools.py  | 25 +++++++++++++++++++++++++
 3 files changed, 43 insertions(+), 14 deletions(-)

diff --git a/tools/dtoc/fdt.py b/tools/dtoc/fdt.py
index d539248430a..cbd9cbabe31 100644
--- a/tools/dtoc/fdt.py
+++ b/tools/dtoc/fdt.py
@@ -193,7 +193,7 @@ class Prop:
         Args:
             bytes: New property value to set
         """
-        self.bytes = str(bytes)
+        self.bytes = bytes
         self.type, self.value = BytesToValue(bytes)
         self.dirty = True
 
@@ -398,7 +398,9 @@ class Node:
             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:
+            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
@@ -410,7 +412,9 @@ class 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:
+            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
@@ -496,7 +500,7 @@ class Fdt:
             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):
@@ -586,7 +590,7 @@ class Fdt:
         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
diff --git a/tools/dtoc/test_fdt.py b/tools/dtoc/test_fdt.py
index 79f97d8013c..3cd34b745ed 100755
--- a/tools/dtoc/test_fdt.py
+++ b/tools/dtoc/test_fdt.py
@@ -47,7 +47,7 @@ def _GetPropertyValue(dtb, node, prop_name):
     # 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):
@@ -383,7 +383,7 @@ class TestProp(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')
@@ -393,21 +393,21 @@ class TestProp(unittest.TestCase):
         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())
@@ -508,7 +508,7 @@ class TestFdtUtil(unittest.TestCase):
         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():
diff --git a/tools/patman/tools.py b/tools/patman/tools.py
index 976670ef006..bdc1953936c 100644
--- a/tools/patman/tools.py
+++ b/tools/patman/tools.py
@@ -317,3 +317,28 @@ def ToChar(byte):
         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
-- 
2.21.0.1020.gf2820cf01a-goog



More information about the U-Boot mailing list