diff options
author | Aldo Cortesi <aldo@nullcube.com> | 2016-06-02 11:37:18 +1200 |
---|---|---|
committer | Aldo Cortesi <aldo@nullcube.com> | 2016-06-02 11:37:18 +1200 |
commit | eaa3b308f7bb48256ccf56ea07d008fa5f9dd6ad (patch) | |
tree | a9a73ca4695beb00a6d83aa65cdc17945fc77107 /netlib/multidict.py | |
parent | 92cdca50c7a43a5ab59b435e73e3b17dbe01cd3b (diff) | |
download | mitmproxy-eaa3b308f7bb48256ccf56ea07d008fa5f9dd6ad.tar.gz mitmproxy-eaa3b308f7bb48256ccf56ea07d008fa5f9dd6ad.tar.bz2 mitmproxy-eaa3b308f7bb48256ccf56ea07d008fa5f9dd6ad.zip |
Fix non-deterministic test failures in export
We had various places in the code where we relied on incidental order of dict
keys. Add a helper to multidict, and fix.
Diffstat (limited to 'netlib/multidict.py')
-rw-r--r-- | netlib/multidict.py | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/netlib/multidict.py b/netlib/multidict.py index 0350ae4f..dc0f3466 100644 --- a/netlib/multidict.py +++ b/netlib/multidict.py @@ -178,6 +178,22 @@ class _MultiDict(MutableMapping, basetypes.Serializable): if key in self: del self[key] + def collect(self): + """ + Returns a list of (key, value) tuples, where values are either + singular if threre is only one matching item for a key, or a list + if there are more than one. The order of the keys matches the order + in the underlying fields list. + """ + coll = [] + for key in self: + values = self.get_all(key) + if len(values) == 1: + coll.append([key, values[0]]) + else: + coll.append([key, values]) + return coll + def to_dict(self): """ Get the MultiDict as a plain Python dict. @@ -197,12 +213,8 @@ class _MultiDict(MutableMapping, basetypes.Serializable): } """ d = {} - for key in self: - values = self.get_all(key) - if len(values) == 1: - d[key] = values[0] - else: - d[key] = values + for k, v in self.collect(): + d[k] = v return d def get_state(self): |