Merge pull request #506 from kytvi2p/unicode2.x

Unicode fixes for python 2.x
This commit is contained in:
Valentin Lorentz 2013-08-17 07:54:45 -07:00
commit dfdc7f1cd1
3 changed files with 26 additions and 16 deletions

View File

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

View File

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

View File

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