Merge pull request #2149 from mhils/multidictview-copy

Add MultiDictView.copy()
This commit is contained in:
Maximilian Hils 2017-03-14 14:38:38 +01:00 committed by GitHub
commit 2d016775fe
2 changed files with 24 additions and 12 deletions

View File

@ -4,7 +4,7 @@ from collections.abc import MutableMapping
from mitmproxy.types import serializable from mitmproxy.types import serializable
class _MultiDict(MutableMapping, serializable.Serializable, metaclass=ABCMeta): class _MultiDict(MutableMapping, metaclass=ABCMeta):
def __repr__(self): def __repr__(self):
fields = ( fields = (
repr(field) repr(field)
@ -171,18 +171,8 @@ class _MultiDict(MutableMapping, serializable.Serializable, metaclass=ABCMeta):
coll.append([key, values]) coll.append([key, values])
return coll return coll
def get_state(self):
return self.fields
def set_state(self, state): class MultiDict(_MultiDict, serializable.Serializable):
self.fields = tuple(tuple(x) for x in state)
@classmethod
def from_state(cls, state):
return cls(state)
class MultiDict(_MultiDict):
def __init__(self, fields=()): def __init__(self, fields=()):
super().__init__() super().__init__()
self.fields = tuple( self.fields = tuple(
@ -197,6 +187,16 @@ class MultiDict(_MultiDict):
def _kconv(key): def _kconv(key):
return 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): class MultiDictView(_MultiDict):
""" """
@ -227,3 +227,6 @@ class MultiDictView(_MultiDict):
@fields.setter @fields.setter
def fields(self, value): def fields(self, value):
self._setter(value) self._setter(value)
def copy(self):
return MultiDict(self.fields)

View File

@ -200,3 +200,12 @@ class TestMultiDictView:
tv["c"] = "b" tv["c"] = "b"
assert p.vals == (("a", "b"), ("c", "b")) assert p.vals == (("a", "b"), ("c", "b"))
assert tv["a"] == "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()