diff options
| author | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2007-06-01 07:30:40 +0000 |
|---|---|---|
| committer | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2007-06-01 07:30:40 +0000 |
| commit | 554f4e7aa8a3ffe65cac3b6698906c06e7db4fe6 (patch) | |
| tree | c014732f8ef6dddcaa198e04d8701c9f4afababe /django/utils | |
| parent | 7a30b4e03ae8848792bd114741bc2fb175d280a4 (diff) | |
unicode: Reverted [5388] and fixed the problem in a different way. Checked
every occurrence of smart_unicode() and force_unicode() that was not previously
a str() call, so hopefully the problems will not reoccur. Fixed #4447. Refs #4435, #4430.
git-svn-id: http://code.djangoproject.com/svn/django/branches/unicode@5400 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/utils')
| -rw-r--r-- | django/utils/encoding.py | 12 | ||||
| -rw-r--r-- | django/utils/feedgenerator.py | 30 |
2 files changed, 24 insertions, 18 deletions
diff --git a/django/utils/encoding.py b/django/utils/encoding.py index 2fe6a2d970..7515d0c41b 100644 --- a/django/utils/encoding.py +++ b/django/utils/encoding.py @@ -12,22 +12,26 @@ class StrAndUnicode(object): def __str__(self): return self.__unicode__().encode('utf-8') -def smart_unicode(s, encoding='utf-8', errors='strict'): +def smart_unicode(s, encoding='utf-8', strings_only=False, errors='strict'): """ Returns a unicode object representing 's'. Treats bytestrings using the 'encoding' codec. + + If strings_only is True, don't convert (some) non-string-like objects. """ if isinstance(s, Promise): # The input is the result of a gettext_lazy() call. return s - return force_unicode(s, encoding, errors) + return force_unicode(s, encoding, strings_only, errors) -def force_unicode(s, encoding='utf-8', errors='strict'): +def force_unicode(s, encoding='utf-8', strings_only=False, errors='strict'): """ Similar to smart_unicode, except that lazy instances are resolved to strings, rather than kept as lazy objects. + + If strings_only is True, don't convert (some) non-string-like objects. """ - if s is None: + if strings_only and isinstance(s, (types.NoneType, int)): return s if not isinstance(s, basestring,): if hasattr(s, '__unicode__'): diff --git a/django/utils/feedgenerator.py b/django/utils/feedgenerator.py index 546d73c7f1..064ec19120 100644 --- a/django/utils/feedgenerator.py +++ b/django/utils/feedgenerator.py @@ -42,20 +42,21 @@ class SyndicationFeed(object): def __init__(self, title, link, description, language=None, author_email=None, author_name=None, author_link=None, subtitle=None, categories=None, feed_url=None, feed_copyright=None): + to_unicode = lambda s: force_unicode(s, strings_only=True) if categories: categories = [force_unicode(c) for c in categories] self.feed = { - 'title': force_unicode(title), + 'title': to_unicode(title), 'link': iri_to_uri(link), - 'description': force_unicode(description), + 'description': to_unicode(description), 'language': force_unicode(language), - 'author_email': force_unicode(author_email), - 'author_name': force_unicode(author_name), + 'author_email': to_unicode(author_email), + 'author_name': to_unicode(author_name), 'author_link': iri_to_uri(author_link), - 'subtitle': force_unicode(subtitle), + 'subtitle': to_unicode(subtitle), 'categories': categories or (), 'feed_url': iri_to_uri(feed_url), - 'feed_copyright': force_unicode(feed_copyright), + 'feed_copyright': to_unicode(feed_copyright), } self.items = [] @@ -67,21 +68,22 @@ class SyndicationFeed(object): objects except pubdate, which is a datetime.datetime object, and enclosure, which is an instance of the Enclosure class. """ + to_unicode = lambda s: force_unicode(s, strings_only=True) if categories: - categories = [force_unicode(c) for c in categories] + categories = [to_unicode(c) for c in categories] self.items.append({ - 'title': force_unicode(title), + 'title': to_unicode(title), 'link': iri_to_uri(link), - 'description': force_unicode(description), - 'author_email': force_unicode(author_email), - 'author_name': force_unicode(author_name), + 'description': to_unicode(description), + 'author_email': to_unicode(author_email), + 'author_name': to_unicode(author_name), 'author_link': iri_to_uri(author_link), 'pubdate': pubdate, - 'comments': force_unicode(comments), - 'unique_id': force_unicode(unique_id), + 'comments': to_unicode(comments), + 'unique_id': to_unicode(unique_id), 'enclosure': enclosure, 'categories': categories or (), - 'item_copyright': force_unicode(item_copyright), + 'item_copyright': to_unicode(item_copyright), }) def num_items(self): |
