diff options
author | Dan Albert <danalbert@google.com> | 2015-01-09 01:56:37 +0000 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2015-01-09 01:56:37 +0000 |
commit | 9cf8d9ca5aef24403867b7154f369cc831739178 (patch) | |
tree | 1546fd470713070fd2593e521ae029ab481534ed | |
parent | d5a8f8a93ed662d830cd4cc57ef14d05838f6897 (diff) | |
parent | f41cd32af0994358ae9f3acca23c0354de5b3f0d (diff) | |
download | external_llvm-9cf8d9ca5aef24403867b7154f369cc831739178.zip external_llvm-9cf8d9ca5aef24403867b7154f369cc831739178.tar.gz external_llvm-9cf8d9ca5aef24403867b7154f369cc831739178.tar.bz2 |
am f41cd32a: Merge "[LIT] Add JSONMetricValue type to wrap types supported by the json encoder."
* commit 'f41cd32af0994358ae9f3acca23c0354de5b3f0d':
[LIT] Add JSONMetricValue type to wrap types supported by the json encoder.
-rw-r--r-- | utils/lit/lit/Test.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/utils/lit/lit/Test.py b/utils/lit/lit/Test.py index 63d31b9..b810230 100644 --- a/utils/lit/lit/Test.py +++ b/utils/lit/lit/Test.py @@ -1,5 +1,6 @@ import os from xml.sax.saxutils import escape +from json import JSONEncoder # Test result codes. @@ -73,6 +74,41 @@ class RealMetricValue(MetricValue): def todata(self): return self.value +class JSONMetricValue(MetricValue): + """ + JSONMetricValue is used for types that are representable in the output + but that are otherwise uninterpreted. + """ + def __init__(self, value): + # Ensure the value is a serializable by trying to encode it. + # WARNING: The value may change before it is encoded again, and may + # not be encodable after the change. + try: + e = JSONEncoder() + e.encode(value) + except TypeError: + raise + self.value = value + + def format(self): + return str(self.value) + + def todata(self): + return self.value + +def toMetricValue(value): + if isinstance(value, MetricValue): + return value + elif isinstance(value, int) or isinstance(value, long): + return IntMetricValue(value) + elif isinstance(value, float): + return RealMetricValue(value) + else: + # Try to create a JSONMetricValue and let the constructor throw + # if value is not a valid type. + return JSONMetricValue(value) + + # Test results. class Result(object): |