Unicode fixes for python 2.x

These changes have been tested with Python 3.2.3 and Python 2.7.5.
This commit is contained in:
Kill Your TV 2013-08-14 15:45:33 +00:00
parent dd37f8dd3f
commit b46a0dd6a2
3 changed files with 26 additions and 16 deletions

View File

@ -33,6 +33,7 @@ import types
import socket
import threading
import re
import sys
import supybot.conf as conf
import supybot.utils as utils
@ -155,17 +156,22 @@ class RSS(callbacks.Plugin):
if self.registryValue(dateconfig, channel):
if headline[2]:
pubDate = ' [%s]' % (headline[2],)
try:
if sys.version_info[0] < 3:
if isinstance(headline[0], unicode):
newheadlines.append(format('%s %u%s',
headline[0].encode('utf-8','replace'),
link,
pubDate))
else:
newheadlines.append(format('%s %u%s',
headline[0].decode('utf-8','replace'),
link,
pubDate))
else:
newheadlines.append(format('%s %u%s',
headline[0],
link,
pubDate))
except UnicodeDecodeError:
newheadlines.append(format('%s %u%s',
headline[0].decode('utf8',
'replace'),
link,
pubDate))
headline[0],
link,
pubDate))
return newheadlines
def _newHeadlines(self, irc, channels, name, url):

View File

@ -153,10 +153,8 @@ class Web(callbacks.PluginRegexp):
domain = utils.web.getDomain(url)
title = utils.web.htmlToText(parser.title.strip())
if sys.version_info[0] < 3:
try:
if isinstance(title, unicode):
title = title.encode('utf8', 'replace')
except UnicodeDecodeError:
pass
s = format(_('Title: %s (at %s)'), title, domain)
irc.reply(s, prefixNick=False)
titleSnarfer = urlSnarfer(titleSnarfer)

View File

@ -450,11 +450,17 @@ def format(s, *args, **kwargs):
if char == 's':
token = args.pop()
if isinstance(token, str):
return token
if sys.version_info[0] < 3:
return token.decode('utf-8','replace')
else:
return token
elif sys.version_info[0] < 3 and isinstance(token, unicode):
return token.encode('utf8')
return token.encode('utf8', 'replace')
else:
return str(token)
if sys.version_info[0] < 3:
return str(token).decode('utf-8','replace')
else:
return str(token)
elif char == 'i':
# XXX Improve me!
return str(args.pop())