python: Eliminate the Python use of eval() and frame manipulation
--HG-- extra : convert_revision : 04520bcfab510580a1c7fb341afbd2487287d1ab
This commit is contained in:
parent
f67cd04673
commit
9cb49ab9e0
2 changed files with 23 additions and 22 deletions
|
@ -757,7 +757,7 @@ class SimObject(object):
|
||||||
if self._ccParams:
|
if self._ccParams:
|
||||||
return self._ccParams
|
return self._ccParams
|
||||||
|
|
||||||
cc_params_struct = eval('m5.objects.params.%sParams' % self.type)
|
cc_params_struct = getattr(m5.objects.params, '%sParams' % self.type)
|
||||||
cc_params = cc_params_struct()
|
cc_params = cc_params_struct()
|
||||||
cc_params.object = self
|
cc_params.object = self
|
||||||
cc_params.name = str(self)
|
cc_params.name = str(self)
|
||||||
|
|
|
@ -46,7 +46,6 @@
|
||||||
|
|
||||||
import copy
|
import copy
|
||||||
import datetime
|
import datetime
|
||||||
import inspect
|
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
@ -67,9 +66,20 @@ def isSimObjectSequence(*args, **kwargs):
|
||||||
def isSimObjectClass(*args, **kwargs):
|
def isSimObjectClass(*args, **kwargs):
|
||||||
return SimObject.isSimObjectClass(*args, **kwargs)
|
return SimObject.isSimObjectClass(*args, **kwargs)
|
||||||
|
|
||||||
|
allParams = {}
|
||||||
|
|
||||||
|
class MetaParamValue(type):
|
||||||
|
def __new__(mcls, name, bases, dct):
|
||||||
|
cls = super(MetaParamValue, mcls).__new__(mcls, name, bases, dct)
|
||||||
|
assert name not in allParams
|
||||||
|
allParams[name] = cls
|
||||||
|
return cls
|
||||||
|
|
||||||
|
|
||||||
# Dummy base class to identify types that are legitimate for SimObject
|
# Dummy base class to identify types that are legitimate for SimObject
|
||||||
# parameters.
|
# parameters.
|
||||||
class ParamValue(object):
|
class ParamValue(object):
|
||||||
|
__metaclass__ = MetaParamValue
|
||||||
|
|
||||||
cxx_predecls = []
|
cxx_predecls = []
|
||||||
swig_predecls = []
|
swig_predecls = []
|
||||||
|
@ -119,16 +129,11 @@ class ParamDesc(object):
|
||||||
|
|
||||||
def __getattr__(self, attr):
|
def __getattr__(self, attr):
|
||||||
if attr == 'ptype':
|
if attr == 'ptype':
|
||||||
try:
|
|
||||||
ptype = SimObject.allClasses[self.ptype_str]
|
ptype = SimObject.allClasses[self.ptype_str]
|
||||||
if not isinstance(ptype, type):
|
assert issubclass(ptype, SimObject.SimObject)
|
||||||
raise NameError
|
|
||||||
self.ptype = ptype
|
self.ptype = ptype
|
||||||
return ptype
|
return ptype
|
||||||
except NameError:
|
|
||||||
raise
|
|
||||||
#raise TypeError, \
|
|
||||||
# "Param qualifier '%s' is not a type" % self.ptype_str
|
|
||||||
raise AttributeError, "'%s' object has no attribute '%s'" % \
|
raise AttributeError, "'%s' object has no attribute '%s'" % \
|
||||||
(type(self).__name__, attr)
|
(type(self).__name__, attr)
|
||||||
|
|
||||||
|
@ -160,6 +165,7 @@ class ParamDesc(object):
|
||||||
# single value.
|
# single value.
|
||||||
|
|
||||||
class VectorParamValue(list):
|
class VectorParamValue(list):
|
||||||
|
__metaclass__ = MetaParamValue
|
||||||
def ini_str(self):
|
def ini_str(self):
|
||||||
return ' '.join([v.ini_str() for v in self])
|
return ' '.join([v.ini_str() for v in self])
|
||||||
|
|
||||||
|
@ -217,15 +223,10 @@ class ParamFactory(object):
|
||||||
|
|
||||||
# E.g., Param.Int(5, "number of widgets")
|
# E.g., Param.Int(5, "number of widgets")
|
||||||
def __call__(self, *args, **kwargs):
|
def __call__(self, *args, **kwargs):
|
||||||
caller_frame = inspect.currentframe().f_back
|
|
||||||
ptype = None
|
ptype = None
|
||||||
try:
|
try:
|
||||||
ptype = eval(self.ptype_str,
|
ptype = allParams[self.ptype_str]
|
||||||
caller_frame.f_globals, caller_frame.f_locals)
|
except KeyError:
|
||||||
if not isinstance(ptype, type):
|
|
||||||
raise TypeError, \
|
|
||||||
"Param qualifier is not a type: %s" % ptype
|
|
||||||
except NameError:
|
|
||||||
# if name isn't defined yet, assume it's a SimObject, and
|
# if name isn't defined yet, assume it's a SimObject, and
|
||||||
# try to resolve it later
|
# try to resolve it later
|
||||||
pass
|
pass
|
||||||
|
@ -300,7 +301,7 @@ class NumericParamValue(ParamValue):
|
||||||
return newobj
|
return newobj
|
||||||
|
|
||||||
# Metaclass for bounds-checked integer parameters. See CheckedInt.
|
# Metaclass for bounds-checked integer parameters. See CheckedInt.
|
||||||
class CheckedIntType(type):
|
class CheckedIntType(MetaParamValue):
|
||||||
def __init__(cls, name, bases, dict):
|
def __init__(cls, name, bases, dict):
|
||||||
super(CheckedIntType, cls).__init__(name, bases, dict)
|
super(CheckedIntType, cls).__init__(name, bases, dict)
|
||||||
|
|
||||||
|
@ -424,7 +425,7 @@ class Addr(CheckedInt):
|
||||||
return self.value + other
|
return self.value + other
|
||||||
|
|
||||||
|
|
||||||
class MetaRange(type):
|
class MetaRange(MetaParamValue):
|
||||||
def __init__(cls, name, bases, dict):
|
def __init__(cls, name, bases, dict):
|
||||||
super(MetaRange, cls).__init__(name, bases, dict)
|
super(MetaRange, cls).__init__(name, bases, dict)
|
||||||
if name == 'Range':
|
if name == 'Range':
|
||||||
|
@ -665,7 +666,7 @@ class Time(ParamValue):
|
||||||
|
|
||||||
allEnums = {}
|
allEnums = {}
|
||||||
# Metaclass for Enum types
|
# Metaclass for Enum types
|
||||||
class MetaEnum(type):
|
class MetaEnum(MetaParamValue):
|
||||||
def __new__(mcls, name, bases, dict):
|
def __new__(mcls, name, bases, dict):
|
||||||
assert name not in allEnums
|
assert name not in allEnums
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue