Commits

Anonymous committed 7471bb0

fix pb with encoding in unicode.

  • Participants
  • Parent commits 4f62dc0

Comments (0)

Files changed (2)

File friendpaste/utils/__init__.py

     elif not isinstance(value, datetime):
         value = datetime.combine(value, time(0))
     return value.replace(microsecond=0).isoformat() + 'Z'
-    
+
+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 strings_only and isinstance(s, (types.NoneType, int, long, datetime.datetime, datetime.date, datetime.time, float)):
+        return s
+    try:
+        if not isinstance(s, basestring,):
+            if hasattr(s, '__unicode__'):
+                s = unicode(s)
+            else:
+                try:
+                    s = unicode(str(s), encoding, errors)
+                except UnicodeEncodeError:
+                    if not isinstance(s, Exception):
+                        raise
+                    # If we get to here, the caller has passed in an Exception
+                    # subclass populated with non-ASCII data without special
+                    # handling to display as a string. We need to handle this
+                    # without raising a further exception. We do an
+                    # approximation to what the Exception's standard str()
+                    # output should be.
+                    s = ' '.join([force_unicode(arg, encoding, strings_only,
+                            errors) for arg in s])
+        elif not isinstance(s, unicode):
+            # Note: We use .decode() here, instead of unicode(s, encoding,
+            # errors), so that if s is a SafeString, it ends up being a
+            # SafeUnicode at the end.
+            s = s.decode(encoding, errors)
+    except UnicodeDecodeError, e:
+        raise CouchitUnicodeDecodeError(s, *e.args)
+    return s
     
 def make_hash(*args):
     if len(args) <= 0:

File friendpaste/views.py

 from friendpaste.template import render_response, highlighter, pretty_type, shighlight, \
 render_template, url_for, ALL_COLORSHEME 
 from friendpaste.utils import local, local_manager, datetimestr_topython,\
-strptime, make_hash, datetime_tojson, send_stomp_msg
+strptime, make_hash, datetime_tojson, send_stomp_msg, force_unicode
 from friendpaste.utils.diff import get_unchanged_lines
 from friendpaste.utils.html import sanitize_html
 
         last_inserted = d.get('nb_lines', last_inserted)
         new_review = {
             "nickname": d.get('nickname', 'anonymous'),
-            "comment": sanitize_html(d.get('new_review', '')), 
+            "comment": force_unicode(sanitize_html(d.get('new_review', ''))), 
             "created": datetime_tojson(datetime.utcnow())
         }
         if review is not None: