]> git.michaelhowe.org Git - packages/p/paho-mqtt.git/commitdiff
Handle "unicode" type payloads on Python 2.7.
authorRoger A. Light <roger@atchoo.org>
Fri, 1 Aug 2014 09:08:11 +0000 (10:08 +0100)
committerRoger A. Light <roger@atchoo.org>
Fri, 1 Aug 2014 09:08:11 +0000 (10:08 +0100)
Thanks to Luc Milland.

Change-Id: I1f3bf49ed3845f61cbd31773fcf11a58943f8108

ChangeLog.txt
examples/pub-utf8-27.py [new file with mode: 0755]
src/paho/mqtt/client.py

index b2a50e92c1182d6d444cd59910bdfb1e30f3eb50..b34b91bb62060903af87b5669ed270512f4254f8 100644 (file)
@@ -4,7 +4,8 @@ v1.0.2
 - Fix "protocol" not being used in publish.single()
 - Fix Client constructor for the case where "localhost" is unresolvable.
   Closes #439277.
-- Don't attempt to encode topic to utf-8 twice.
+- Don't attempt to encode topic to utf-8 twice. Thanks to Luc Milland.
+- Handle "unicode" type payloads on Python 2.7. Thanks to Luc Milland.
 
 v1.0.1
 ======
diff --git a/examples/pub-utf8-27.py b/examples/pub-utf8-27.py
new file mode 100755 (executable)
index 0000000..a639b47
--- /dev/null
@@ -0,0 +1,35 @@
+#!/usr/bin/python
+# vim: set fileencoding=utf-8
+
+# Copyright (c) 2014 Roger Light <roger@atchoo.org>
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Eclipse Distribution License v1.0
+# which accompanies this distribution. 
+#
+# The Eclipse Distribution License is available at 
+#   http://www.eclipse.org/org/documents/edl-v10.php.
+#
+# Contributors:
+#    Roger Light - initial implementation
+
+# This shows an example of using the publish.single helper function.
+
+import sys
+try:
+    import paho.mqtt.publish as publish
+except ImportError:
+    # This part is only required to run the example from within the examples
+    # directory when the module itself is not installed.
+    #
+    # If you have the module installed, just use "import paho.mqtt.publish"
+    import os
+    import inspect
+    cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"../src")))
+    if cmd_subfolder not in sys.path:
+        sys.path.insert(0, cmd_subfolder)
+    import paho.mqtt.publish as publish
+
+t=u"paho/test/single/ô"
+
+publish.single(t, u"bôô", hostname="test.mosquitto.org")
index 5d2c9108dcc84569e7b3fc5d1d9e44f2c61402e2..2ef4129525189d78ca3909843309ea79892ba89e 100755 (executable)
@@ -840,7 +840,7 @@ class Client(object):
             raise ValueError('Invalid topic.')
         if qos<0 or qos>2:
             raise ValueError('Invalid QoS level.')
-        if isinstance(payload, str) or isinstance(payload, bytearray):
+        if isinstance(payload, str) or isinstance(payload, bytearray) or isinstance(payload, unicode):
             local_payload = payload
         elif isinstance(payload, int) or isinstance(payload, float):
             local_payload = str(payload)