From c248946b3da4af3647329bc58af31e977f07cf03 Mon Sep 17 00:00:00 2001 From: matejcik Date: Tue, 30 Oct 2018 14:57:17 +0100 Subject: [PATCH] protobuf: make MessageType more dict-like, drop _add_ and _extend_ so that SignTx(**tx) works _add_x and _extend_x methods are left-overs from google protobuf and shouldn't be used anymore --- trezorlib/protobuf.py | 43 ++++--------------------------------------- 1 file changed, 4 insertions(+), 39 deletions(-) diff --git a/trezorlib/protobuf.py b/trezorlib/protobuf.py index c94362c8c8..19214fe995 100644 --- a/trezorlib/protobuf.py +++ b/trezorlib/protobuf.py @@ -150,46 +150,11 @@ class MessageType: def __iter__(self): return self.__dict__.__iter__() - def __getattr__(self, attr): - if attr.startswith("_add_"): - return self._additem(attr[5:]) + def keys(self): + return (name for name, _, _ in self.get_fields().values()) - if attr.startswith("_extend_"): - return self._extenditem(attr[8:]) - - raise AttributeError(attr) - - def _extenditem(self, attr): - def f(param): - try: - l = getattr(self, attr) - except AttributeError: - l = [] - setattr(self, attr, l) - - l += param - - return f - - def _additem(self, attr): - # Add new item for repeated field type - for v in self.get_fields().values(): - if v[0] != attr: - continue - if not (v[2] & FLAG_REPEATED): - raise AttributeError - - try: - l = getattr(self, v[0]) - except AttributeError: - l = [] - setattr(self, v[0], l) - - item = v[1]() - l.append(item) - return lambda: item - - raise AttributeError + def __getitem__(self, key): + return getattr(self, key) def _fill_missing(self): # fill missing fields