Skip to content
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

Make sure messages are consistently encoded as unicode #15

Merged
merged 1 commit into from
Nov 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions posttroll/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,14 @@ def __init__(self, subject='', atype='', data='', binary=False, rawstr=None):
if rawstr:
self.__dict__ = _decode(rawstr)
else:
self.subject = subject
try:
self.subject = subject.decode('utf-8')
except AttributeError:
self.subject = subject
try:
self.type = atype.decode('utf-8')
except AttributeError:
self.type = atype
self.type = atype
self.sender = _getsender()
self.time = datetime.utcnow()
Expand Down Expand Up @@ -168,9 +175,16 @@ def encode(self):
def __repr__(self):
return self.encode()

def __str__(self):
def __unicode__(self):
return self.encode()

def __str__(self):
try:
return unicode(self).encode('utf-8')
except NameError:
return self.encode()


def _validate(self):
"""Validate a messages attributes.
"""
Expand Down Expand Up @@ -299,7 +313,7 @@ def datetime_encoder(obj):
def _encode(msg, head=False, binary=False):
"""Convert a Message to a raw string.
"""
rawstr = str(_MAGICK) + "{0:s} {1:s} {2:s} {3:s} {4:s}".format(
rawstr = str(_MAGICK) + u"{0:s} {1:s} {2:s} {3:s} {4:s}".format(
msg.subject, msg.type, msg.sender, msg.time.isoformat(), msg.version)

if not head and msg.data:
Expand Down
4 changes: 2 additions & 2 deletions posttroll/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Publisher(object):
while True:
counter += 1
message = Message("/counter", "info", str(counter))
pub.send_string(str(message))
pub.send_string(message.encode())
time.sleep(3)
except KeyboardInterrupt:
print("terminating publisher...")
Expand Down Expand Up @@ -247,7 +247,7 @@ class Publish(NoisyPublisher):
counter += 1
message = Message("/counter", "info", str(counter))
print("publishing", message)
pub.send(str(message))
pub.send(message.encode())
time.sleep(3)
except KeyboardInterrupt:
print("terminating publisher...")
Expand Down
19 changes: 19 additions & 0 deletions posttroll/tests/test_pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,24 @@ def test_pub_suber(self):
self.assertTrue(tested)
pub.stop()

class TestPub(unittest.TestCase):

"""Testing the publishing capabilities.
"""

def setUp(self):
test_lock.acquire()

def tearDown(self):
test_lock.release()

def test_pub_unicode(self):
message = Message("/pџтяöll", "info", 'hej')
with Publish("a_service", 9000) as pub:
try:
pub.send(message.encode())
except UnicodeDecodeError:
self.fail("Sending raised UnicodeDecodeError unexpectedly!")

class TestListenerContainer(unittest.TestCase):

Expand Down Expand Up @@ -282,5 +300,6 @@ def suite():
mysuite.addTest(loader.loadTestsFromTestCase(TestNS))
mysuite.addTest(loader.loadTestsFromTestCase(TestNSWithoutMulticasting))
mysuite.addTest(loader.loadTestsFromTestCase(TestListenerContainer))
mysuite.addTest(loader.loadTestsFromTestCase(TestPub))

return mysuite