Gabe Black has uploaded this change for review. (
https://gem5-review.googlesource.com/c/public/gem5/+/16504
Change subject: config: Make parameter conversion handle integers in other
bases.
......................................................................
config: Make parameter conversion handle integers in other bases.
Python's float() function/type can't handle hexadecimal notation, but
int() can. When converting the string representation of a number into
a numeric type, we can first attempt to use float() on it, and if that
fails try to use int() with an autodetected base.
Change-Id: Ic46cf4ae86b7eba6f55d731d1b25e3f84b8bb64c
---
M src/python/m5/util/convert.py
1 file changed, 13 insertions(+), 7 deletions(-)
diff --git a/src/python/m5/util/convert.py b/src/python/m5/util/convert.py
index 7b9cb38..87757ad 100644
--- a/src/python/m5/util/convert.py
+++ b/src/python/m5/util/convert.py
@@ -93,26 +93,32 @@
# memory size configuration stuff
+def toNum(value, target_type):
+ try:
+ return float(value)
+ except ValueError:
+ pass
+ try:
+ return int(value, 0)
+ except ValueError:
+ raise ValueError("cannot convert '%s' to %s" % (value,
target_type))
+
def toFloat(value, target_type='float', units=None, prefixes=[]):
assertStr(value)
if units and not value.endswith(units):
units = None
if not units:
- try:
- return float(value)
- except ValueError:
- raise ValueError("cannot convert '%s' to %s" % \
- (value, target_type))
+ return toNum(value, target_type)
value = value[:-len(units)]
prefix = next((p for p in prefixes.keys() if value.endswith(p)), None)
if not prefix:
- return float(value)
+ return toNum(value, target_type)
value = value[:-len(prefix)]
- return float(value) * prefixes[prefix]
+ return toNum(value, target_type) * prefixes[prefix]
def toMetricFloat(value, target_type='float', units=None):
return toFloat(value, target_type, units, metric_prefixes)
--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/16504
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: Ic46cf4ae86b7eba6f55d731d1b25e3f84b8bb64c
Gerrit-Change-Number: 16504
Gerrit-PatchSet: 1
Gerrit-Owner: Gabe Black <[email protected]>
Gerrit-MessageType: newchange
_______________________________________________
gem5-dev mailing list
[email protected]
http://m5sim.org/mailman/listinfo/gem5-dev