Andreas Sandberg has uploaded this change for review. ( https://gem5-review.googlesource.com/c/public/gem5/+/15982

Change subject: RFC: python: Make metaclass declarations Python 3 safe
......................................................................

RFC: python: Make metaclass declarations Python 3 safe

NB: This change turns a couple of assertions into warnings since the
six compatibility code for metaclasses exhibits weird behavior.

Change-Id: I5dea08bf0558cfca57897a124cb131c78114e59e
---
M src/python/m5/SimObject.py
M src/python/m5/params.py
M src/python/m5/util/code_formatter.py
M src/python/m5/util/pybind.py
4 files changed, 21 insertions(+), 16 deletions(-)



diff --git a/src/python/m5/SimObject.py b/src/python/m5/SimObject.py
index 97cf6d0..03d5d3a 100644
--- a/src/python/m5/SimObject.py
+++ b/src/python/m5/SimObject.py
@@ -45,6 +45,8 @@

 from __future__ import print_function

+from six import add_metaclass
+
 import sys
 from types import FunctionType, MethodType, ModuleType
 from functools import wraps
@@ -981,23 +983,23 @@
                 out.extend(self[t]._sim_objects)
         else:
             if isinstance(idx, int):
-                _range = range(idx, idx + 1)
+                _range = list(range(idx, idx + 1))
             elif not isinstance(idx, slice):
                 raise SimObjectCliWrapperException( \
                         'invalid index type: ' + repr(idx))
             for sim_object in self._sim_objects:
                 if isinstance(idx, slice):
-                    _range = range(*idx.indices(len(sim_object)))
+                    _range = list(range(*idx.indices(len(sim_object))))
                 out.extend(sim_object[i] for i in _range)
         return SimObjectCliWrapper(out)

 # The SimObject class is the root of the special hierarchy.  Most of
 # the code in this class deals with the configuration hierarchy itself
 # (parent/child node relationships).
+@add_metaclass(MetaSimObject)
 class SimObject(object):
     # Specify metaclass.  Any class inheriting from SimObject will
     # get this metaclass.
-    __metaclass__ = MetaSimObject
     type = 'SimObject'
     abstract = True

diff --git a/src/python/m5/params.py b/src/python/m5/params.py
index 0a563b8..f8ba076 100644
--- a/src/python/m5/params.py
+++ b/src/python/m5/params.py
@@ -61,6 +61,8 @@

 from __future__ import print_function

+from six import add_metaclass
+
 import copy
 import datetime
 import re
@@ -86,15 +88,17 @@
 class MetaParamValue(type):
     def __new__(mcls, name, bases, dct):
         cls = super(MetaParamValue, mcls).__new__(mcls, name, bases, dct)
-        assert name not in allParams
+        if name in allParams:
+ warn("%s already exists in allParams. This may be caused by the " \
+                 "Python 2.7 compatibility layer." % (name, ))
         allParams[name] = cls
         return cls


 # Dummy base class to identify types that are legitimate for SimObject
 # parameters.
+@add_metaclass(MetaParamValue)
 class ParamValue(object):
-    __metaclass__ = MetaParamValue
     cmd_line_settable = False

     # Generate the code needed as a prerequisite for declaring a C++
@@ -231,8 +235,8 @@
 # that the value is a vector (list) of the specified type instead of a
 # single value.

+@add_metaclass(MetaParamValue)
 class VectorParamValue(list):
-    __metaclass__ = MetaParamValue
     def __setattr__(self, attr, value):
         raise AttributeError("Not allowed to set %s on '%s'" % \
                              (attr, type(self).__name__))
@@ -533,8 +537,8 @@
 # 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):
-    __metaclass__ = CheckedIntType
     cmd_line_settable = True

     def _check(self):
@@ -1367,8 +1371,8 @@


 # Base class for enum types.
+@add_metaclass(MetaEnum)
 class Enum(ParamValue):
-    __metaclass__ = MetaEnum
     vals = []
     cmd_line_settable = True

@@ -1707,8 +1711,8 @@
 # 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):
-    __metaclass__ = Singleton
     _name = 'Null'

     def __call__(cls):
@@ -2071,9 +2075,8 @@
 # '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):
-    __metaclass__ = Singleton
-
     ptype_str = 'Port'
     ptype = Port

diff --git a/src/python/m5/util/code_formatter.py b/src/python/m5/util/code_formatter.py
index d48c59b..f47b964 100644
--- a/src/python/m5/util/code_formatter.py
+++ b/src/python/m5/util/code_formatter.py
@@ -25,6 +25,7 @@
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 from __future__ import print_function
+from six import add_metaclass

 import __builtin__
 import inspect
@@ -108,9 +109,8 @@
                 }
cls.pattern = re.compile(pat, re.VERBOSE | re.DOTALL | re.MULTILINE)

+@add_metaclass(code_formatter_meta)
 class code_formatter(object):
-    __metaclass__ = code_formatter_meta
-
     delim = r'$'
     ident = r'[_A-z]\w*'
     pos = r'[0-9]+'
diff --git a/src/python/m5/util/pybind.py b/src/python/m5/util/pybind.py
index f666547..19397fa 100644
--- a/src/python/m5/util/pybind.py
+++ b/src/python/m5/util/pybind.py
@@ -35,11 +35,11 @@
 #
 # Authors: Andreas Sandberg

+from six import with_metaclass
+
 from abc import *

-class PyBindExport(object):
-    __metaclass__ = ABCMeta
-
+class PyBindExport(with_metaclass(ABCMeta, object)):
     @abstractmethod
     def export(self, code, cname):
         pass

--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/15982
To unsubscribe, or for help writing mail filters, visit https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: master
Gerrit-Change-Id: I5dea08bf0558cfca57897a124cb131c78114e59e
Gerrit-Change-Number: 15982
Gerrit-PatchSet: 1
Gerrit-Owner: Andreas Sandberg <[email protected]>
Gerrit-MessageType: newchange
_______________________________________________
gem5-dev mailing list
[email protected]
http://m5sim.org/mailman/listinfo/gem5-dev

Reply via email to