python: Use six's with_metaclass instead of it's add_metaclass.

The decorator creates two versions of a class, adding it to the Params
dict multiple times which generates an annoying warning. Alternatively,
the with_metaclass mechanism sets up an alternative base class which
does not create the extra class and doesn't generate the warning.

It may be the case that this generates extra classes which just don't
lead to a warning? Or in other words, would we then have Params types
with weird, internal names generated by six? Hopefully not, but that may
be preferable to the annoying warnings, especially when running tests
which run gem5 many times.

Change-Id: I9395cde3fc95126c0a0c4db67fc5b0c6bf2dd9ed
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/33276
Reviewed-by: Andreas Sandberg <andreas.sandberg@arm.com>
Maintainer: Andreas Sandberg <andreas.sandberg@arm.com>
Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
Gabe Black
2020-08-23 20:15:40 -07:00
parent cf88ba0c09
commit 5d420afaa0

View File

@@ -55,7 +55,7 @@
#####################################################################
from __future__ import print_function
from six import add_metaclass
from six import with_metaclass
import six
if six.PY3:
long = int
@@ -97,8 +97,7 @@ class MetaParamValue(type):
# Dummy base class to identify types that are legitimate for SimObject
# parameters.
@add_metaclass(MetaParamValue)
class ParamValue(object):
class ParamValue(with_metaclass(MetaParamValue, object)):
cmd_line_settable = False
# Generate the code needed as a prerequisite for declaring a C++
@@ -236,8 +235,7 @@ class ParamDesc(object):
# that the value is a vector (list) of the specified type instead of a
# single value.
@add_metaclass(MetaParamValue)
class VectorParamValue(list):
class VectorParamValue(with_metaclass(MetaParamValue, list)):
def __setattr__(self, attr, value):
raise AttributeError("Not allowed to set %s on '%s'" % \
(attr, type(self).__name__))
@@ -588,8 +586,7 @@ class CheckedIntType(MetaParamValue):
# class is subclassed to generate parameter classes with specific
# bounds. Initialization of the min and max bounds is done in the
# metaclass CheckedIntType.__init__.
@add_metaclass(CheckedIntType)
class CheckedInt(NumericParamValue):
class CheckedInt(with_metaclass(CheckedIntType, NumericParamValue)):
cmd_line_settable = True
def _check(self):
@@ -1447,8 +1444,7 @@ module_init(py::module &m_internal)
# Base class for enum types.
@add_metaclass(MetaEnum)
class Enum(ParamValue):
class Enum(with_metaclass(MetaEnum, ParamValue)):
vals = []
cmd_line_settable = True
@@ -1501,7 +1497,6 @@ class Enum(ParamValue):
return self.value
# This param will generate a scoped c++ enum and its python bindings.
@add_metaclass(MetaEnum)
class ScopedEnum(Enum):
vals = []
cmd_line_settable = True
@@ -1789,8 +1784,7 @@ class MemoryBandwidth(float,ParamValue):
# make_param_value() above that lets these be assigned where a
# SimObject is required.
# only one copy of a particular node
@add_metaclass(Singleton)
class NullSimObject(object):
class NullSimObject(with_metaclass(Singleton, object)):
_name = 'Null'
def __call__(cls):
@@ -2157,8 +2151,7 @@ VectorSlavePort = VectorResponsePort
# 'Fake' ParamDesc for Port references to assign to the _pdesc slot of
# proxy objects (via set_param_desc()) so that proxy error messages
# make sense.
@add_metaclass(Singleton)
class PortParamDesc(object):
class PortParamDesc(with_metaclass(Singleton, object)):
ptype_str = 'Port'
ptype = Port