[unicode] django.utils.functional.lazy can't decorate functions with unicode results
|Reported by:||Ivan Sagalaev <Maniac@…>||Owned by:||mtredinnick|
|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 __unicode__ (it doesn't)
- 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)
Changed 7 years ago by Ivan Sagalaev <Maniac@…>
comment:1 Changed 7 years ago by mtredinnick
- Cc mtredinnick removed
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
- Triage Stage changed from Unreviewed to Accepted
comment:5 Changed 7 years ago by mtredinnick
- Resolution set to fixed
- Status changed from new to closed