In a unit test case that I am running, I get a KeyError exception on the 4th json object in the json text below because the piece of code responsible for decoding is looking for an object that isn't there, but should be.
I went through the sub-objects and found that it was the "cpuid" object that causes the problem. When I remove it and run the test, it works fine.
def _make_report_entry(record):
response = self.app.post(
'/machinestats',
params=dict(record=self.json_encode([
{"type": "crash", "instance_id": "xxx",
"version": "0.2.0", "build_id": "unknown",
"crash_text": "Gah!"},
{"type": "machine_info", "machine_info": "I'm awesome.",
"version": "0.2.0", "build_id": "unknown",
"instance_id": "yyy"},
{"machine_info": "Soup", "crash_text": "boom!",
"version": "0.2.0", "build_id": "unknown",
"instance_id": "zzz", "type": "crash"},
{"build_id" : "unknown", "cpu_brand" : "intel",
"cpu_count" : 4,
"cpuid": {
"00000000":
{"eax" :123,"ebx" :456,
"ecx" :789,"edx" :321},
"00000001":
{"eax" :123,"ebx" :456,
"ecx" :789,"edx" :321}},
"driver_installed" : True,
"instance_id" : "yyy",
"version" : "0.2.0",
"machine_info" : "I'm awesome.",
"os_version" : "linux",
"physical_memory_mib" : 1024,
"product_loaded" : True,
"type" : "machine_info",
"virtualization_advertised" : True}
])))
In the piece of code being tested, I use simplejson.JSONDecoder from django.utils to decode the JSON. When I log the decoded output for the above JSON that gets passed to my decoding function, I get this:
root: INFO: {u'instance_id': u'xxx', u'type': u'crash', u'crash_text': u'Gah!', u'version': u'0.2.0', u'build_id': u'unknown'}
root: INFO: {u'build_id': u'unknown', u'instance_id': u'yyy', u'version': u'0.2.0', u'machine_info': u"I'm awesome.", u'type': u'machine_info'}
root: INFO: {u'build_id': u'unknown', u'machine_info': u'Soup', u'version': u'0.2.0', u'instance_id': u'zzz', u'crash_text': u'boom!', u'type': u'crash'}
root: INFO: {u'eax': 123, u'edx': 321, u'ebx': 456, u'ecx': 789}
On the last JSON object, only the object within the JSON cpuid object is being passed to my decoding function. Because my decoding function is expecting the the other objects (e.g., 'type', 'instance_id', etc.), I get a KeyError exception.
[Sorry for the earlier unnecessarily long post, I hope that this will narrow it down a bit more]