[Piglit] [Patch v7 07/13] results.py: Replace JSONEncoder subclass with default method

Dylan Baker baker.dylan.c at gmail.com
Mon Jun 23 16:38:27 PDT 2014


Both methods are equally viable solution when using the builtin json
module, but using defaults is superior when using simplejson (a C based
implementation of the json module providing an identical interface but
vastly better performance) which can be used with piglit if it's
installed.

v7: - fix one framework test

Signed-off-by: Dylan Baker <baker.dylan.c at gmail.com>
---
 framework/results.py           | 21 +++++++++++++--------
 framework/tests/dmesg_tests.py |  7 ++++---
 2 files changed, 17 insertions(+), 11 deletions(-)

diff --git a/framework/results.py b/framework/results.py
index c9831f7..7762d39 100644
--- a/framework/results.py
+++ b/framework/results.py
@@ -40,13 +40,17 @@ __all__ = [
 ]
 
 
-class PiglitJSONEncoder(json.JSONEncoder):
-    def default(self, o):
-        if isinstance(o, status.Status):
-            return str(o)
-        elif isinstance(o, set):
-            return list(o)
-        return json.JSONEncoder.default(self, o)
+def _piglit_encoder(obj):
+    """ Encoder for piglit that can transform additional classes into json
+
+    Adds support for status.Status objects and for set() instances
+
+    """
+    if isinstance(obj, status.Status):
+        return str(obj)
+    elif isinstance(obj, set):
+        return list(obj)
+    return obj
 
 
 class JSONWriter:
@@ -98,7 +102,8 @@ class JSONWriter:
         self.file = file
         self.__indent_level = 0
         self.__inhibit_next_indent = False
-        self.__encoder = PiglitJSONEncoder(indent=self.INDENT)
+        self.__encoder = json.JSONEncoder(indent=self.INDENT,
+                                          default=_piglit_encoder)
 
         # self.__is_collection_empty
         #
diff --git a/framework/tests/dmesg_tests.py b/framework/tests/dmesg_tests.py
index ff70e2d..c719d99 100644
--- a/framework/tests/dmesg_tests.py
+++ b/framework/tests/dmesg_tests.py
@@ -24,10 +24,11 @@ import os
 import sys
 import subprocess
 import re
+import json
 import nose.tools as nt
 from nose.plugins.skip import SkipTest
 from framework.dmesg import DummyDmesg, LinuxDmesg, get_dmesg
-from framework.results import TestResult, PiglitJSONEncoder
+from framework.results import TestResult, _piglit_encoder
 from framework.exectest import PiglitTest
 from framework.gleantest import GleanTest
 from framework.shader_test import ShaderTest
@@ -258,9 +259,9 @@ def test_update_result_add_dmesg():
 
 def test_json_serialize_updated_result():
     """ Test that a TestResult that has been updated is json serializable """
-    encoder = PiglitJSONEncoder()
     result = test_update_result_add_dmesg()
-    encoded = encoder.encode(result)
+    encoder = json.JSONEncoder(default=_piglit_encoder)
+    encoder.encode(result)
 
 
 def test_testclasses_dmesg():
-- 
2.0.0



More information about the Piglit mailing list