[unicode] django.utils.functional.lazy can't decorate functions with unicode results
|Reported by:||Owned by:||Malcolm Tredinnick|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Currently the admin page showing, say, a user object is broken for Russian translation. I've traced the reason -- a leaky abstraction of a lazy proxy object.
The proxy object created by 'lazy' should pretend to be an object of type returned by a decorated function. And this should work:
ugettext = lazy(ugettext, unicode) s = ugettext(...) # a proxy object pretending to be a unicode instance s = unicode(s)
But it doesn't work because unicode(s) checks:
- if s is an instance of unicode (it isn't)
- if s has
- if s has
__str___, which it does (! unicode object itself has it)
Then it calls
__str__ on unicode object and it breaks for non-ascii characters.
I've created a fix but it seems a bit fragile because it creates a special case for unicode objects in lazy proxies.
P.S. The patch also contains two str -> smart_unicode conversions that don't strictly belong here but they are required for the testcase to work.
Change History (7)
comment:1 Changed 10 years ago by
|Cc:||Malcolm Tredinnick removed|
|Triage Stage:||Unreviewed → Accepted|