summaryrefslogtreecommitdiff
path: root/src/python/m5/util
diff options
context:
space:
mode:
authorAndreas Sandberg <andreas.sandberg@arm.com>2019-01-26 09:19:22 +0000
committerAndreas Sandberg <andreas.sandberg@arm.com>2019-02-22 10:47:36 +0000
commit7d71f6641fcb660de0f003e2c028b464d7116ca1 (patch)
treeeab821617b26ce34b0dc834f2e0f11cfee67c2a0 /src/python/m5/util
parent8e5d168332c4ac3851aee4f815cff0b62b37cc40 (diff)
downloadgem5-7d71f6641fcb660de0f003e2c028b464d7116ca1.tar.xz
python: Make iterator handling Python 3 compatible
Many functions that used to return lists (e.g., dict.items()) now return iterators and their iterator counterparts (e.g., dict.iteritems()) have been removed. Switch calls to the Python 2.7 iterator methods to use the Python 3 equivalent and add explicit list conversions where necessary. Change-Id: I0c18114955af8f4932d81fb689a0adb939dafaba Signed-off-by: Andreas Sandberg <andreas.sandberg@arm.com> Reviewed-on: https://gem5-review.googlesource.com/c/15992 Reviewed-by: Juha Jäykkä <juha.jaykka@arm.com> Reviewed-by: Jason Lowe-Power <jason@lowepower.com>
Diffstat (limited to 'src/python/m5/util')
-rw-r--r--src/python/m5/util/code_formatter.py4
-rw-r--r--src/python/m5/util/dot_writer.py2
-rw-r--r--src/python/m5/util/jobfile.py8
-rw-r--r--src/python/m5/util/multidict.py21
-rw-r--r--src/python/m5/util/smartdict.py10
-rw-r--r--src/python/m5/util/sorteddict.py29
-rw-r--r--src/python/m5/util/terminal.py4
7 files changed, 27 insertions, 51 deletions
diff --git a/src/python/m5/util/code_formatter.py b/src/python/m5/util/code_formatter.py
index 129fbd0e3..c845c9021 100644
--- a/src/python/m5/util/code_formatter.py
+++ b/src/python/m5/util/code_formatter.py
@@ -296,11 +296,11 @@ if __name__ == '__main__':
f(' $y')
f('''$__file__:$__line__
{''')
- f("${{', '.join(str(x) for x in xrange(4))}}")
+ f("${{', '.join(str(x) for x in range(4))}}")
f('${x}')
f('$x')
f.indent()
- for i in xrange(5):
+ for i in range(5):
f('$x')
f('$i')
f('$0', "zero")
diff --git a/src/python/m5/util/dot_writer.py b/src/python/m5/util/dot_writer.py
index 7155f1163..f368faacb 100644
--- a/src/python/m5/util/dot_writer.py
+++ b/src/python/m5/util/dot_writer.py
@@ -67,7 +67,7 @@ except:
pydot = False
def simnode_children(simNode):
- for child in simNode._children.itervalues():
+ for child in simNode._children.values():
if isNullPointer(child):
continue
if isSimObjectVector(child):
diff --git a/src/python/m5/util/jobfile.py b/src/python/m5/util/jobfile.py
index 613289a81..45214a0b5 100644
--- a/src/python/m5/util/jobfile.py
+++ b/src/python/m5/util/jobfile.py
@@ -40,7 +40,7 @@ class Data(object):
if not isinstance(obj, Data):
raise AttributeError("can only update from Data object")
- for key,val in obj.__dict__.iteritems():
+ for key,val in obj.__dict__.items():
if key.startswith('_') or key in ('name', 'desc'):
continue
@@ -57,7 +57,7 @@ class Data(object):
(key, self.__dict__[key], val))
d = self.__dict__[key]
- for k,v in val.iteritems():
+ for k,v in val.items():
if k in d:
raise AttributeError(
"%s specified more than once in %s" % (k, key))
@@ -100,7 +100,7 @@ class Data(object):
return self.__dict__[key]
def __iter__(self):
- keys = self.__dict__.keys()
+ keys = list(self.__dict__.keys())
keys.sort()
for key in keys:
if not key.startswith('_'):
@@ -115,7 +115,7 @@ class Data(object):
def __repr__(self):
d = {}
- for key,value in self.__dict__.iteritems():
+ for key,value in self.__dict__.items():
if not key.startswith('_'):
d[key] = value
diff --git a/src/python/m5/util/multidict.py b/src/python/m5/util/multidict.py
index 5cc13eefa..23301565e 100644
--- a/src/python/m5/util/multidict.py
+++ b/src/python/m5/util/multidict.py
@@ -82,27 +82,18 @@ class multidict(object):
def has_key(self, key):
return key in self
- def iteritems(self):
+ def items(self):
for item in self.next():
yield item
- def items(self):
- return [ item for item in self.next() ]
-
- def iterkeys(self):
+ def keys(self):
for key,value in self.next():
yield key
- def keys(self):
- return [ key for key,value in self.next() ]
-
- def itervalues(self):
+ def values(self):
for key,value in self.next():
yield value
- def values(self):
- return [ value for key,value in self.next() ]
-
def get(self, key, default=None):
try:
return self[key]
@@ -152,8 +143,8 @@ if __name__ == '__main__':
test2.setdefault('f', multidict)
- print('test1>', test1.items())
- print('test2>', test2.items())
+ print('test1>', list(test1.items()))
+ print('test2>', list(test2.items()))
#print(test1['a'])
print(test1['b'])
print(test1['c'])
@@ -166,7 +157,7 @@ if __name__ == '__main__':
print(test2['d'])
print(test2['e'])
- for key in test2.iterkeys():
+ for key in test2.keys():
print(key)
test2.get('g', 'foo')
diff --git a/src/python/m5/util/smartdict.py b/src/python/m5/util/smartdict.py
index 76b7eb4f8..b8127b149 100644
--- a/src/python/m5/util/smartdict.py
+++ b/src/python/m5/util/smartdict.py
@@ -138,17 +138,11 @@ class SmartDict(attrdict):
dict.__setitem__(self, key, str(item))
def values(self):
- return [ Variable(v) for v in dict.values(self) ]
-
- def itervalues(self):
- for value in dict.itervalues(self):
+ for value in dict.values(self):
yield Variable(value)
def items(self):
- return [ (k, Variable(v)) for k,v in dict.items(self) ]
-
- def iteritems(self):
- for key,value in dict.iteritems(self):
+ for key,value in dict.items(self):
yield key, Variable(value)
def get(self, key, default='False'):
diff --git a/src/python/m5/util/sorteddict.py b/src/python/m5/util/sorteddict.py
index dd534b3cb..28c9c601d 100644
--- a/src/python/m5/util/sorteddict.py
+++ b/src/python/m5/util/sorteddict.py
@@ -41,7 +41,7 @@ class SortedDict(dict):
try:
return self._sorted_keys
except AttributeError:
- _sorted_keys = self.sorted(dict.iterkeys(self))
+ _sorted_keys = self.sorted(dict.keys(self))
self._sorted_keys = _sorted_keys
return _sorted_keys
@@ -89,7 +89,7 @@ class SortedDict(dict):
def __repr__(self):
return 'SortedDict({%s})' % ', '.join('%r: %r' % item
- for item in self.iteritems())
+ for item in self.items())
def __setitem__(self, key, item):
dict.__setitem__(self, key, item)
self._del_keys()
@@ -107,22 +107,13 @@ class SortedDict(dict):
return t(self)
def keys(self):
- return self._keys[:]
+ return self._keys
def values(self):
- return list(self.itervalues())
-
- def items(self):
- return list(self.iteritems())
-
- def iterkeys(self):
- return iter(self._keys)
-
- def itervalues(self):
for k in self._keys:
yield self[k]
- def iteritems(self):
+ def items(self):
for k in self._keys:
yield k, self[k]
@@ -184,12 +175,12 @@ class SortedDict(dict):
if __name__ == '__main__':
def display(d):
print(d)
- print(d.keys())
- print(list(d.iterkeys()))
- print(d.values())
- print(list(d.itervalues()))
- print(d.items())
- print(list(d.iteritems()))
+ print(list(d.keys()))
+ print(list(d.keys()))
+ print(list(d.values()))
+ print(list(d.values()))
+ print(list(d.items()))
+ print(list(d.items()))
d = SortedDict(x=24,e=5,j=4,b=2,z=26,d=4)
display(d)
diff --git a/src/python/m5/util/terminal.py b/src/python/m5/util/terminal.py
index 9dc5d9850..00f8b7061 100644
--- a/src/python/m5/util/terminal.py
+++ b/src/python/m5/util/terminal.py
@@ -63,7 +63,7 @@ capability_map = {
'Normal': 'sgr0'
}
-capability_names = capability_map.keys()
+capability_names = list(capability_map.keys())
def null_cap_string(s, *args):
return ''
@@ -84,7 +84,7 @@ class ColorStrings(object):
def __init__(self, cap_string):
for i, c in enumerate(color_names):
setattr(self, c, cap_string('setaf', i))
- for name, cap in capability_map.iteritems():
+ for name, cap in capability_map.items():
setattr(self, name, cap_string(cap))
termcap = ColorStrings(cap_string)