String.decode: Only encode('utf-8') when the decode string is unicode

Closes: Sf#3165718
Signed-off-by: James McCoy <jamessan@users.sourceforge.net>
(cherry picked from commit 01c8dc7f78)

Signed-off-by: Daniel Folkinshteyn <nanotube@users.sourceforge.net>
This commit is contained in:
James McCoy 2011-10-22 14:57:20 -04:00 committed by Daniel Folkinshteyn
parent f9377108b0
commit 50e4b6baf1

View File

@ -81,7 +81,12 @@ class String(callbacks.Plugin):
<http://docs.python.org/library/codecs.html#standard-encodings>.
"""
try:
irc.reply(text.decode(encoding).encode('utf-8'))
s = text.decode(encoding)
# Not all encodings decode to a unicode object. Only encode those
# that do.
if isinstance(s, unicode):
s = s.encode('utf-8')
irc.reply(s)
except LookupError:
irc.errorInvalid('encoding', encoding)
except binascii.Error: