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>
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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')
|
||||
|
||||
@@ -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'):
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user