Sync py2_unicode decorator with Django.
authorBerker Peksag <berker.peksag@gmail.com>
Mon, 2 Jun 2014 22:41:44 +0000 (01:41 +0300)
committerBerker Peksag <berker.peksag@gmail.com>
Mon, 2 Jun 2014 22:41:44 +0000 (01:41 +0300)
mediagoblin/_compat.py

index c38e0727ab8d351b137efa12a5cde1f86fdae760..ab5dbcf6c37b5930f91586d6ace2abb2bde31973 100644 (file)
@@ -1,5 +1,3 @@
-import sys
-
 from six import PY3, iteritems
 
 from mediagoblin import mg_globals
@@ -17,8 +15,13 @@ else:
     ungettext = mg_globals.thread_scope.translations.ungettext
 
 
+# taken from https://github.com/django/django/blob/master/django/utils/encoding.py
 def py2_unicode(klass):
     if not PY3:
+        if '__str__' not in klass.__dict__:
+            raise ValueError("@py2_unicode cannot be applied "
+                             "to %s because it doesn't define __str__()." %
+                             klass.__name__)
         klass.__unicode__ = klass.__str__
         klass.__str__ = lambda self: self.__unicode__().encode('utf-8')
     return klass