Skip to content

Commit

Permalink
Encode reply strings in Python 2
Browse files Browse the repository at this point in the history
  • Loading branch information
icgood committed Apr 22, 2016
1 parent 38d196d commit fc532be
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
7 changes: 6 additions & 1 deletion slimta/smtp/reply.py
Expand Up @@ -30,6 +30,8 @@

import re

from slimta.util import pycompat

__all__ = ['Reply', 'unknown_command', 'unknown_parameter', 'bad_sequence',
'bad_arguments', 'timed_out', 'unhandled_error',
'tls_failure', 'invalid_credentials']
Expand Down Expand Up @@ -123,7 +125,10 @@ def __bool__(self):
return self.code is not None

# Python 2 compat.
__nonzero__ = __bool__
if pycompat.PY2:
__nonzero__ = __bool__
__unicode__ = __str__
__str__ = __bytes__

def copy(self, reply):
"""Direct-copies the given reply code and message into the current
Expand Down
13 changes: 9 additions & 4 deletions test/test_slimta_smtp_reply.py
Expand Up @@ -2,6 +2,7 @@

from slimta.smtp.reply import Reply
from slimta.smtp.io import IO
from slimta.util.pycompat import PY3


class TestSmtpReply(unittest.TestCase):
Expand Down Expand Up @@ -30,12 +31,16 @@ def test_repr(self):
self.assertEqual(expected, repr(r))

def test_str(self):
r = Reply('250', '2.1.0 Ok')
self.assertEqual('250 2.1.0 Ok', str(r))
if PY3:
r = Reply('250', '2.1.0 Ok \U0001f44d')
self.assertEqual('250 2.1.0 Ok \U0001f44d', str(r))
else:
r = Reply('250', u'2.1.0 Ok \U0001f44d')
self.assertEqual('250 2.1.0 Ok \xf0\x9f\x91\x8d', str(r))

def test_bytes(self):
r = Reply('250', '2.1.0 Ok')
self.assertEqual(b'250 2.1.0 Ok', bytes(r))
r = Reply('250', u'2.1.0 Ok \U0001f44d')
self.assertEqual(b'250 2.1.0 Ok \xf0\x9f\x91\x8d', bytes(r))

def test_is_error(self):
replies = [Reply(str(i)+'50', 'Test') for i in range(1, 6)]
Expand Down

0 comments on commit fc532be

Please sign in to comment.