Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
16: ('e', 'H'),
32: ('f', 'I'),
64: ('d', 'Q'),
}[data_type.bit_length]
fmt_prefix = '<'
out, = struct.unpack(fmt_prefix + unpack_fmt, struct.pack(fmt_prefix + pack_fmt, int_value))
return out
elif isinstance(data_type, pydsdl.FixedLengthArrayType):
return [_make_random_object(data_type.element_type) for _ in range(data_type.capacity)]
elif isinstance(data_type, pydsdl.VariableLengthArrayType):
length = random.randint(0, data_type.capacity)
return [_make_random_object(data_type.element_type) for _ in range(length)]
elif isinstance(data_type, pydsdl.StructureType):
o = pyuavcan.dsdl.get_generated_class(data_type)()
for f in data_type.fields_except_padding:
v = _make_random_object(f.data_type)
pyuavcan.dsdl.set_attribute(o, f.name, v)
return o
elif isinstance(data_type, pydsdl.UnionType):
f = random.choice(data_type.fields)
v = _make_random_object(f.data_type)
o = pyuavcan.dsdl.get_generated_class(data_type)()
pyuavcan.dsdl.set_attribute(o, f.name, v)
return o
else: # pragma: no cover
raise TypeError(f'Unsupported type: {type(data_type)}')
et = model.element_type
if isinstance(et, pydsdl.UnsignedIntegerType) and et.bit_length == 8: # Special case for faster testing
out = numpy.random.randint(0, 256, size=length, dtype=numpy.uint8)
else:
out = [make_random_object(model.element_type) for _ in range(length)]
if length < 10000: # pragma: no branch
if isinstance(et, pydsdl.UnsignedIntegerType) and et.bit_length <= 8 and fifty_fifty():
out = bytes(out)
if model.string_like and fifty_fifty():
try:
out = bytes(out).decode()
except ValueError:
pass
return out
elif isinstance(model, pydsdl.StructureType):
o = pyuavcan.dsdl.get_class(model)()
for f in model.fields_except_padding:
v = make_random_object(f.data_type)
pyuavcan.dsdl.set_attribute(o, f.name, v)
return o
elif isinstance(model, pydsdl.UnionType):
f = random.choice(model.fields)
v = make_random_object(f.data_type)
o = pyuavcan.dsdl.get_class(model)()
pyuavcan.dsdl.set_attribute(o, f.name, v)
return o
else: # pragma: no cover
raise TypeError(f'Unsupported type: {type(model)}')