iotile / iotile/coretools

`struct.pack` rounding error in `pack_rpc_payload`

Open
#739 2 comments 0 reactions 1 assignee Claimed by @mattrunchey View on GitHub
class:self-contained type:bug
Dominant language
Python
Stars
14
Forks
7
PR merge metrics
No merged PRs in 30d

Description

## Float packing error

In `pack_rpc_payload` (`iotile/core/hw/virtual/common_types.py`) .
When packing floats, the `pack` function of the `struct` package may introduce an error on the packed value that causes the validation to fail. The validation should take this behavior into account. The issue affects both `rpc` and `rpc_v2`.
Here is an example with `pack_rpc_payload` :

```
>>> from iotile.core.hw.virtual import pack_rpc_payload
>>> arg_format = "f"
>>> args = [2.6]
>>> pack_rpc_payload(arg_format, args)
Traceback (most recent call last):
File "", line 1, in
File "/Users/julienraspaud/espaceProject/candidate_coding_challenges/radiation/.venv/lib/python3.7/site-packages/iotile/core/hw/virtual/common_types.py", line 100, in pack_rpc_payload
code=code, args=args)
iotile.core.hw.virtual.common_types.RPCInvalidArgumentsError: RPCInvalidArgumentsError: Passed values would be truncated, please validate the size of your string
Additional Information:
code: >> import struct
>>> code = ">> args = [2.6]
>>> packed_result = struct.pack(code, *args)
>>> unpacked_validation = struct.unpack(code, packed_result)
>>> print(unpacked_validation)
(2.5999999046325684,)
```

### Information

Python version : 3.7.2

Versions of iotile dependencies installed:
```
iotile-core==4.1.0
iotile-test==1.0.2
iotile-transport-bled112==2.0.2
```

System :
```
ProductName: Mac OS X
ProductVersion: 10.12.6
BuildVersion: 16G1815
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.