[Piglit] [PATCH V3] core.py: Fix sporadic loss of tests in summary

Dylan Baker baker.dylan.c at gmail.com
Mon Jun 3 22:14:57 PDT 2013


The checking in TestrunResult to determine if a file was proper json or
not is very fragile: it assumes that the last line of the file will be
}. Sometimes this triggers and rebuilds a valid json file, dropping the
last test and resulting in python overhead.

This patch replaces that check with a try/except block. This block
attempts to load the json file with json.load, and on failure attempts
to fix the json.

V2: - replaces json.loads with json.load: this was causing an error but
	  being caught by the except block.
	- Corrects issue created by the try block changing file.tell() so
	  that file.seek would fail in self.__repairFile()
V3: - refactor duplicated code

Signed-off-by: Dylan Baker <baker.dylan.c at gmail.com>
---
 framework/core.py | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/framework/core.py b/framework/core.py
index 14a8161..df6bece 100644
--- a/framework/core.py
+++ b/framework/core.py
@@ -297,14 +297,8 @@ class TestrunResult:
                 is returned.
         '''
 
-        saved_position = file.tell()
+        file.seek(0)
         lines = file.readlines()
-        file.seek(saved_position)
-
-        if lines[-1] == '}':
-            # JSON object was closed properly. No repair is
-            # necessary.
-            return file
 
         # JSON object was not closed properly.
         #
@@ -353,13 +347,17 @@ class TestrunResult:
         json.dump(raw_dict, file, indent=JSONWriter.INDENT)
 
     def parseFile(self, file):
-        file = self.__repairFile(file)
-        raw_dict = json.load(file)
+        # Attempt to open the json file normally, if it fails then attempt to
+        # repair it.
+        try:
+            raw_dict = json.load(file)
+        except ValueError:
+            raw_dict = json.load(self.__repairFile(file))
 
         # Check that only expected keys were unserialized.
         for key in raw_dict:
             if key not in self.serialized_keys:
-                raise Exception('unexpected key in results file: ' + str(key))
+                raise Exception('unexpected key in results file: ', str(key))
 
         self.__dict__.update(raw_dict)
 
-- 
1.8.1.4



More information about the Piglit mailing list