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 threading
import re
import sys
import supybot.conf as conf
import supybot.utils as utils
@ -155,15 +156,20 @@ 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],
headline[0].encode('utf-8','replace'),
link,
pubDate))
except UnicodeDecodeError:
else:
newheadlines.append(format('%s %u%s',
headline[0].decode('utf8',
'replace'),
headline[0].decode('utf-8','replace'),
link,
pubDate))
else:
newheadlines.append(format('%s %u%s',
headline[0],
link,
pubDate))
return newheadlines

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,9 +450,15 @@ def format(s, *args, **kwargs):
if char == 's':
token = args.pop()
if isinstance(token, str):
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:
if sys.version_info[0] < 3:
return str(token).decode('utf-8','replace')
else:
return str(token)
elif char == 'i':