diff --git a/python/src/trezorlib/protobuf.py b/python/src/trezorlib/protobuf.py index 645a94519..d14741be0 100644 --- a/python/src/trezorlib/protobuf.py +++ b/python/src/trezorlib/protobuf.py @@ -161,7 +161,7 @@ class EnumType: def validate(self, fvalue: int) -> int: if fvalue not in self.enum_values: # raise TypeError("Invalid enum value") - LOG.warning("Value {} unknown for type {}".format(fvalue, self.enum_name)) + LOG.info("Value {} unknown for type {}".format(fvalue, self.enum_name)) return fvalue def to_str(self, fvalue: int) -> str: @@ -485,7 +485,10 @@ def format_message( return "{} bytes {}{}".format(length, output, suffix) if isinstance(value, int) and isinstance(ftype, EnumType): - return "{} ({})".format(ftype.to_str(value), value) + try: + return "{} ({})".format(ftype.to_str(value), value) + except TypeError: + return str(value) return repr(value) @@ -558,7 +561,10 @@ def to_dict(msg: MessageType, hexlify_bytes: bool = True) -> Dict[str, Any]: elif isinstance(value, list): return [convert_value(ftype, v) for v in value] elif isinstance(value, int) and isinstance(ftype, EnumType): - return ftype.to_str(value) + try: + return ftype.to_str(value) + except TypeError: + return value else: return value diff --git a/python/tests/test_protobuf_encoding.py b/python/tests/test_protobuf_encoding.py index 8108fdc54..7d7709f28 100644 --- a/python/tests/test_protobuf_encoding.py +++ b/python/tests/test_protobuf_encoding.py @@ -15,6 +15,7 @@ # If not, see . from io import BytesIO +import logging import pytest @@ -137,6 +138,7 @@ def test_simple_message(): def test_validate_enum(caplog): + caplog.set_level(logging.INFO) # round-trip of a valid value msg = EnumMessageMoreValues(enum=0) buf = BytesIO() @@ -154,7 +156,7 @@ def test_validate_enum(caplog): assert len(caplog.records) == 1 record = caplog.records.pop(0) - assert record.levelname == "WARNING" + assert record.levelname == "INFO" assert record.getMessage() == "Value 19 unknown for type t" msg.enum = 3 @@ -165,7 +167,7 @@ def test_validate_enum(caplog): assert len(caplog.records) == 1 record = caplog.records.pop(0) - assert record.levelname == "WARNING" + assert record.levelname == "INFO" assert record.getMessage() == "Value 3 unknown for type t" @@ -182,12 +184,14 @@ def test_repeated(): def test_enum_in_repeated(caplog): + caplog.set_level(logging.INFO) + msg = RepeatedFields(enumlist=[0, 1, 2, 3]) buf = BytesIO() protobuf.dump_message(buf, msg) assert len(caplog.records) == 2 for record in caplog.records: - assert record.levelname == "WARNING" + assert record.levelname == "INFO" assert "unknown for type t" in record.getMessage() diff --git a/python/tests/test_protobuf_misc.py b/python/tests/test_protobuf_misc.py index 1ff38a5bb..73d6efd44 100644 --- a/python/tests/test_protobuf_misc.py +++ b/python/tests/test_protobuf_misc.py @@ -216,3 +216,21 @@ def test_nested_recover(): dictdata = {"nested": {}} recovered = protobuf.dict_to_proto(NestedMessage, dictdata) assert isinstance(recovered.nested, SimpleMessage) + + +@with_simple_enum +def test_unknown_enum_to_str(): + simple = SimpleMessage(enum=SimpleEnum.QUUX) + string = protobuf.format_message(simple) + assert "enum: QUUX (13)" in string + + simple = SimpleMessage(enum=6000) + string = protobuf.format_message(simple) + assert "enum: 6000" in string + + +@with_simple_enum +def test_unknown_enum_to_dict(): + simple = SimpleMessage(enum=6000) + converted = protobuf.to_dict(simple) + assert converted["enum"] == 6000