splunklib.searchcommands.splunk_csv.dict_reader raises KeyError when supports_multivalues=True
This error seems to occur because dict_reader.py populates the fieldnames property with only the non __mv_* field names, which causes super(DictReader, self).next() to return a dict where the __mv_* fields are grouped together in a single dictionary entry.
Example:
-
Splunk passes a set of field names to the custom command:
test1,test2,__mv_test1,__mv_test2 -
splunk_csv.DictReader.fieldnamesgets called which results in:csv.DictReader._fieldnames = ["test1", "test2","__mv_test1","__mv_test2"] splunk_csv.DictReader.__fieldnames = ["test1", "test2"] splunk_csv.DictReader.__mv_fieldnames = [("test1","__mv_test1"), ("test2","__mv_test2")] return __fieldnamesNote that
__fieldnamesis returned. -
splunk_csv.DictReader.nextis called, which in turn callscsv.DictReader.nextand reads an event from Splunk:"1 2 3","9193","$1$,$2$,$3$", -
csv.DictReader.nextcombines the event andfieldnamesproperty...d = dict(zip(self.fieldnames, row)) lf = len(self.fieldnames) lr = len(row) if lf < lr: d[self.restkey] = row[lf:] elif lf > lr: for key in self.fieldnames[lr:]: d[key] = self.restval return dBecause this method uses
fieldnames,dnow contains:[("test1","1 2 3"),("test2","9193"),(self.restkey,["$1$,$2$,$3$",""])] -
splunk_csv.DictReader.nextiterates overself.__mv_fieldnamesand attempts to look up "__mv_test1" in the dictionary, which results in a KeyError.
Running Splunk 6.2.4 Python SDK 1.3.1
Thanks @brattonc, we're working on updating search commands. We'll be sure to address this when those changes are released
For my relatively simple streaming command, switching fieldnames to return _fieldnames instead of __fieldnames fixed this bug without any noticeable side effects. I'm running Python SDK 1.4.0 on Splunk 6.2.4.
We've just released v1.5.0 to address this, thanks again for the bug report @ahoying @brattonc