RSS._getConverter: Encode strings before handing them off to other functions

When the feed has a specified encoding, we'll be dealing with unicode objects
in the response from feedparser.parse().  To avoid possible UnicodeErrors, we
need to encode() before handing the string off to other functions, so the
other functions are always dealing with bytestrings instead of bytestrings and
unicode objects.  Mixing unicode and bytestrings will cause implicit
conversions of the unicode objects, which will most likely use the wrong
encoding.

Signed-off-by: James McCoy <jamessan@users.sourceforge.net>
This commit is contained in:
James McCoy 2011-10-22 15:23:56 -04:00
parent 01c8dc7f78
commit 964c73f591

View File

@ -262,8 +262,14 @@ class RSS(callbacks.Plugin):
def _getConverter(self, feed): def _getConverter(self, feed):
toText = utils.web.htmlToText toText = utils.web.htmlToText
if 'encoding' in feed: if 'encoding' in feed:
return lambda s: toText(s).strip().encode(feed['encoding'], def conv(s):
'replace') # encode() first so there implicit encoding doesn't happen in
# other functions when unicode and bytestring objects are used
# together
s = s.encode(feed['encoding'], 'replace')
s = toText(s).strip()
return s
return conv
else: else:
return lambda s: toText(s).strip() return lambda s: toText(s).strip()