[U-Boot] [PATCH 7/9] dtoc: Use items() to iterate over dictionaries in python 3.x

Paul Burton paul.burton at imgtec.com
Mon Sep 26 16:30:33 CEST 2016


In python 3.x the iteritems() method has been removed from dictionaries,
and the items() method does effectively the same thing. Convert the code
to attempt to use iteritems() to be efficient on python 2.x, but use
items() when that fails on python 3.x.

Signed-off-by: Paul Burton <paul.burton at imgtec.com>
---

 tools/dtoc/dtoc.py         | 24 ++++++++++++++++++++----
 tools/dtoc/fdt_fallback.py |  8 +++++++-
 2 files changed, 27 insertions(+), 5 deletions(-)

diff --git a/tools/dtoc/dtoc.py b/tools/dtoc/dtoc.py
index 518aa51..4ba8604 100755
--- a/tools/dtoc/dtoc.py
+++ b/tools/dtoc/dtoc.py
@@ -224,14 +224,22 @@ class DtbPlatdata:
             fields = {}
 
             # Get a list of all the valid properties in this node.
-            for name, prop in node.props.iteritems():
+            try:
+                iterator = node.props.iteritems()
+            except:
+                iterator = node.props.items()
+            for name, prop in iterator:
                 if name not in PROP_IGNORE_LIST and name[0] != '#':
                     fields[name] = copy.deepcopy(prop)
 
             # If we've seen this node_name before, update the existing struct.
             if node_name in structs:
                 struct = structs[node_name]
-                for name, prop in fields.iteritems():
+                try:
+                    iterator = fields.iteritems()
+                except:
+                    iterator = fields.items()
+                for name, prop in iterator:
                     oldprop = struct.get(name)
                     if oldprop:
                         oldprop.Widen(prop)
@@ -246,7 +254,11 @@ class DtbPlatdata:
         for node in self._valid_nodes:
             node_name = self.GetCompatName(node)
             struct = structs[node_name]
-            for name, prop in node.props.iteritems():
+            try:
+                iterator = node.props.iteritems()
+            except:
+                iterator = node.props.items()
+            for name, prop in iterator:
                 if name not in PROP_IGNORE_LIST and name[0] != '#':
                     prop.Widen(struct[name])
             upto += 1
@@ -298,7 +310,11 @@ class DtbPlatdata:
             var_name = Conv_name_to_c(node.name)
             self.Buf('static struct %s%s %s%s = {\n' %
                 (STRUCT_PREFIX, struct_name, VAL_PREFIX, var_name))
-            for pname, prop in node.props.iteritems():
+            try:
+                iterator = node.props.iteritems()
+            except:
+                iterator = node.props.items()
+            for pname, prop in iterator:
                 if pname in PROP_IGNORE_LIST or pname[0] == '#':
                     continue
                 ptype = TYPE_NAMES[prop.type]
diff --git a/tools/dtoc/fdt_fallback.py b/tools/dtoc/fdt_fallback.py
index 0c0ebbc..498cb66 100644
--- a/tools/dtoc/fdt_fallback.py
+++ b/tools/dtoc/fdt_fallback.py
@@ -58,7 +58,13 @@ class Node(NodeBase):
         This fills in the props and subnodes properties, recursively
         searching into subnodes so that the entire tree is built.
         """
-        for name, byte_list_str in self._fdt.GetProps(self.path).iteritems():
+        props = self._fdt.GetProps(self.path)
+        try:
+            iterator = props.iteritems()
+        except:
+            iterator = props.items()
+
+        for name, byte_list_str in iterator:
             prop = Prop(self, name, byte_list_str)
             self.props[name] = prop
 
-- 
2.10.0



More information about the U-Boot mailing list