Open new items in text mode (unicode), and safely handle unicode in news item v2.2_rc38
authorZac Medico <zmedico@gentoo.org>
Mon, 10 Aug 2009 08:15:05 +0000 (08:15 -0000)
committerZac Medico <zmedico@gentoo.org>
Mon, 10 Aug 2009 08:15:05 +0000 (08:15 -0000)
names.

svn path=/main/trunk/; revision=13975

pym/portage/news.py

index 750df3dd6fa78757c4bbbecbb5182b4ecce98d80..c6bd9eafa315beca41004555695223e3cfe80530 100644 (file)
@@ -7,6 +7,7 @@ __all__ = ["NewsManager", "NewsItem", "DisplayRestriction",
        "DisplayProfileRestriction", "DisplayKeywordRestriction",
        "DisplayInstalledRestriction"]
 
+import codecs
 import logging
 import os
 import re
@@ -116,6 +117,8 @@ class NewsManager(object):
                                        itemid + "." + self.language_id + ".txt")
                                if not os.path.isfile(filename):
                                        continue
+                               if not isinstance(itemid, unicode):
+                                       itemid = unicode(itemid, encoding='utf_8', errors='replace')
                                item = NewsItem(filename, itemid)
                                if not item.isValid():
                                        continue
@@ -224,7 +227,8 @@ class NewsItem(object):
                return self._valid
 
        def parse(self):
-               lines = open(self.path).readlines()
+               lines = codecs.open(self.path, mode='r',
+                       encoding='utf_8', errors='replace').readlines()
                self.restrictions = []
                invalids = []
                for i, line in enumerate(lines):