paho.mqtt.python icon indicating copy to clipboard operation
paho.mqtt.python copied to clipboard

Message data instance types not validated when passed down in UserData -> ugly exception

Open ikkemaniac opened this issue 3 years ago • 1 comments

paho.mqtt.__version__: 1.5.1 py version: 3.9.7

When using publish.multiple() and supplying "retain": str("True") the following ugly exception will be returned:

Exception has occurred: TypeError
unsupported operand type(s) for |: 'int' and 'str'
  File "myfile.py", line 26, in send_stuff
    publish.multiple(
...

was able to trace it down to:

Exception has occurred: TypeError 
unsupported operand type(s) for |: 'int' and 'str'
  File "/usr/lib/python3/dist-packages/paho/mqtt/client.py", line 2507, in _send_publish
    command = PUBLISH | ((dup & 0x1) << 3) | (qos << 1) | retain
...

so the exception happens here (v1.5.1):

https://github.com/eclipse/paho.mqtt.python/blob/42f0b13001cb39aee97c2b60a3b4807314dfcb4d/src/paho/mqtt/client.py#L2497-L2507

retain in my case was str not bool and thus results in an str to int comparison

this can probably easily be fixed by adding but i'm unfamiliar with y'alls coding practices. and isinstance(retain, bool) at the end of the assert https://github.com/eclipse/paho.mqtt.python/blob/42f0b13001cb39aee97c2b60a3b4807314dfcb4d/src/paho/mqtt/client.py#L2500-L2501

and isinstance(retain, bool)

ikkemaniac avatar Jan 07 '22 21:01 ikkemaniac

I can see your point; the exception is a bit difficult to understand. Having said that, I'm not sure that it's realistic to check the type of all parameters passed to the library. I'll tag this as an enhancement and we can see what others think.

MattBrittan avatar Jan 08 '24 02:01 MattBrittan