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

Change subject: python: Require a unit in anyToFreuency and anyToLatency
......................................................................

python: Require a unit in anyToFreuency and anyToLatency

The anytToFrequency and anyToLatency conversion functions are
currently ambiguous when called without a unit. Fix this by always
requiring a unit.

Change-Id: I5ea94e655f7ca82c0efe70b9f9f7f734fbf711c1
Signed-off-by: Andreas Sandberg <andreas.sandb...@arm.com>
---
M src/python/m5/util/convert.py
M tests/pyunit/util/test_convert.py
2 files changed, 32 insertions(+), 34 deletions(-)



diff --git a/src/python/m5/util/convert.py b/src/python/m5/util/convert.py
index ce06ea4..12d3aa1 100644
--- a/src/python/m5/util/convert.py
+++ b/src/python/m5/util/convert.py
@@ -203,32 +203,40 @@
     return toMetricFloat(value, 'latency', 's')

 def anyToLatency(value):
-    """result is a clock period"""
-    try:
-        return 1 / toFrequency(value)
-    except (ValueError, ZeroDivisionError):
-        pass
+    """Convert a magnitude and unit to a clock period."""

-    try:
-        return toLatency(value)
-    except ValueError:
-        pass
-
-    raise ValueError("cannot convert '%s' to clock period" % value)
+    magnitude, unit = toNum(value,
+                            target_type='latency',
+                            units=('Hz', 's'),
+                            prefixes=metric_prefixes,
+                            converter=float)
+    if unit == 's':
+        return magnitude
+    elif unit == 'Hz':
+        try:
+            return 1.0 / magnitude
+        except ZeroDivisionError:
+            raise ValueError(f"cannot convert '{value}' to clock period")
+    else:
+ raise ValueError(f"'{value}' needs a valid unit to be unambiguous.")

 def anyToFrequency(value):
-    """result is a clock period"""
-    try:
-        return toFrequency(value)
-    except ValueError:
-        pass
+    """Convert a magnitude and unit to a clock frequency."""

-    try:
-        return 1 / toLatency(value)
-    except ValueError as ZeroDivisionError:
-        pass
-
-    raise ValueError("cannot convert '%s' to clock period" % value)
+    magnitude, unit = toNum(value,
+                            target_type='frequency',
+                            units=('Hz', 's'),
+                            prefixes=metric_prefixes,
+                            converter=float)
+    if unit == 'Hz':
+        return magnitude
+    elif unit == 's':
+        try:
+            return 1.0 / magnitude
+        except ZeroDivisionError:
+            raise ValueError(f"cannot convert '{value}' to frequency")
+    else:
+ raise ValueError(f"'{value}' needs a valid unit to be unambiguous.")

 def toNetworkBandwidth(value):
     return toMetricFloat(value, 'network bandwidth', 'bps')
diff --git a/tests/pyunit/util/test_convert.py b/tests/pyunit/util/test_convert.py
index 6d02b51..fcfedc4 100644
--- a/tests/pyunit/util/test_convert.py
+++ b/tests/pyunit/util/test_convert.py
@@ -163,28 +163,18 @@
         self.assertEqual(conv('1kHz'), 1e-3)

         self.assertRaises(ValueError, conv, '42k')
-
-    @unittest.expectedFailure
-    def test_anyToLatency_ambiguous(self):
-        # This the behavior of anyToFrequency is currently ambiguous
-        # (and surprising) for unitless quantities. The following
-        # should be true to be consistent with the other conversion
-        # functions, but that isn't currently the case.
-        self.assertEqual(convert.anyToLatency('42'), 42.0)
-
+        self.assertRaises(ValueError, conv, '42')

     def test_anyToFrequency(self):
         conv = convert.anyToFrequency

-        # This is ambiguous and should probably not be allowed.
-        self.assertEqual(conv('42'), 42.0)
-
         self.assertEqual(conv('42kHz'), 42e3)

         self.assertEqual(conv('0.1s'), 10.0)
         self.assertEqual(conv('1ms'), 1000.0)

         self.assertRaises(ValueError, conv, '42k')
+        self.assertRaises(ValueError, conv, '42')

     def test_toNetworkBandwidth(self):
         conv = convert.toNetworkBandwidth

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

Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: I5ea94e655f7ca82c0efe70b9f9f7f734fbf711c1
Gerrit-Change-Number: 39435
Gerrit-PatchSet: 1
Gerrit-Owner: Andreas Sandberg <andreas.sandb...@arm.com>
Gerrit-MessageType: newchange
_______________________________________________
gem5-dev mailing list -- gem5-dev@gem5.org
To unsubscribe send an email to gem5-dev-le...@gem5.org
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s

Reply via email to