aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortpavelchak2020-02-10 13:28:50 +0200
committertpavelchak2020-02-10 13:28:50 +0200
commit8c2efb31153037b258c96a108aa539615ef8312d (patch)
treefaf109a0a0bc7584c0b309965b4d3379dc986222
parent962903afc7634d52444ec047a4ee59d9e19ec330 (diff)
downloadeventmq-8c2efb31153037b258c96a108aa539615ef8312d.tar.gz
eventmq-8c2efb31153037b258c96a108aa539615ef8312d.zip
Fix Python 2/3 compatibility issues
- fix params for `zsocket.send_multipart()`
-rw-r--r--eventmq/publisher.py3
-rw-r--r--eventmq/tests/utils.py21
-rw-r--r--eventmq/utils/classes.py6
-rw-r--r--eventmq/utils/messages.py6
4 files changed, 22 insertions, 14 deletions
diff --git a/eventmq/publisher.py b/eventmq/publisher.py
index cf29a2e..068c31f 100644
--- a/eventmq/publisher.py
+++ b/eventmq/publisher.py
@@ -67,7 +67,8 @@ class Publisher():
67 67
68 def publish(self, topic, msg): 68 def publish(self, topic, msg):
69 logger.debug("Notifying topic: {}".format(topic)) 69 logger.debug("Notifying topic: {}".format(topic))
70 return self.zsocket.send_multipart([topic, msg]) 70 return self.zsocket.send_multipart([six.ensure_binary(topic),
71 six.ensure_binary(msg)])
71 72
72 @property 73 @property
73 def ready(self): 74 def ready(self):
diff --git a/eventmq/tests/utils.py b/eventmq/tests/utils.py
index 23f34c0..2d275ed 100644
--- a/eventmq/tests/utils.py
+++ b/eventmq/tests/utils.py
@@ -14,6 +14,7 @@
14# along with eventmq. If not, see <http://www.gnu.org/licenses/>. 14# along with eventmq. If not, see <http://www.gnu.org/licenses/>.
15import uuid 15import uuid
16 16
17import six
17import zmq 18import zmq
18 19
19from .. import conf, constants 20from .. import conf, constants
@@ -48,12 +49,12 @@ def send_raw_INFORM(sock, type_, queues=(conf.DEFAULT_QUEUE_NAME,)):
48 """ 49 """
49 msgid = str(uuid.uuid4()) 50 msgid = str(uuid.uuid4())
50 tracker = sock.zsocket.send_multipart(( 51 tracker = sock.zsocket.send_multipart((
51 '', 52 b'',
52 constants.PROTOCOL_VERSION, 53 six.ensure_binary(constants.PROTOCOL_VERSION),
53 'INFORM', 54 b'INFORM',
54 msgid, 55 six.ensure_binary(msgid),
55 ','.join(queues), 56 six.ensure_binary(','.join(queues)),
56 type_ 57 six.ensure_binary(type_)
57 ), copy=False, track=True) 58 ), copy=False, track=True)
58 tracker.wait(1) 59 tracker.wait(1)
59 60
@@ -72,10 +73,10 @@ def send_raw_READY(sock):
72 """ 73 """
73 msgid = str(uuid.uuid4()) 74 msgid = str(uuid.uuid4())
74 tracker = sock.zsocket.send_multipart(( 75 tracker = sock.zsocket.send_multipart((
75 '', 76 b'',
76 constants.PROTOCOL_VERSION, 77 six.ensure_binary(constants.PROTOCOL_VERSION),
77 'READY', 78 b'READY',
78 msgid 79 six.ensure_binary(msgid)
79 ), copy=False, track=True) 80 ), copy=False, track=True)
80 tracker.wait(1) 81 tracker.wait(1)
81 82
diff --git a/eventmq/utils/classes.py b/eventmq/utils/classes.py
index 14409ea..50047a8 100644
--- a/eventmq/utils/classes.py
+++ b/eventmq/utils/classes.py
@@ -411,6 +411,10 @@ class ZMQSendMixin(object):
411 411
412 msg = encodify(headers + message) 412 msg = encodify(headers + message)
413 413
414 # Decode bytes to strings in python3
415 if sys.version[0] == '3' and type(msg[0] in (bytes,)):
416 msg = [m.decode() for m in msg]
417
414 # If it's not at least 4 frames long then most likely it isn't an 418 # If it's not at least 4 frames long then most likely it isn't an
415 # eventmq message 419 # eventmq message
416 if len(msg) > 4 and \ 420 if len(msg) > 4 and \
@@ -419,7 +423,7 @@ class ZMQSendMixin(object):
419 logger.debug('Sending message: %s' % str(msg)) 423 logger.debug('Sending message: %s' % str(msg))
420 424
421 try: 425 try:
422 self.zsocket.send_multipart(msg, 426 self.zsocket.send_multipart([six.ensure_binary(m) for m in msg],
423 flags=zmq.NOBLOCK) 427 flags=zmq.NOBLOCK)
424 except zmq.error.ZMQError as e: 428 except zmq.error.ZMQError as e:
425 if 'No route' in str(e): 429 if 'No route' in str(e):
diff --git a/eventmq/utils/messages.py b/eventmq/utils/messages.py
index d652c4c..33e53d3 100644
--- a/eventmq/utils/messages.py
+++ b/eventmq/utils/messages.py
@@ -18,6 +18,8 @@
18""" 18"""
19import logging 19import logging
20 20
21import six
22
21from . import random_characters 23from . import random_characters
22from .. import conf, constants, exceptions 24from .. import conf, constants, exceptions
23 25
@@ -153,7 +155,7 @@ def fwd_emqp_router_message(socket, recipient_id, payload):
153 Args: 155 Args:
154 socket: socket to send the message with 156 socket: socket to send the message with
155 recipient_id (str): the id of the connected device to reply to 157 recipient_id (str): the id of the connected device to reply to
156 payload (tuple): The message to send. The first frame should be an 158 payload (list): The message to send. The first frame should be an
157 empty string 159 empty string
158 """ 160 """
159 import zmq 161 import zmq
@@ -166,7 +168,7 @@ def fwd_emqp_router_message(socket, recipient_id, payload):
166 if conf.SUPER_DEBUG: 168 if conf.SUPER_DEBUG:
167 logger.debug('Forwarding message: {}'.format(str(payload))) 169 logger.debug('Forwarding message: {}'.format(str(payload)))
168 try: 170 try:
169 socket.zsocket.send_multipart(payload, 171 socket.zsocket.send_multipart([six.ensure_binary(x) for x in payload],
170 flags=zmq.NOBLOCK) 172 flags=zmq.NOBLOCK)
171 except zmq.error.ZMQError as e: 173 except zmq.error.ZMQError as e:
172 if e.errno in errnos: 174 if e.errno in errnos: