summaryrefslogtreecommitdiff
path: root/src/python/m5
diff options
context:
space:
mode:
authorAndreas Sandberg <andreas.sandberg@arm.com>2019-01-26 10:51:29 +0000
committerAndreas Sandberg <andreas.sandberg@arm.com>2019-02-20 18:27:10 +0000
commit4fa56fe2f8618f5fbc47ec21ac033d5e7ac2790d (patch)
tree193b59ca73e7c7a512ec7517320cd8cda57e533d /src/python/m5
parentd07cceb13e13cfe806c37527e602cde0789ea8c3 (diff)
downloadgem5-4fa56fe2f8618f5fbc47ec21ac033d5e7ac2790d.tar.xz
python: Add __bool__ helpers in addition to __nonzero__
Python 3 uses __bool__ instead of __nonzero__ when performing a Boolean comparison. Change-Id: I85185bbe136ecae67346fa23569e24edd7329222 Signed-off-by: Andreas Sandberg <andreas.sandberg@arm.com> Reviewed-on: https://gem5-review.googlesource.com/c/15996 Reviewed-by: Nikos Nikoleris <nikos.nikoleris@arm.com>
Diffstat (limited to 'src/python/m5')
-rw-r--r--src/python/m5/params.py5
-rw-r--r--src/python/m5/util/smartdict.py9
2 files changed, 11 insertions, 3 deletions
diff --git a/src/python/m5/params.py b/src/python/m5/params.py
index 09aaa5af7..b5de6ef6d 100644
--- a/src/python/m5/params.py
+++ b/src/python/m5/params.py
@@ -829,9 +829,12 @@ class Bool(ParamValue):
# implement truth value testing for Bool parameters so that these params
# evaluate correctly during the python configuration phase
- def __nonzero__(self):
+ def __bool__(self):
return bool(self.value)
+ # Python 2.7 uses __nonzero__ instead of __bool__
+ __nonzero__ = __bool__
+
def ini_str(self):
if self.value:
return 'true'
diff --git a/src/python/m5/util/smartdict.py b/src/python/m5/util/smartdict.py
index 61e48ddba..76b7eb4f8 100644
--- a/src/python/m5/util/smartdict.py
+++ b/src/python/m5/util/smartdict.py
@@ -55,8 +55,10 @@ class Variable(str):
return toLong(str(self))
def __float__(self):
return toFloat(str(self))
- def __nonzero__(self):
+ def __bool__(self):
return toBool(str(self))
+ # Python 2.7 uses __nonzero__ instead of __bool__
+ __nonzero__ = __bool__
def convert(self, other):
t = type(other)
if t == bool:
@@ -107,9 +109,12 @@ class UndefinedVariable(object):
"""Placeholder class to represent undefined variables. Will
generally cause an exception whenever it is used, but evaluates to
zero for boolean truth testing such as in an if statement"""
- def __nonzero__(self):
+ def __bool__(self):
return False
+ # Python 2.7 uses __nonzero__ instead of __bool__
+ __nonzero__ = __bool__
+
class SmartDict(attrdict):
"""Dictionary class that holds strings, but intelligently converts
those strings to other types depending on their usage"""