From c393a51f4a00e88e7a72ad2c7bd56d19194f0b6a Mon Sep 17 00:00:00 2001 From: Nathan Binkert Date: Fri, 11 Mar 2005 18:47:11 -0500 Subject: move the conversion stuff that was in configs/kernel/Config.py into the m5 package as convert.py add a smartdict class which stores strings and can intelligently interpret those string variables as several other types. make the env dict use the smartdict class python/m5/config.py: move a bunch of conversion functions into convert.py turn the env dict into a smartdict adapt the _CheckedInt stuff to deal with derived types python/m5/objects/BaseCPU.mpy: env is now a smartdict and can properly convert to bool --HG-- extra : convert_revision : 8abcd35a5ab14b82f280aea59020953869e33365 --- python/m5/smartdict.py | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 python/m5/smartdict.py (limited to 'python/m5/smartdict.py') diff --git a/python/m5/smartdict.py b/python/m5/smartdict.py new file mode 100644 index 000000000..e282bc07b --- /dev/null +++ b/python/m5/smartdict.py @@ -0,0 +1,85 @@ +from convert import * + +class SmartDict(dict): + class Proxy(str): + def __int__(self): + return int(to_integer(str(self))) + def __long__(self): + return long(to_integer(str(self))) + def __float__(self): + return float(to_integer(str(self))) + def __nonzero__(self): + return to_bool(str(self)) + def convert(self, other): + t = type(other) + if t == bool: + return bool(self) + if t == int: + return int(self) + if t == long: + return long(self) + if t == float: + return float(self) + return str(self) + def __lt__(self, other): + return self.convert(other) < other + def __le__(self, other): + return self.convert(other) <= other + def __eq__(self, other): + return self.convert(other) == other + def __ne__(self, other): + return self.convert(other) != other + def __gt__(self, other): + return self.convert(other) > other + def __ge__(self, other): + return self.convert(other) >= other + + def __add__(self, other): + return self.convert(other) + other + def __sub__(self, other): + return self.convert(other) - other + def __mul__(self, other): + return self.convert(other) * other + def __div__(self, other): + return self.convert(other) / other + def __truediv__(self, other): + return self.convert(other) / other + + def __radd__(self, other): + return other + self.convert(other) + def __rsub__(self, other): + return other - self.convert(other) + def __rmul__(self, other): + return other * self.convert(other) + def __rdiv__(self, other): + return other / self.convert(other) + def __rtruediv__(self, other): + return other / self.convert(other) + + + def __getitem__(self, key): + return self.Proxy(dict.__getitem__(self, key)) + + def __setitem__(self, key, item): + dict.__setitem__(self, key, str(item)) + + def values(self): + return [ self.Proxy(v) for v in dict.values(self) ] + + def itervalues(self): + for value in dict.itervalues(self): + yield self.Proxy(value) + + def items(self): + return [ (k, self.Proxy(v)) for k,v in dict.items(self) ] + + def iteritems(self): + for key,value in dict.iteritems(self): + yield key, self.Proxy(value) + + def get(self, key, default=''): + return self.Proxy(dict.get(self, key, str(default))) + + def setdefault(self, key, default=''): + return self.Proxy(dict.setdefault(self, key, str(default))) + -- cgit v1.2.3 From 1b841a871ecd717dd8705d12ff7311c6fc97fc63 Mon Sep 17 00:00:00 2001 From: Steve Reinhardt Date: Mon, 14 Mar 2005 07:46:26 -0500 Subject: - Add capability to auto-generate Param structs from .mpy SimObject descriptions. Structs are defined in simobj/param/ObjectName.hh. - Move compile-time python params (from CPPDEFINES) to separate dict from run-time params (from os.environ). The former are needed to generate proper param structs. This also helps prevent users from messing things up by setting the wrong environment vars (which could have overridden compile-time settings in the old system). - Other misc cleanup of m5 python package. SConscript: Include simobj/SConscript build/SConstruct: Fix type in comment python/SConscript: Move CPPDEFINES dict-generating code to m5scons.flatten_defines python/m5/__init__.py: - Generate a build_env SmartDict here to hold compile-time params (passed in via __main__.m5_build_env). - Move panic and env here from config.py. python/m5/config.py: Move panic, env to top level (m5/__init__.py) python/m5/objects/BaseCPU.mpy: Use build_env instead of env for compile-time params python/m5/smartdict.py: Add some comments. sim/sim_object.hh: Include auto-generated Param struct. Not used yet, just here as proof of concept. test/genini.py: Put -E arguments in build_env as well as os.environ --HG-- extra : convert_revision : cf6f4a2565b230c495b33b18612d6030988adac5 --- python/m5/smartdict.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'python/m5/smartdict.py') diff --git a/python/m5/smartdict.py b/python/m5/smartdict.py index e282bc07b..4ea8210d3 100644 --- a/python/m5/smartdict.py +++ b/python/m5/smartdict.py @@ -1,6 +1,23 @@ +# The SmartDict class fixes a couple of issues with using the content +# of os.environ or similar dicts of strings as Python variables: +# +# 1) Undefined variables should return False rather than raising KeyError. +# +# 2) String values of 'False', '0', etc., should evaluate to False +# (not just the empty string). +# +# #1 is solved by overriding __getitem__, and #2 is solved by using a +# proxy class for values and overriding __nonzero__ on the proxy. +# Everything else is just to (a) make proxies behave like normal +# values otherwise, (b) make sure any dict operation returns a proxy +# rather than a normal value, and (c) coerce values written to the +# dict to be strings. + + from convert import * class SmartDict(dict): + class Proxy(str): def __int__(self): return int(to_integer(str(self))) @@ -58,7 +75,7 @@ class SmartDict(dict): def __getitem__(self, key): - return self.Proxy(dict.__getitem__(self, key)) + return self.Proxy(dict.get(self, key, 'False')) def __setitem__(self, key, item): dict.__setitem__(self, key, str(item)) @@ -77,9 +94,9 @@ class SmartDict(dict): for key,value in dict.iteritems(self): yield key, self.Proxy(value) - def get(self, key, default=''): + def get(self, key, default='False'): return self.Proxy(dict.get(self, key, str(default))) - def setdefault(self, key, default=''): + def setdefault(self, key, default='False'): return self.Proxy(dict.setdefault(self, key, str(default))) -- cgit v1.2.3