core: allow bytes types in cpstruct utils function

This commit is contained in:
p1-bmu 2022-04-26 10:43:40 +02:00
parent 1a6d4d6ab8
commit af73dcec2b
1 changed files with 4 additions and 2 deletions

View File

@ -383,6 +383,8 @@ def uint_to_hex(uint, bitlen):
# structure duplication routine # structure duplication routine
#------------------------------------------------------------------------------# #------------------------------------------------------------------------------#
_atomic_types = str_types + bytes_types + integer_types + (NoneType, bool)
def cpstruct(struct, wobj=False): def cpstruct(struct, wobj=False):
"""Return an identical object to struct in terms of internal values, but with """Return an identical object to struct in terms of internal values, but with
copied wrapping structures. Values need to be integer or string (or any object copied wrapping structures. Values need to be integer or string (or any object
@ -398,14 +400,14 @@ def cpstruct(struct, wobj=False):
ValueError in case of invalid struct type ValueError in case of invalid struct type
except if wobj is set to True, where any object is returned as is except if wobj is set to True, where any object is returned as is
""" """
atomic_types = str_types + integer_types + (NoneType, bool)
if isinstance(struct, (tuple, list, set)): if isinstance(struct, (tuple, list, set)):
return struct.__class__([cpstruct(i) for i in struct]) return struct.__class__([cpstruct(i) for i in struct])
elif isinstance(struct, dict): elif isinstance(struct, dict):
return {cpstruct(i): cpstruct(j) for i, j in struct.items()} return {cpstruct(i): cpstruct(j) for i, j in struct.items()}
elif wobj: elif wobj:
return struct return struct
elif isinstance(struct, atomic_types): elif isinstance(struct, _atomic_types):
return struct return struct
else: else:
raise(ValueError('invalid argument of type %s' % type(struct))) raise(ValueError('invalid argument of type %s' % type(struct)))