diff options
author | Maximilian Hils <git@maximilianhils.com> | 2016-04-29 20:34:12 -0700 |
---|---|---|
committer | Maximilian Hils <git@maximilianhils.com> | 2016-04-29 20:59:26 -0700 |
commit | 74cfd7a4e2a64aa5ee3c98c3c7a0e2e668779618 (patch) | |
tree | fc3bba451a43b9e34b6dca88bc60ac5dd1af4427 /mitmproxy/stateobject.py | |
parent | cb1119f3eebc57914fc6093f0afcc7b3cd88fcc7 (diff) | |
download | mitmproxy-74cfd7a4e2a64aa5ee3c98c3c7a0e2e668779618.tar.gz mitmproxy-74cfd7a4e2a64aa5ee3c98c3c7a0e2e668779618.tar.bz2 mitmproxy-74cfd7a4e2a64aa5ee3c98c3c7a0e2e668779618.zip |
stateobject: support lists
Diffstat (limited to 'mitmproxy/stateobject.py')
-rw-r--r-- | mitmproxy/stateobject.py | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/mitmproxy/stateobject.py b/mitmproxy/stateobject.py index fff6e116..765c35d6 100644 --- a/mitmproxy/stateobject.py +++ b/mitmproxy/stateobject.py @@ -1,10 +1,18 @@ from __future__ import absolute_import import six +from typing import List, Any from netlib.utils import Serializable +def _is_list(cls): + # The typing module backport is somewhat broken. + # Python 3.5 or 3.6 should fix this. + is_list_bugfix = getattr(cls, "__origin__", False) == getattr(List[Any], "__origin__", True) + return issubclass(cls, List) or is_list_bugfix + + class StateObject(Serializable): """ @@ -28,8 +36,12 @@ class StateObject(Serializable): state = {} for attr, cls in six.iteritems(self._stateobject_attributes): val = getattr(self, attr) - if hasattr(val, "get_state"): + if val is None: + state[attr] = None + elif hasattr(val, "get_state"): state[attr] = val.get_state() + elif _is_list(cls): + state[attr] = [x.get_state() for x in val] else: state[attr] = val return state @@ -49,6 +61,9 @@ class StateObject(Serializable): elif hasattr(cls, "from_state"): obj = cls.from_state(state.pop(attr)) setattr(self, attr, obj) + elif _is_list(cls): + cls = cls.__parameters__[0] + setattr(self, attr, [cls.from_state(x) for x in state.pop(attr)]) else: # primitive types such as int, str, ... setattr(self, attr, cls(state.pop(attr))) if state: |