Skip to content

fix message.payload str casting. #1313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2018

Conversation

nectarine
Copy link

@nectarine nectarine commented Jan 11, 2018

Env: Python 3.6.4 (default, Jan 6 2018, 11:51:59)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin

cloudiot_pubsub_example_mqtt_device.py.py in iot end_to_end_example contains wrong str casting.
json.loads takes byte parameter, so you don't have to cast for using it.
If you really want to convert byte to string, it should be

payload = message.payload.decode()

current version produces an error

File "cloudiot_pubsub_example_mqtt_device.py", line 136, in on_message data = json.loads(payload) File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/__init__.py", line 354, in loads return _default_decoder.decode(s) File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/decoder.py", line 339, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/decoder.py", line 357, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Jan 11, 2018
@theacodes theacodes requested a review from gguuss January 11, 2018 17:47
@theacodes
Copy link
Contributor

@gguuss can you take a look at this?

@@ -121,7 +121,7 @@ def on_subscribe(self, unused_client, unused_userdata, unused_mid,

def on_message(self, unused_client, unused_userdata, message):
"""Callback when the device receives a message on a subscription."""
payload = str(message.payload)
payload = message.payload
print('Received message \'{}\' on topic \'{}\' with Qos {}'.format(
payload, message.topic, str(message.qos)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should you remove the str(message.qos) from this line too?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A string formatter will automatically cast it into string. So, it could be an useless casting, but really doesn't matter. (message.qos is an int type, I guess)

The perfect printing might be like

print('Received message \'{}\' on topic \'{}\' with Qos {}'.format(
             payload.decode(), message.topic, message.qos))

But exposing type of payload could be useful to specify that it is a byte type.

Therefore, it just depends on the author's intention, just for logging or providing extra information though the log.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can leave as-is.

Copy link
Contributor

@gguuss gguuss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested, works for me in python 2/3

@gguuss gguuss merged commit 5647df0 into GoogleCloudPlatform:master Feb 21, 2018
@nectarine nectarine deleted the iot/end_to_end_example branch February 22, 2018 05:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants