1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 23:48:12 +00:00

trezorlib/protobuf.py: return BytesType from wire as bytes, not bytearray.

This makes more sense, because bytes are immutable and callers have no business
mutating structures from the wire anyway.

Incidentally this should fix issue #236, where rlp library would treat
bytes and bytearrays differently and produce invalid structures in our usecase.

Also very minor nitpicks and code cleanup for neater typing.
This commit is contained in:
matejcik 2018-03-20 13:00:36 +01:00
parent f87d931ce7
commit afb3e04c24

View File

@ -244,12 +244,13 @@ def load_message(reader, msg_type):
elif ftype is BoolType: elif ftype is BoolType:
fvalue = bool(ivalue) fvalue = bool(ivalue)
elif ftype is BytesType: elif ftype is BytesType:
fvalue = bytearray(ivalue) buf = bytearray(ivalue)
reader.readinto(fvalue) reader.readinto(buf)
fvalue = bytes(buf)
elif ftype is UnicodeType: elif ftype is UnicodeType:
fvalue = bytearray(ivalue) buf = bytearray(ivalue)
reader.readinto(fvalue) reader.readinto(buf)
fvalue = fvalue.decode() fvalue = buf.decode()
elif issubclass(ftype, MessageType): elif issubclass(ftype, MessageType):
fvalue = load_message(LimitedReader(reader, ivalue), ftype) fvalue = load_message(LimitedReader(reader, ivalue), ftype)
else: else:
@ -270,10 +271,7 @@ def dump_message(writer, msg):
fields = mtype.FIELDS fields = mtype.FIELDS
for ftag in fields: for ftag in fields:
field = fields[ftag] fname, ftype, fflags = fields[ftag]
fname = field[0]
ftype = field[1]
fflags = field[2]
fvalue = getattr(msg, fname, None) fvalue = getattr(msg, fname, None)
if fvalue is None: if fvalue is None: