From de1de3f626c142df759cd14aac5c46fd878d2d3c Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Tue, 14 Mar 2017 02:18:02 +0100 Subject: [PATCH 1/2] add MultiDictView.copy --- mitmproxy/types/multidict.py | 3 +++ test/mitmproxy/types/test_multidict.py | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/mitmproxy/types/multidict.py b/mitmproxy/types/multidict.py index 31a1f22b7..ac49200b6 100644 --- a/mitmproxy/types/multidict.py +++ b/mitmproxy/types/multidict.py @@ -230,3 +230,6 @@ class MultiDictView(_MultiDict): @fields.setter def fields(self, value): self._setter(value) + + def copy(self): + return MultiDict(self.fields) diff --git a/test/mitmproxy/types/test_multidict.py b/test/mitmproxy/types/test_multidict.py index 9b13c5cd8..7d38f6ba6 100644 --- a/test/mitmproxy/types/test_multidict.py +++ b/test/mitmproxy/types/test_multidict.py @@ -205,3 +205,12 @@ class TestMultiDictView: tv["c"] = "b" assert p.vals == (("a", "b"), ("c", "b")) assert tv["a"] == "b" + + def test_copy(self): + p = TParent() + tv = multidict.MultiDictView(p.getter, p.setter) + c = tv.copy() + assert isinstance(c, multidict.MultiDict) + assert tv.items() == c.items() + c["foo"] = "bar" + assert tv.items() != c.items() From 30fd285ec724623f17e88cf2121d061fa8c53638 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Tue, 14 Mar 2017 02:20:24 +0100 Subject: [PATCH 2/2] multidictviews should not be serializable --- mitmproxy/types/multidict.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/mitmproxy/types/multidict.py b/mitmproxy/types/multidict.py index ac49200b6..36a1d6cd2 100644 --- a/mitmproxy/types/multidict.py +++ b/mitmproxy/types/multidict.py @@ -4,7 +4,7 @@ from collections.abc import MutableMapping from mitmproxy.types import serializable -class _MultiDict(MutableMapping, serializable.Serializable, metaclass=ABCMeta): +class _MultiDict(MutableMapping, metaclass=ABCMeta): def __repr__(self): fields = ( repr(field) @@ -174,18 +174,8 @@ class _MultiDict(MutableMapping, serializable.Serializable, metaclass=ABCMeta): coll.append([key, values]) return coll - def get_state(self): - return self.fields - def set_state(self, state): - self.fields = tuple(tuple(x) for x in state) - - @classmethod - def from_state(cls, state): - return cls(state) - - -class MultiDict(_MultiDict): +class MultiDict(_MultiDict, serializable.Serializable): def __init__(self, fields=()): super().__init__() self.fields = tuple( @@ -200,6 +190,16 @@ class MultiDict(_MultiDict): def _kconv(key): return key + def get_state(self): + return self.fields + + def set_state(self, state): + self.fields = tuple(tuple(x) for x in state) + + @classmethod + def from_state(cls, state): + return cls(state) + class MultiDictView(_MultiDict): """