misc: Use python 3's argumentless super().

When calling a method in a superclass, you can/should use the super()
method to get a reference to that class. The python 2 version of that
method takes two parameters, the current class name, and the "self"
instance. The python 3 version takes no arguments. This is better for a
at least three reasons.

First, this version is less verbose because you don't have to specify
any arguments.

Second, you don't have to remember which argument goes where (I always
have to look it up), and you can't accidentally use the wrong class
name, or forget to update it if you copy code from a different class.

Third, this version will work correctly if you use a class decorator.
I don't know exactly how the mechanics of this work, but it is referred
to in a comment on this stackoverflow question:

https://stackoverflow.com/questions/681953/how-to-decorate-a-class

Change-Id: I427737c8f767e80da86cd245642e3b057121bc3b
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/52224
Reviewed-by: Gabe Black <gabe.black@gmail.com>
Maintainer: Gabe Black <gabe.black@gmail.com>
Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
Gabe Black
2021-10-28 16:16:07 -07:00
parent e4cff7b5ca
commit ba5f68db3d
142 changed files with 301 additions and 370 deletions

View File

@@ -76,7 +76,7 @@ class Singleton(type):
if hasattr(cls, '_instance'):
return cls._instance
cls._instance = super(Singleton, cls).__call__(*args, **kwargs)
cls._instance = super().__call__(*args, **kwargs)
return cls._instance
def addToPath(path):

View File

@@ -31,17 +31,17 @@ class attrdict(dict):
def __getattr__(self, attr):
if attr in self:
return self.__getitem__(attr)
return super(attrdict, self).__getattribute__(attr)
return super().__getattribute__(attr)
def __setattr__(self, attr, value):
if attr in dir(self) or attr.startswith('_'):
return super(attrdict, self).__setattr__(attr, value)
return super().__setattr__(attr, value)
return self.__setitem__(attr, value)
def __delattr__(self, attr):
if attr in self:
return self.__delitem__(attr)
return super(attrdict, self).__delattr__(attr)
return super().__delattr__(attr)
def __getstate__(self):
return dict(self)
@@ -54,7 +54,7 @@ class multiattrdict(attrdict):
nested dictionaries."""
def __getattr__(self, attr):
try:
return super(multiattrdict, self).__getattr__(attr)
return super().__getattr__(attr)
except AttributeError:
if attr.startswith('_'):
raise
@@ -67,7 +67,7 @@ class optiondict(attrdict):
"""Modify attrdict so that a missing attribute just returns None"""
def __getattr__(self, attr):
try:
return super(optiondict, self).__getattr__(attr)
return super().__getattr__(attr)
except AttributeError:
return None

View File

@@ -53,7 +53,7 @@ class FdtPropertyWords(pyfdt.FdtPropertyWords):
# Make sure all values are ints (use automatic base detection if the
# type is str)
words = [int(w, base=0) if type(w) == str else int(w) for w in words]
super(FdtPropertyWords, self).__init__(name, words)
super().__init__(name, words)
class FdtPropertyStrings(pyfdt.FdtPropertyStrings):
"""Create a property with string values."""
@@ -62,7 +62,7 @@ class FdtPropertyStrings(pyfdt.FdtPropertyStrings):
if type(strings) == str:
strings = [strings]
strings = [str(string) for string in strings] # Make all values strings
super(FdtPropertyStrings, self).__init__(name, strings)
super().__init__(name, strings)
class FdtPropertyBytes(pyfdt.FdtPropertyBytes):
"""Create a property with integer (8-bit signed) values."""
@@ -74,7 +74,7 @@ class FdtPropertyBytes(pyfdt.FdtPropertyBytes):
# type is str)
values = [int(v, base=0)
if isinstance(v, str) else int(v) for v in values]
super(FdtPropertyBytes, self).__init__(name, values)
super().__init__(name, values)
class FdtState(object):
"""Class for maintaining state while recursively generating a flattened
@@ -172,7 +172,7 @@ class FdtNode(pyfdt.FdtNode):
def __init__(self, name, obj=None):
"""Create a new node and immediately set the phandle property, if obj
is supplied"""
super(FdtNode, self).__init__(name)
super().__init__(name)
if obj != None:
self.appendPhandle(obj)
@@ -198,7 +198,7 @@ class FdtNode(pyfdt.FdtNode):
item.merge(subnode)
subnode = item
super(FdtNode, self).append(subnode)
super().append(subnode)
def appendList(self, subnode_list):
"""Append all properties/nodes in the iterable."""
@@ -245,7 +245,7 @@ class Fdt(pyfdt.Fdt):
def add_rootnode(self, rootnode, prenops=None, postnops=None):
"""First sort the device tree, so that properties are before nodes."""
rootnode = self.sortNodes(rootnode)
super(Fdt, self).add_rootnode(rootnode, prenops, postnops)
super().add_rootnode(rootnode, prenops, postnops)
def writeDtbFile(self, filename):
"""Convert the device tree to DTB and write to a file."""